mrbean Posted December 17, 2012 Share Posted December 17, 2012 I have a little problem i hope someone can help me with it. Here's the code: <?php session_start(); echo $_SESSION['username']; // result: demo include($_SERVER['DOCUMENT_ROOT']."/database.php"); $database = new database("mysql"); $database->query("SELECT `first_time`, `money`, `life` FROM `[users]` WHERE `username` = '".$_SESSION['username']."' ","userdata"); $user = mysql_fetch_object($checkfirstime); ?> Database: http://ctrlv.in/145476 Database.php: <?php class database { public function __construct($type) { global $server,$username,$password,$databasename,$querytype,$mysql,$mysqli,$mssql; if($type === "mysql") { $mysql = mysql_connect($server, $username, $password)or die("kan geen verbinding maken met de server, de volgende error is voortgekomen:".mysql_error()); mysql_select_db($databasename, $mysql)or die("kan geen verbinding maken met de database, de volgende error is voortgekomen:".mysql_error()); } else if($type === "mysqli") { $mysqli = new mysqli($server, $username, $password, $database); } else if($type === "mssql") { $mssql = mssql_connect($server, $username, $password); mssql_select_db($database, $mssql); } $querytype = $type; } public function query($query,$naam = "GEEN") { global ${$naam},$querytype; if($querytype === "mysql") { ${$naam} = mysql_query($query)or die('error in '.$naam.': '.mysql_error()); } else if($querytype === "mysqli") { ${$naam} = $mysqli->query($query)or die('error in '.$naam.': '.mysql_error()); } else if($querytype === "mssql") { ${$naam} = mssql_query($query)or die('error in '.$naam.': '.mysql_error()); } } } ?> The error I get: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in engine.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/ Share on other sites More sharing options...
Muddy_Funster Posted December 17, 2012 Share Posted December 17, 2012 (edited) the error is because the query has failed. Loom into using an or die(mysql_error()) arror catch for debugging and development, it will help you catch any errors from the database, such as this. Problem is most likely coming from : FROM `[users]` don't put backticks around table names - unless you have chosen (against all good sense) to use a reserved word for the table name, and dont use the [ ] either, unless for some obscure reason you actualy created a table with those in the name. Edited December 17, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1399835 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2012 Share Posted December 17, 2012 Sorry to be blunt, but your code is a gross misuse of a class/oop and variable variables, to the point that you don't even know the variable name that your query's result resource is in. Hint: it's not in $checkfirstime. One of the points of using classes/oop is the code and data needed for any particular purpose is encapsulated/self-contained. By using the global keyword and a variable variable on top of that, you are breaking that encapsulation/self-containment. Another point of functions/class methods is that they return the result they produce. Your ->query() method should return the false/true/result resource and you assign that to a program variable - $result = $database->query(.....); Forget you ever saw the global keyword and variable variables. They result in code that you won't be able to troubleshoot simple problem with (and would get you fired from a large project where unexplained values changing due to them will cause a bunch of wasted time for the team working on the project.) -------------------------------- Some hints for your class - 1) The type should be stored in a class property. The rest of the class code uses that stored value. 2) You should pass the database connection details/database name into the class, either in the constructor or using a specific 'setter' class method call. 3) The database link resource should be stored in a class property, i.e. $link (there's no need to have separate $mysql, $mysqli, $mssql since any one instance of your class can/should only operate on one database type.) 4) The query method should return the result from the query. Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1399839 Share on other sites More sharing options...
mrbean Posted December 18, 2012 Author Share Posted December 18, 2012 (edited) Sorry to be blunt, but your code is a gross misuse of a class/oop and variable variables, to the point that you don't even know the variable name that your query's result resource is in. Hint: it's not in $checkfirstime. One of the points of using classes/oop is the code and data needed for any particular purpose is encapsulated/self-contained. By using the global keyword and a variable variable on top of that, you are breaking that encapsulation/self-containment. Another point of functions/class methods is that they return the result they produce. Your ->query() method should return the false/true/result resource and you assign that to a program variable - $result = $database->query(.....); Forget you ever saw the global keyword and variable variables. They result in code that you won't be able to troubleshoot simple problem with (and would get you fired from a large project where unexplained values changing due to them will cause a bunch of wasted time for the team working on the project.) -------------------------------- Some hints for your class - 1) The type should be stored in a class property. The rest of the class code uses that stored value. 2) You should pass the database connection details/database name into the class, either in the constructor or using a specific 'setter' class method call. 3) The database link resource should be stored in a class property, i.e. $link (there's no need to have separate $mysql, $mysqli, $mssql since any one instance of your class can/should only operate on one database type.) 4) The query method should return the result from the query. Thank you for your help, I will change my class after I fixed this. I have changed the code, and tried this: $userdata = mysql_query("SELECT `first_time`, `money`, `life` FROM users WHERE 'username' = 'demo'"); $user = mysql_fetch_object($userdata)or die("ERROR: ".mysql_error()); echo $user->money; Result: ERROR: Edited December 18, 2012 by mrbean Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1400187 Share on other sites More sharing options...
Christian F. Posted December 19, 2012 Share Posted December 19, 2012 (edited) You need to move the "or die()" part one line up. It's meant to go after mysql_query (), to show if there are any errors with the SQL query itself. Edited December 19, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1400316 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 username has single quotes around it, not backticks.... Quote Link to comment https://forums.phpfreaks.com/topic/272099-mysql_fetch_object-supplied-argument-is-not-a-valid-mysql-result-resource/#findComment-1400318 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.