I get this error message when I try to use mysql_fetch_array():
while( $deffefgpSfet->fetch() ) {
$roefe= mysql_fetch_array($ffefe, MYSQL_NUM);
Warning: mysql_fetch_array(): supplied argument is not a valid
MySQL result resource in <b>/opt/lampp/htdocs/index.php on line 157
You don't show the code for the object $defeft but I can assume what you're trying to do...
$blah = mysql_query("SELECT 1 FROM information_schema.tables");
if(!$blah) { // check for mysql errors
echo mysql_error();
exit;
}
while( $defeft = mysql_fetch_array($blah)) {
echo $defeft['row'];
}
Defeft holds the rows data in a array, and while loops through each row.
doesn't that error refer to using mysql_fetch_X on a variable that is not a Resource, e.g. the query failed, ensure your query returns a valid result, rather than a mysql error?
$a = mysql_query($sql) or die();
try this (or similar) :)
Related
This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 8 years ago.
I tried to search and found some similar questions but none seemed to help with my code. I upgraded to a new server:
MySQL = 5.0.96-community
PHP = 4.4.9
I get following error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource in /path/to/file/filename.php on line 209
Here's the Line 209:
$num_rows = mysql_num_rows($result);
The code right above it for reference is:
<?
// Connect to DB
$db = mysql_connect("localhost","db_name","password");
if (!$db)
{
echo "No connection.";
exit;
}
mysql_select_db("db_name");
$v = str_replace(' ','_',$v);
$query = "SELECT * FROM reviews";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
You aren't doing basic error checking:
$result = mysql_query($query);
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error
See also mysql_error():
Returns the text of the error message from previous MySQL operation
I get this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
when I changed my code to this
$term = $_POST["term"];
$query = mysql_query("SELECT id FROM planet1 WHERE MATCH (title) AGAINST ('$term')");
while($row = mysql_fetch_array( $query )) {
echo $row['id'],'<br>';
}
This happens when there is an error during the execution of the SQL query :
mysql_query() returns false
and that's not a resource as mysql_fetch_array() expects.
You should try calling mysql_error(), to have some information about the error that's caused by the execution of your MySQL :
$query = mysql_query("SELECT id FROM planet1 WHERE MATCH (title) AGAINST ('$term')");
if (!$query) {
echo mysql_error();
die;
}
Note: of course, this echo+die is OK while developping, but should never be present on a production server.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I'm having a problem with this mysql code. I presume its a basic error in the $sqlx... line but I'm slightly lost.
The code basically prints messages from a db
Here is the code:
$sqls="SELECT username FROM social WHERE `adder`='$username'";
$results=mysql_query($sqls);
$resulti= mysql_num_rows($results);
if ($resulti==0) {
echo "You haven't added anyone yet. Find some suggestions";
}
$row=mysql_fetch_array($results);
$sqlx="SELECT * FROM messages WHERE `sender` IN ($row)";
$resultx= mysql_query($sqlx);
$resultz= mysql_num_rows($resultx);
if ($resultz==0){
echo "No messages at all!!";
}
else {
$finished="false";
$r=0;
While(($rowx=mysql_fetch_assoc($resultx))&&($finished=="false")) {
//echo off messages
$username is got further up the file.
Here is the error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/social/iframe/index.php on line 34
Line 34 is $resultz= mysql_num_rows($resultx);
But like i said the error is probably the line two up from that.
One interesting happens. "No messages at all!!" is echoed out which means the result of the mysql_query is 0. This is why I am convinced it is the line 32, ($sqlx)
Any idea??
Have I done the mysql_fetch_array wrong when getting $row??
thanks
$row=mysql_fetch_array($results);
$sqlx="SELECT * FROM messages WHERE `sender` IN ($row)";
This will create the following query:
SELECT * FROM messages WHERE `sender` IN (Array)
This is obviously not a valid MySQL query. You have to process the array.
$sqlx = "SELECT * FROM `messages` WHERE `sender` IN ("; // start of query
foreach($row as $r)
$sqlx .= "'".$r['username']."',"; // insert all returned usernames
$sqlx = substr($sqlx,0,-1).')'; // substract the last comma and close the query
Or, as RiaD pointed out in the comments:
$sqlx = "SELECT * FROM `messages` WHERE `sender` IN (".
implode(',',array_map(function($x){return "'".$x['username']."'"; }, $row)).
")";
PS: Riad, it should be $x['username'] instead of $x and you forgot the semicolon ;)
mysql_query($sqlx) return false instead of result. It means any error occured. Try to check is $sqlx correct query and check mysql_error() to get what error is occured. To check was here any error or not you can use
if(!$resultx){
print 'error:'.mysql_error();
}
else{
//use result
}
If your query fails mysql_query($sqlx) returns false rather than resource. So, you need to check, that this function returned true (e.g. if (!results) {}) nad print use mysql_error() to see what error was.
if(!$resultx){
print 'error:'.mysql_error();
}
Also, you are embedding $row variable into the query string. But this var is an Array, so you end up with a query like this:
SELECT * FROM messages WHERE `sender` IN (Array)
See mysql_fetch_array manual for details
I wanted to arrange the array of table list with sort() function but i am getting same kind of warning.
<?php
require_once("lib/connection.php");
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
sort($result);
foreach ($result as $result){
echo $result ;
}
?>
and the warning I am getting are:
Warning: sort() expects parameter 1 to be array, resource given in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 10
The warning is pretty clear: mysql_query does not return an array with results from the query, but a resource. You need a function like mysql_fetch_array() to return the data you need (and on which you can perform a sort operation).
See the manual for the use of mysql_query() http://nl3.php.net/mysql_query
And maybe unrelated, but you can sort your results in MySQL right away by adding ORDER BY <fieldname> to your query.
The variable $result is only a resource of the type result. You need to fetch then the data from the result set with e.g. mysql_fetch_assoc().
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row["Tables_in_st_db_1"];
}
sort($array);
foreach ($array as $item) {
echo $item;
}
I'm not providing the most efficient code imaginable, but this should make it clear what's going on and solve your problem:
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$my_array_of_table_names = array();
while ( $row = mysql_fetch_array($result, MYSQL_NUM)) {
$my_array_of_table_names[] = $row[0];
}
sort($my_array_of_table_names);
foreach ($my_array_of_table_names as $table_name){
echo "$table_name\n";
}
Your problem is that you aren't actually getting the data from the query.
mysql_query() doesn't give you a recordset.
What it does is query the database and returns a database resource which you can then use to get the data.
What you need is after calling mysql_query(), you then need to also call mysql_fetch_array() or similar. (there are a range of functions available, but that's probably the best one to use in this case). Then sort() the data from that, not $result.
It clearly says: it expects an array and you pass something else.
If you had checked the type of $result you would have seen that it is not an array, intead a resource.
im not to sure why im getting a Warning: mysql_fetch_array(): when running this query
$query="SHOW SLAVE STATUS;";
$result = mysql_db_query("aid", $query, $con);
well i use my_db_query because im new to php and thats all i really know well here is the full error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/admin/domains/ismfast.com/public_html/V3/internal/karl_tools/slave_email.php on line 23
$query="SHOW SLAVE STATUS;";
$result = mysql_db_query("aid", $query, $con); echo $result;
$r = mysql_fetch_array($result);
if($r['Slave_SQL_Running']=="Yes"){echo $r['Exec_Master_Log_Pos']}