Jump to content

Mysql_Fetch_Object(): Supplied Argument Is Not A Valid Mysql Result Resource


mrbean

Recommended Posts

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:

 

  Quote
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in engine.php on line 7

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 :

  Quote

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.

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.

  On 12/17/2012 at 3:50 PM, PFMaBiSmAd said:

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:

post-92629-0-50023500-1355868931_thumb.png

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.