Database class returns error: supplied argument is not a ...
Posted: Sun Apr 20, 2008 5:02 am
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
index.php
All criticism is muchly appreciated!
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));
}
}
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>