[PHP] Syntax problem PDO and bindvalue

2011-12-12 Thread Stephen

So I am getting this SQL error:

Error selecting photographs: SQLSTATE[42000]: Syntax error or access 
violation: 1064 You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the right syntax to 
use near ''4'' at line 2


My code is:

function updatephotos($dbh, $x) {

echo $x['number'] . br /;  this echo is 4

  $sql = SELECT * FROM photographs WHERE
  photo_filename LIKE '%2%' LIMIT 0, :q;;

  $stmt = $dbh-prepare($sql);

  try {

$stmt-bindValue( ':q', $x['number'], PDO::PARAM_INT );
$stmt-execute();

  } catch (PDOException $e) {
return 'Error selecting photographs: ' . $e-getMessage();
}

while ( list( $id, $name, $alt, $caption) = $stmt-fetch(PDO::FETCH_NUM)) {
echo $name . br /;
}


  return test worked ;
}

If I hard code the SQL as:

$sql = SELECT * FROM photographs WHERE
  photo_filename LIKE '%2%' LIMIT 0, 4;

all works well.

Can anyone see what is wrong?

How can I see the prepared SQL statement before it is executed?

Thanks
Stephen

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Syntax problem PDO and bindvalue

2011-12-12 Thread FeIn
I don't think you're suppose to end your queries with a semicolon. Try:

$sql = SELECT * FROM photographs WHERE
 photo_filename LIKE '%2%' LIMIT 0, :q;


On Mon, Dec 12, 2011 at 1:49 PM, Stephen stephe...@rogers.com wrote:

 So I am getting this SQL error:

 Error selecting photographs: SQLSTATE[42000]: Syntax error or access
 violation: 1064 You have an error in your SQL syntax; check the manual that
 corresponds to your MySQL server version for the right syntax to use near
 ''4'' at line 2

 My code is:

 function updatephotos($dbh, $x) {

 echo $x['number'] . br /;  this echo is 4

  $sql = SELECT * FROM photographs WHERE
  photo_filename LIKE '%2%' LIMIT 0, :q;;

  $stmt = $dbh-prepare($sql);

  try {

$stmt-bindValue( ':q', $x['number'], PDO::PARAM_INT );
$stmt-execute();

  } catch (PDOException $e) {
return 'Error selecting photographs: ' . $e-getMessage();
}

 while ( list( $id, $name, $alt, $caption) = $stmt-fetch(PDO::FETCH_NUM)) {
 echo $name . br /;
}


  return test worked ;
 }

 If I hard code the SQL as:

 $sql = SELECT * FROM photographs WHERE
  photo_filename LIKE '%2%' LIMIT 0, 4;

 all works well.

 Can anyone see what is wrong?

 How can I see the prepared SQL statement before it is executed?

 Thanks
 Stephen

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Syntax problem PDO and bindvalue

2011-12-12 Thread Fatih P.
On Mon, Dec 12, 2011 at 4:22 PM, FeIn aci...@gmail.com wrote:

 I don't think you're suppose to end your queries with a semicolon. Try:


you can end your queries with semicolon in prepared statements.

How can I see the prepared SQL statement before it is executed?
try-  var_dump ($statement);