lockdown
lockdown

Reputation: 1496

mysql divison by zero error

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);
    printf("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!

Upvotes: 1

Views: 690

Answers (3)

DhruvPathak
DhruvPathak

Reputation: 43245

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;
?>

Upvotes: 1

wonk0
wonk0

Reputation: 13972

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 . '%" ' );

Upvotes: 1

mu is too short
mu is too short

Reputation: 434685

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?

Upvotes: 3

Related Questions