Page 1 of 1

mysql_fetch_object(): supplied argument is not a valid MySQL

Posted: Sun Feb 11, 2007 12:27 pm
by psychotomus
how do I fix these so I don't get that error?

Code: Select all

$owned_air1 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='Liverpool' AND username='$username'"));
$owned_air2 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='Manchester' AND username='$username'"));
$owned_air3 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='New York' AND username='$username'"));
$owned_air4 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='Compton' AND username='$username'"));
$owned_air5 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='Amsterdam' AND username='$username'"));
$owned_air6 = mysql_fetch_object(mysql_query("SELECT id FROM airport WHERE location='Hong Kong' AND username='$username'"));

Posted: Sun Feb 11, 2007 12:31 pm
by wildwobby
connect to the mysql databse first with:

Code: Select all

mysql_connect('localhost', 'mysql_user', 'mysql_password');
....

Posted: Sun Feb 11, 2007 12:41 pm
by psychotomus
Its allready connected as its allready performing SQL task before i call those SQL's such as

Code: Select all

$query_user=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");
$fetch_user=mysql_fetch_object($query_user);

$query_air=mysql_query("SELECT * FROM airport WHERE location='$fetch_user->location' LIMIT 1");
$fetch_air=mysql_fetch_object($query_air);
and thsoe don't get no errors.

Posted: Sun Feb 11, 2007 12:43 pm
by wildwobby
psychotomus wrote:Its allready connected as its allready performing SQL task before i call those SQL's such as

Code: Select all

$query_user=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");
$fetch_user=mysql_fetch_object($query_user);

$query_air=mysql_query("SELECT * FROM airport WHERE location='$fetch_user->location' LIMIT 1");
$fetch_air=mysql_fetch_object($query_air);
and thsoe don't get no errors.
Ok, maybe try storing the mysql_query() result in a variable than storing the mysql_fetch_object() in another variable?

Posted: Sun Feb 11, 2007 12:57 pm
by volka

Code: Select all

function fetch_object($query) {
	$result = mysql_query($query) or die(mysql_error().': '.$query);
	$obj = mysql_fetch_object($result);
	if ( false===$obj ) {
		die("no result for '$query'");
	}
	return $obj;
}

$owned_air1 = fetch_object("SELECT id FROM airport WHERE location='Liverpool' AND username='$username'");
$owned_air2 = fetch_object("SELECT id FROM airport WHERE location='Manchester' AND username='$username'");
$owned_air3 = fetch_object("SELECT id FROM airport WHERE location='New York' AND username='$username'");
$owned_air4 = fetch_object("SELECT id FROM airport WHERE location='Compton' AND username='$username'");
$owned_air5 = fetch_object("SELECT id FROM airport WHERE location='Amsterdam' AND username='$username'");
$owned_air6 = fetch_object("SELECT id FROM airport WHERE location='Hong Kong' AND username='$username'");