PHP MySQL Insert / update query returns true, but with an error

I have the following update:

$conn->query_cust("update cms_users set last_login_date = now() where id = " . $_SESSION['USER_INFO']->id); 

I am sure that the problem is not in the query_cust function, because I use the same function in several other forms and they work well. Anyway, here is the definition (with two var_dumps for debug-info):

 function query_cust($s='',$rows=false,$organize=true) { var_dump($s); if (!$q=mysql_query($s,$this->con)) return false; if ($rows!==false) $rows = intval($rows); $rez=array(); $count=0; $type = $organize ? MYSQL_NUM : MYSQL_ASSOC; var_dump($q); while (($rows===false || $count<$rows) && $line=mysql_fetch_array($q,$type)) { if ($organize) { foreach ($line as $field_id => $value) { $table = mysql_field_table($q, $field_id); if ($table==='') $table=0; $field = mysql_field_name($q,$field_id); $rez[$count][$table][$field]=$value; } } else { $rez[$count] = $line; } ++$count; } if (!mysql_free_result($q)) return false; return $rez; } 

When executed, I get a warning message:

 string(57) "update cms_users set last_login_date = now() where id = 1" bool(true) Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/XX/XXXXXXX/html/core/mysql.php on line 26 

What bothers me is that it returns bool (true), but with (correct me if I'm wrong) is not a dangerous warning !? When I copy the update request manually, for example, PHPmyAdmin, it works (the user has the right to update / insert the table).

Just a little information for those who have the same problem: locally, the code runs without any problems (even with "error_reporting = E_ALL" turned on), but when executed in a test environment (Godaddy hosting), a warning appears.

PS: I'm not so in PHP (I'm from the JAVA world), so please have mercy by sending your answers and my stupid questions. Thanks.

+4
source share
2 answers

Per documentation , mysql_query returns true if the query does not return a result. The change:

 if (!$q=mysql_query($s,$this->con)) return false; 

in

 if (!is_resource($q=mysql_query($s,$this->con))) return $q; 
+2
source

You are running an update request, not a selection request. Thus, it returns true because the update was successful. But nothing happens with mysql_fetch_array ().

If you want to choose something, you need to provide a selection request :)

+2
source

Source: https://habr.com/ru/post/1386956/


All Articles