I am trying to pull a random record out of a database running MYSQL. I get the error code "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource". The code is <?php // Create the connection and select the DB $link = mysql_connect("localhost","user","password"); if ($link) { mysql_selectdb("test",$link); // Select records from the DB $query = "SELECT * FROM demo ORDER BY Rand() LIMIT 2"; $result = mysql_query($query); // Display records from the table echo "<table border='1'>"; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr>"; } echo "</table>"; } else { echo "Can't connect to the database!"; } ?> PHP: Does anyone know whats happing?
Usually when you get it means your info is wrong (table, db, etc.) or the result is empty (there is no data in the table) so check that. Also I don't know if it's just a typo or the problem but "mysql_selectdb" should be "mysql_select_db" (LINE 7) Hope that helps
You can use this to debug also: $result = mysql_query($query); if( !$result ) { echo mysql_error(); } PHP:
RAND() is a function! The problem is most likely with mysql_selectdb() which should be mysql_select_db() - note the extra underscore
Thanks All! I found someone that could help me in person, but he told me exactly the same stuff that you did! Your info will help with other problems I am sure!
Hi I know this is a very old thread. I have replied that for the all future visitors with a view to helping them: see below: $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId"); Cart is table name here. I know that when table name is too short and without underscore thn mql shows the error. because mysql can not detect short name. when table name is too short and without underscore thn you must have to add ` both side of the table name in query. Or if u dont want to add ` thn select a name with a underscore or select a long name. See example and try below one and both will work : $result = mysql_query("select count(*) from `cart` where cookieId = '" . GetCartId() . "' and itemId = $itemId"); or, $result = mysql_query("select count(*) from cart_1 where cookieId = '" . GetCartId() . "' and itemId = $itemId");
Also getting: PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Code: while ($row = call_user_func($func_name,$res_id)){ Any Suggestions?
Also getting: PHP Warning: Invalid argument supplied for foreach() Code:$r = DBquery($q); $out = array(); foreach ($r as $item) { $id = $item['id']; Any Suggestions?