Page 1 of 1

MySQLi using limit and bind param problem?

Posted: Mon Nov 24, 2008 3:03 pm
by chrisjroyce

Code: Select all

 
<?php
   $link = mysqli_connect("hostName", "user", "pass", "db");
 
   /* check connection */
   if (mysqli_connect_errno()) 
   {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }
 
   $limit = 1;
   
   /* create a prepared statement */
   $stmt = $link->prepare('SELECT `id` FROM `proofImages` WHERE `clientId` = 1 LIMIT ?');
   
   $stmt->bind_param('i', $limit);
 
    /* execute query */
    $stmt->execute();
 
    /* bind result variables */
    $stmt->bind_result($col1);
 
    /* fetch values */
    while ($stmt->fetch()) 
   {
        printf("%s \n", $col1);
    }
 
    /* close statement */
    mysqli_stmt_close($stmt);
 
/* close connection */
mysqli_close($link);
?>
 
Whenever I try to bind a value to my query for the limit the query fails. I have no idea why, and if I remove the Limit everything works.

Any ideas? It's been driving me crazy for days!

Re: MySQLi using limit and bind param problem?

Posted: Wed Nov 26, 2008 2:16 am
by novice4eva

Code: Select all

$stmt->bind_param(1, $limit); /* INSTEAD OF $stmt->bind_param('i', $limit);*/

Re: MySQLi using limit and bind param problem?

Posted: Wed Nov 26, 2008 6:17 am
by chrisjroyce
Still doesn't work sadly.

Isn't the 'i' required so that it knows that I'm passing an integer?

This is the error I get
Fatal error: Call to a member function bind_param() on a non-object in /var/www/vhosts/domain.co.uk/subdomains/projects/httpdocs/imageViewer/limit.php on line 16
So it looks like it's failing on the statement creation?

I am thinking that perhaps you cannot do limits with params?

Re: MySQLi using limit and bind param problem?

Posted: Wed Nov 26, 2008 10:02 pm
by novice4eva
I am terribly sorry, you are right about it. But i went though the manual and adhering to it, well it should be something like this:

Code: Select all

 
$stmt = mysqli_prepare($link, "SELECT `id` FROM `proofImages` WHERE `clientId` = 1 LIMIT ?");
mysqli_stmt_bind_param($stmt, 's', $limit);
 
What confused me was the two styles of connection namely:Procedural style and OO style, and the ways the prepare(function) were used are different!!!! Anyways this is interesting, tell me if you get it right, check this php.net resource
http://us2.php.net/mysqli_stmt_bind_param