Re: A prepared statements question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 12 Jul 2009 15:25:15 -0400 (EDT), "Jason Carson" wrote:

> For anyone reading this thread, here is the final code that I used...
> 
> $link = mysqli_connect($hostname, $username, $password, $database);
> $stmt = mysqli_prepare($link, "SELECT * FROM administrators WHERE
> adminusers=?);
> mysqli_stmt_bind_param($stmt, "s", $adminuser);
> mysqli_stmt_execute($stmt);
> mysqli_stmt_store_result($stmt);
> $count = mysqli_stmt_num_rows($stmt);
> 
> if($count==1){
> header("location:admin.php");
> } else {
> echo "Failure";
> }

You should always check for errors, so...

  /* without actually testing or checking against the manual */

  $q = "SELECT * FROM administrators WHERE adminusers=?";

  if (   $link = mysqli_connect($hostname, $username, $password, $database)
      && $stmt = mysqli_prepare($link, $q)
      &&         mysqli_stmt_bind_param($stmt, "s", $adminuser)
      &&         mysqli_stmt_execute($stmt)
      &&         mysqli_stmt_store_result($stmt))
  {
    $count = mysqli_stmt_num_rows($stmt);
  } else {
    /* Of course, at this point it would be nice to know which
       function failed. I don't think there is a neat way to
       find that out, and checking every function for errors
       would make the code look much much worse than using the
       old mysql[i]_query functions. Bleah. */
  }


/Nisse

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux