|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-09-08 19:33 UTC] tony2001@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 07:00:01 2025 UTC |
Description: ------------ The code is supposed to able insert two values into a table, but it is not doing so. There is something wrong between prepared stmt and mysql_stmt_bind_param. Reproduce code: --------------- <?php // Make the connection. $dbc = @mysqli_connect ('localhost', 'root', 'platypus', 'test') OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); // Make the query. $query = "INSERT INTO accounts (name, balance) VALUES (?, ?)"; // Prepare the statement. $stmt = mysqli_prepare($dbc, $query); // Bind the variables. mysqli_stmt_bind_param($stmt, 'sd', $name, $balance); // Create an array of values to be inserted. $data = array( array('Italo Calvino', 65465.99), array('Vladimir Nabokov', 132.74), array('James Joyce', 432.74), array('William Faulkner', 841664.67), array('F. Scott Fitzgerald', 69.23), array('Zora Neale Hurston', 130654.44), array('Franz Kafka', 87.63), array('William Carlos Williams', 9.98), array('Jane Austen', 1324.02), array('George Eliot', 49683.56) ); // Print a caption. echo "<p>The query being prepared is: $query</p>\n"; // Loop through the array, inserting each record. foreach ($data as $record) { // Assign the variables. $name = $record[0]; $balance = $record[1]; // Execute the query. mysqli_stmt_execute($stmt); // Print the results. echo "<p>Name: $name<br />Balance: $balance<br />Result: "; // Print a message based upon the result. if (mysqli_stmt_affected_rows($stmt) == 1) { echo 'OK'; } else { echo 'FAILED ' . mysqli_stmt_error($stmt); } echo '</p>'; } // End of foreach loop. Expected result: ---------------- Name: Italo Calvino Balance: 65465.99 Result: OK Actual result: -------------- Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: Number of variables doesn't match number of parameters in prepared statement in c:\program files\apache group\Apache\htdocs\Ch11\prepared.php on line 21 The query being prepared is: INSERT INTO accounts (name, balance) VALUES (?, ?) Name: Italo Calvino Balance: 65465.99 Result: FAILED No data supplied for parameters in prepared statement