markusn00b Posted April 20, 2008 Share Posted April 20, 2008 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 <?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)); // line 99 } } index.php <!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! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 20, 2008 Share Posted April 20, 2008 Your problem is that you never define $this->Where On a different note, it's generally bad practice to give your php scripts the extention .inc Unless you set up your webserver to parse .inc files as .php files, someone could browse directly to the page and view the PHP source code. And even if you do, you could change hosts and forget about this, which could create a security issue down the line. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.