Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Location
    Seattle, WA
    Posts
    601

    Unanswered: Prepared Statements

    I have a prepared statement of:
    PHP Code:
    <?php
    $qGetProg 
    "SELECT progID, progName FROM tblProg";
        if (
    $rGetProg $mysqli->query($qGetProg)){
        while(
    $row $rGetProg->fetch_assoc()){
            
    $rGetPID $row['progID'];
            
    $rGetPN $row['progName'];
        echo 
    "\t\t<option value=\"".$rGetPID."\">".$rGetPN."</option>\n";
        }
        
    $rGetProg->close();
    }
    ?>
    I have read and a friend has told me that I should be able to say $rGetProg->execute(); and it should call block again since it is a prepared statement. When I do call that, I get a friendly little fatal error.

    Ideas?
    Ryan
    My Blog

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    You need to set up a prepared statement.
    Code:
    //Create the SQL statement, with ? to reflect 
    //the parameters to be supplied later.  
    $sql = 'INSERT INTO tablename VALUES(?, ?)';
    
    // Allocates and initializes a statement object
    // suitable for mysqli_stmt_prepare().
    $stmt = mysqli_stmt_init($dbHandle);
    
    // Prepare statement, bind parameters (an integer and a string)
    // and execute the statement
    // 'is' tells MySQL you're passing an integer(i) and a string (s)
    
    if (mysqli_stmt_prepare($stmt, $sql)) {
      mysqli_stmt_bind_param($stmt, 'is', $field1, $field2);
      
      // Set parameters and execute 1
      $field1 = 1;
      $field2 = 'first';
      mysqli_stmt_execute($stmt);
      
      // Set parameters and execute 2
      $field1 = 2;
      $field2 = 'second';
      mysqli_stmt_execute($stmt);
    This code exert has been taken from http://www.databasejournal.com/featu...le.php/3599166

    Preparing MySQL statements is a GOOD feature and should be used wherever possible. MySQL optimises such statements and keeps them in cache so it doesn't have to recompile the work path each time you execute it.

  3. #3
    Join Date
    Jun 2004
    Location
    Seattle, WA
    Posts
    601
    Thanks, I read that article already.
    Ryan
    My Blog

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Hopefully you should see what you're not doing then

  5. #5
    Join Date
    Jun 2004
    Location
    Seattle, WA
    Posts
    601
    Now I do ...
    Ryan
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •