-2

I'm moving a legacy php site over to a new host. There is a file that connects the php to the DB, but it is throwing an error on the new server. Everything seems to work.

The error is:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/jjrods/public_html/scripts/php/db.php on line 21.

Below is the script:

<?php
class dataBase {

    public $conn;
    public $result;
    public $numrows;

    //constructor  - connects to db
    public function __construct($query = null) {
        $this->conn = mysql_connect('localhost', 'userName', 'passWord');
        if ($this->conn == false)
            return false;
        mysql_select_db('new_Database');
        return ($query) ? $this->sql($query) : $this->conn;
    }//end __construct method

    //write sql query to database
    public function sql($query) {
        $this->result = mysql_query($query);
        if ($this->result)
            $this->numrows = mysql_num_rows($this->result);
        return $this->result;
    }//end sql method

}//end dataBase class
?>

When I set this same site up locally, I didn't have these issues. Could it be a permissions issue?

So I've added the code from the page that errors. It seems to be doing a select as required by num_rows. Does this help anyone see why it throwing an error on one server, but not another?

if (isset ( $_GET ["id"] )) {
$db2 = new dataBase ( "SELECT * FROM car WHERE carId=" . $_GET ["id"] . " AND type='Muscle'" );
if ($db2->numrows > 0) {
    // car found
    $car = mysql_fetch_array ( $db2->result, MYSQL_ASSOC );
    $db2 = new dataBase ( "UPDATE car SET views=" . ($car ['views'] + 1) . " WHERE carId=" . $car ['carId'] );
    $db2 = new dataBase ( "SELECT location FROM pic WHERE carId=" . $car ['carId'] . " ORDER BY orderNum ASC" );
    if ($db2->numrows > 0) {
        // get pictures
        while ( $row = mysql_fetch_array ( $db2->result, MYSQL_ASSOC ) ) {
            $pics [] = $row ['location'];
        }
    } else
        $isError = true;

Thanks for any ideas!

  • 2
    You cannot `return false` from a constructor. The constructor will always return an instance of the class `dataBase` even though it may be incomplete or in an error state, so the connection is probably faulty and your code doesn't respond appropriately to that fault. – Michael Berkowski Dec 16 '13 at 18:48
  • ... so if you did something like `$db = new dataBase()` and it failed, you cannot test it with `if ($db)` to verify that it was created because the object will always be truthy. – Michael Berkowski Dec 16 '13 at 18:50
  • 1
    @andrewsi It looks like he _is_ checking the return value of `mysql_query` with `if ($this->result)`. – Barmar Dec 16 '13 at 18:52
  • @Barmar - huh; so he is. Thank you for pointing that out. – andrewsi Dec 16 '13 at 18:55
  • When I looked at the link people have marked as the same question, it talks about setting up the server to display errors correctly. Would this suggest the current server (www.jjrods.com and my local machine) just aren't displaying the error. – Martin Malley Dec 16 '13 at 19:19
  • No. Errors detected by the MySQL server are never displayed automatically. Configuring `error_reporting(-1)` in your development environment is good general practice, but won't make a difference in this case. – Barmar Dec 16 '13 at 22:22

1 Answers1

1

Take a look at your method:

//write sql query to database
public function sql($query) {
    $this->result = mysql_query($query);
    if ($this->result)
        $this->numrows = mysql_num_rows($this->result);
    return $this->result;
}//end sql method

It sounds like $query is SQL for an INSERT or UPDATE statement. If so, mysql_query() returns true on success and false on failure, not an object, because these statements don't return result sets. Even if it is a SELECT query, if the query fails, mysql_query() returns false. Try var_dump($this->result); to see what your value is.

elixenide
  • 43,445
  • 14
  • 72
  • 97
  • If it's a `SELECT` query and it returns `false`, the `if ($this->result)` will correctly skip over the call to `mysql_num_rows`. – Barmar Dec 16 '13 at 18:53
  • My php understanding is pretty weak. I didn't set up this website. With that, what is the purpose of this script? It is the only one with the DB login information in it. – Martin Malley Dec 16 '13 at 19:34
  • I can't say, exactly, without seeing the entire app, but it certainly looks like it's intended to run all database queries on behalf of the rest of the application. That said, it's not well thought-out. If `$query` contains SQL for an `INSERT` or `UPDATE` statement (or a broken `SELECT` or other statement), the line `$this->numrows = mysql_num_rows($this->result);` will always fail. – elixenide Dec 16 '13 at 19:36
  • So I added var_dump in place of return $this. It came up with:bool(true) resource(6) of type (mysql result). Does that mean anything? The page and the items from the DB load correctly. – Martin Malley Dec 16 '13 at 20:06
  • That means your query succeeded, but didn't return a result set because it was an `INSERT`, `UPDATE`, or something similar. You can't run `mysql_num_rows()` on `true`. Also, by the way, [don't use mysql_*; it's deprecated.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1) – elixenide Dec 16 '13 at 20:09