tags:

views:

37

answers:

3

I recently rebuilt my website and now I'm getting this error:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 28

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 29

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 30

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 31

Those lines are as follows:

mysql_connect($dbhost,$dbuser,$dbpass)
or die("Error: Failed to connect to database");

mysql_select_db($dbname)
or die("Error: Failed to select databse");

$query = "SELECT * FROM foxlose ORDER BY RAND ( ) LIMIT 1";

$result = mysql_query($query);



$title = mysql_result($result,$i,"title");
$link = mysql_result($result,$i,"link");
$nomen = mysql_result($result,$i,"name");
$text = mysql_result($result,$i,"text");


mysql_close();

The last four lines before mysql_close(); are lines 28-31.

Only here's the thing: those lines were the same in the old version of my site, and they worked. I can't figure out what's changed. The code also works fine when I use my computer as a server and host it locally.

Any idea what this error is trying to tell me?

Edit: Added all mysql in the document.

+5  A: 

It seems to me odd that you close the connection to a database by:

mysql_close();

and then you use

$variable = mysql_result(...);

Generally, if you want to know the reason of the error use the function mysql_error.

MartyIX
MartyIX is right, add the mysql_close() at the bottom, after all the queries
krike
Disagree. MySQL Close simply closes the connection. His results are already stored in a variable. The mysql_result in no way relies on mysql_close. Ideally he should be using mysql_fetch_array or fetch_assoc for performance but either way it shouldn't be looking for an open connection to parse a variable set
CogitoErgoSum
the php script is trying to find a database reference - thats how mysql and php work. As you've closed the reference you are still processing the function, but its not a valid resource because it can't find the reference. I'm not really sure mysql_close() is even necessary. I can only think that you would use it if you were accessing multiple databases.
Thomas Clayson
I am still getting the same error with mysql_close(); after all mysql queries
John
agree with CogitoErgoSum - you should be using mysql_fetch_array() instead of mysql_result(). mysql_result is ugly mainly because it will throw an error if the query returns no rows.
Thomas Clayson
ok... just do this after `$result = mysql_query($query)`: `if(!$result) {echo "Not working";} if(!mysql_num_rows($result)) {echo "Not working either";}` run it and see if it echos either of those.
Thomas Clayson
@Thomas: http://www.php.net/manual/en/function.mysql-result.php#86175 - Warning or error? I can't try out right now. But it's interesting behaviour, didn't know that, thanks!
MartyIX
Turns out I had simply formatted my rand() incorrectly, as khaarstad pointed out below. I'm going to work on using fetch_array instead of result though. Thanks!
John
@MartyIX hmm... I don't really know to be honest - I just know its not very friendly, and there's probably no reason to be using it when you could be using mysql_fetch_array/assoc instead. :)
Thomas Clayson
+1  A: 

Try removing the space after RAND:

$query = "SELECT * FROM foxlose ORDER BY RAND() LIMIT 1;

This was it! Thank you so much. Looking back now, I was using a different method of selecting a random entry in the previous version; I just thought that was such a semantic issue it didn't need to be mentioned. I must be running a different version of mysql on my computer than the one my webhost has.
John
Just to note, on a production server you may want to rethink using RAND() as it can be an expensive operation. There are no elegant solutions but it may be more efficient (though not easier) to get a row count and randomize with PHP between 1 and the row count and do a LIMIT $rand, 1 than use mySQLs RAND
CogitoErgoSum
A: 

There are several things you could do here.

  1. Check the mysql_query connection is actually set. Alternately youcan ensure the connection is being passed by passing it to the mysql query

i.e.

mysql_query($query, $connction);
  1. Additionally, Thomas Clayson pointed out, ensure you are getting results. If there are no results mysql_result will throw an error

  2. Ensure mysql_select_db (assuming you are using this), is connecting to the database properly.

  3. Depending on versions and such you may need a semicolon to close your mysql statement

i.e.

$query = "SELECT * FROM foxlose ORDER BY RAND () LIMIT 1;";

Much of this has been taken from other people with similar issues. full credit (and more ideas) here: http://www.daniweb.com/forums/thread26425.html

CogitoErgoSum