开发者

mysql divison by zero error

开发者 https://www.devze.com 2023-03-26 07:57 出处:网络
I am trying to use PHP to update column data but I am receiving a division by zero error. mysql_query(\'UPDATE personas SET toolbar_id=\\\'\' . $result . \'\\\' WHERE download_ff LIKE concat(\'%\',\\

I am trying to use PHP to update column data but I am receiving a division by zero error.

mysql_query('UPDATE personas SET toolbar_id=\'' . $result . '\' WHERE download_ff LIKE concat('%',\'' . $result . '\','%')');
    echo mysql_error($link);
    p开发者_如何学Crintf("Records inserted: %d\n", mysql_affected_rows());

Ive tried a few ways to concat the $result but it is all resulting in a division by zero error. Help!


Your problem is right here:

concat('%',\'' . $result . '\','%')

The % ends out outside the string so PHP is interpreting it as a modulus operation and the strings on both sides of the modulus will be zeros in a numeric context, the result is that you're trying to 0 % 0 and you get your "division by zero" error.

Try escaping the inner single quotes:

concat(\'%\',\'' . $result . '\',\'%\')

Or, create a $like_result in your PHP that includes the percent signs in the string and then do this:

mysql_query('UPDATE personas SET toolbar_id=\'' . $result . '\' WHERE download_ff LIKE \'' . $like_result . '\';

You are using mysql_real_escape_string too, right?


How about using single quotes for PHP and double quotes for SQL, that way you don't have to mess around with backslashes to escape quotes; using some whitespace also helps to make query better to read:

mysql_query( 
 'UPDATE personas SET 
   toolbar_id="' . $result . '" 
  WHERE 
   download_ff LIKE "%' . $result . '%" ' );


Did not debug about the divistion by zero error. But your problem seems to be improper string formatting and concatenation, PHP might be treating % as modulus operation with empty string ( i.e. 0 );

<?php

$a = 10;
$b = a%'';

?>   /* would give : Warning: Division by zero on line 4 */

Try the second version of the query, it would work : http://codepad.org/6erRjYa7

<?php

$result = "foo";    
$query = $query = 'UPDATE personas SET toolbar_id=\'' . $result . '\' WHERE download_ff LIKE concat('%',\'' . $result . '\','%')';    
echo $query;

$queryNew =  "UPDATE personas SET toolbar_id='".$result."' WHERE download_ff LIKE concat('%','".$result."','%')";    
echo PHP_EOL ;
echo $queryNew;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消