Database class returns error: supplied argument is not a ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Database class returns error: supplied argument is not a ...

Post by markusn00b »

Error: mysql_num_rows() supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\test\Database.inc on line 99.

Now, i decided it tackle OOP as it is a very necessary skill to have; i've learnt that from reading articles on here.

Upon deciding to create a simple connection class, i elaborated on in so it would run the queries, as well.

I guess this is me calling for your help with the error, and also a coding critique - i know it's very sloppy and four-letter-word worthy.

Database.inc

Code: Select all

 
<?php
 
/**************************************************************|
| This is the connection class for a php to mysql connection   |
| Created by a complete n00b, it will cover the very basics    |
| and if i come accross anything that should be included,      |
| it shall be.                                                 |
|                                                              |
|--------------------------------------------------------------
| TO DO:
|  - Allow for array's to be passed in assign()
 
*/
 
 
 
class DB {
    
    /**********************************************************|
    | Defining the common variables.                           |
    | DBName    = The name of the database to be used.         |
    | DBHost    = Where the DataBase is. (usually 'localhost') |
    | DBPass    = The password to use for access to the DB     |
    | DBUser    = The username to  "   "    "    "   "  "      |
    |----------------------------------------------------------*/
    
    var $DBName = '***';
    var $DBHost = '***';
    var $DBPass = '***';
    var $DBUser = '***';
    var $Where;
    var $Limit;
    var $Order;
    var $Table;
    
    /**********************************************************|
    | If you were to assign the variables outside the          |
    | Database.inc, you would do so like:                      |
    |                                                          |
    |       $DB->assign('DBName', 'mahcuz');                   |
    |       $DB->assign('DBHost', 'localhost');                |
    |       $DB->assign('DBUser', 'mahcuzdbadmin');            |
    |       $DB->assign('DBPass', 'ideomotor');                |
    |----------------------------------------------------------*/
    
    function assign($var_name, $val)
    {
        /*
        | Array to string conversion won't go down with poppa php!
        |*/
    
        if(is_array($var_name))
        {
            die("Passing arrays isn't allowed. Please reformat the code to assign each C.Vars.");
        }
        
        $this->$var_name = $val;
        return $this->$var_name;
        
    }
    
    
    
    function connect_mysql()
    {
        
        mysql_connect($this->DBHost, $this->DBUser, $this->DBPass) or die(mysql_error());
        
    }
    
    
    function select_db_mysql()
    {
        
        mysql_select_db($this->DBName);
        
    }
    
    
    function set_table($_Table)
    {
        $this->Table = $_Table;
    }
    
    
    function get_data($_Where = null)
    {
        return mysql_query("SELECT * FROM `{$this->Table}` {$this->Where}") or die(mysql_error());
    }
        
        
    function run_query($Query)
    {
        mysql_query($Query) or die(mysql_error());
    }
        
    function num_rows()
    {
        return mysql_num_rows($this->get_data($this->Where));
    }
    
}
 
index.php

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
 
<body>
 
<?php 
include("Database.inc");
 
$DB = new DB();
 
$DB->connect_mysql();
 
$DB->select_db_mysql();
 
$DB->set_table('Members_Info');
 
$DB->get_data('`Username` = "mahcuz"');
 
echo $DB->num_rows();
 
?>
 
</body>
 
All criticism is muchly appreciated!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Database class returns error: supplied argument is not a ...

Post by Kieran Huggins »

First of all, you should always use a DB handle:

Code: Select all

$this->DB = mysql_connect($this->DBHost, $this->DBUser, $this->DBPass) or die(mysql_error());
You should also use result handles

Code: Select all

$this->result = mysql_query("SELECT * FROM `{$this->Table}` {$this->Where}") or die(mysql_error());
return $this->result;
Then grabbing the number of affected rows could be:

Code: Select all

mysql_num_rows($this->result);
Post Reply