0

So i'm working on a project and i am running to to a few mysql & php errors.

First Error :

This is all code associated with First Error

// Database Class 

class Database {
    public $_link, $_result, $_numRows;

    public function __construct($server, $username, $password, $db){
        $this->_link = mysql_connect($server, $username, $password) or die('Cannot Execute:'. mysql_error());
        mysql_select_db($db, $this->_link);
    }

    public function disconnect(){
        mysql_close($this->_link);
    }

    public function query($sql){
        $this->_result  = mysql_query($sql, $this->_link) or die('Cannot Execute:'. mysql_error());
        $this->_numRows = mysql_num_rows($this->_result);
    }

    public function numRows() {
        return $this->_numRows;
    }

    public function rows(){
        $rows = array();
        for($x = 0; $x < $this->numRows(); $x++) {
            $rows[] = mysql_fetch_assoc($this->_result);    
        }
        return $rows;
    }

}

// Setup.php 

$is_set = 'false'; 
global $company_name, $ebay_id;
    if( isset($_POST['ebay_id'], $_POST['company_name']) ){
        $_ebay_id = $_POST['ebay_id'];
        $_company_name = $_POST['company_name'];
        $is_set = 'true';
    } else { 
        // Silence is Golden!
    }

    $_service_database = new Database('localhost', 'root', 'password', 'chat-admin');

    $_service_database->query("SELECT * FROM installs WHERE `identity` = '$mysqldb' AND `db_name` = '$mysqldb'");


    if ($_service_database->numRows() == 0){ 
        if($is_set == true){
             $_service_database->query("INSERT INTO installs (ebay_id, name, db_name, identity) VALUES ('$_ebay_id', '$_company_name', '$mysqldb', '$mysqldb')");
        } 
    } else { 
        // header("Location: index.php");
    }
    $_service_database->disconnect();

?>

<form name="setup" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input placeholder="eBay Username" type="text" name="ebay_id">
<input placeholder="Company Name" type="text" name="company_name">
<input type="hidden" name="insert" value="true">
<input type="submit">
</form>

So what this does is checks if the variables defined are in the database if they are not then when the form is submitted it inserts our values, if they are in the database it redirects to index.php, ok so i am receiving the all mighty MySQL num_rows* expects parameter 1 to be resource, boolean given ..., now i have searched everywhere and only found that the die error should solve, that is why you can see it on line 16 in the function query, now although i get the error everything still works its not really good for me to turn error reporting off so any help would be great. Also if you see undefined variables like $mysqldb they are actually set but on a different page and they are valid

Second Error : = function made for simplicity of a templating system

This is all associated with Second Error

function get_content_type($value){
    if (strpos($value,'title:') === 0 ) {
        $title = explode("title: ",$value);
        $value = $title[1];
    } else if (strpos($value,'css:') === 0 ) {
        $css = explode("css: ",$value);
        $value = $css[1];
    } else if (strpos($value, 'js:') === 0 ) {
        $javascript = explode("js: ", $value);
        $value = $javascript[1];
    } else {
        // Silence is Golden
    }

    if($value == $title[1]){
        echo '<title>'.$value.'</title>';
    } else if ($value == $css[1]) {
        echo '<link rel="stylesheet" href="'.$value.'">';
    } else if ($value == $javascript[1]) { 
        echo '<script src="'.$value.'"></script>';
    }
}

// Calling
get_content_type('title: Welcome to my site');
get_content_type('css: http://something.com/style.css');
get_content_type('js: http://code.jquery.com/jquery-latest.min.js');

everything works as this outputs

<title>Welcome to my site</title><link rel="stylesheet" href="http://somesite.com/style.css"><script src="http://code.jquery.com/jquery-latest.min.js"></script>

Only when i set error_reporting(0);

The errors i get are undefined variables because obviously i'm setting one, then im unsettling that one and setting the other so eventually only 1 is true although its already outputted all of them correctly.

UPDATE

Second Error : Errors =

Notice: Undefined variable: css in C:\Server\www\hidie\libs\test.php on line 19 Notice: Undefined variable: title in C:\Server\www\hidie\libs\test.php on line 17 Notice: Undefined variable: title in C:\Server\www\hidie\libs\test.php on line 17

First Error : Errors =

ERROR: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

6
  • 2
    you should include the errors in your question. My crystal ball is in the shop. Commented Feb 6, 2013 at 1:47
  • Also, since this is abstract, num_rows will return false on that insert since you're generically calling it in query. num_rows only works on select and show. You need to check affected rows on other operations Commented Feb 6, 2013 at 1:50
  • i have updated the errors, also Kai Qing i don't understand what you are trying to tell me, i'm not a pro at this sorry Commented Feb 6, 2013 at 1:51
  • 1
    You also may want to switch over to mysqli instead or mysql PDO. mysql connect has been deprecated. Commented Feb 6, 2013 at 1:53
  • if($value == $title[1]) to if(isset($title) && is_array($title) && $value == $title[1]) Commented Feb 6, 2013 at 1:55

1 Answer 1

2

Ok so here's your problem:

First, as mentioned in the comment, mysql_num_rows only works on select and show. You have it in your query method, which is called by an insert. You can't do that.

public function query($sql){
    $this->_result  = mysql_query($sql, $this->_link) or die('Cannot Execute:'. mysql_error());
    $this->_numRows = mysql_num_rows($this->_result); // <- BAD!
}

You call it on this line:

if($is_set == true){
     $_service_database->query("INSERT INTO installs (ebay_id, name, db_name, identity) VALUES ('$_ebay_id', '$_company_name', '$mysqldb', '$mysqldb')");
} 

You could make a second method for database and call it for all calls that write to the db:

public function execute($sql){
        $this->_result  = mysql_query($sql, $this->_link) or die('Cannot Execute:'. mysql_error());
    }

which would be called like so:

if($is_set == true){
     $_service_database->execute("INSERT INTO installs (ebay_id, name, db_name, identity) VALUES ('$_ebay_id', '$_company_name', '$mysqldb', '$mysqldb')");
} 

Second, you need to implicitly define the variables $css and $title outside of those conditionals. Otherwise you are attempting to call undefined vars as indicated by the message...

$title = "";
$css = "";

if (strpos($value,'title:') === 0 ) {
    $title = explode("title: ",$value);
    $value = $title[1];
} else if (strpos($value,'css:') === 0 ) {
    $css = explode("css: ",$value);
    $value = $css[1];
} else if (strpos($value, 'js:') === 0 ) {
    $javascript = explode("js: ", $value);
    $value = $javascript[1];
}   else {
    // Silence is Golden
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much i set the 2 variables and it worked, Great! but. you say that the query is bad, so what do you suggest instead of that?
I updated my answer. Basically, you just make an execute method in the db class that does not try to call num_rows after the query. That's just one solution and probably the easiest to get used to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.