Created
December 11, 2012 20:13
-
-
Save n5i/4261747 to your computer and use it in GitHub Desktop.
Mysqli bind param
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world'); | |
/* check connection */ | |
if (mysqli_connect_errno()) { | |
printf("Connect failed: %s\n", mysqli_connect_error()); | |
exit(); | |
} | |
$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); | |
$stmt->bind_param('sssd', $code, $language, $official, $percent); | |
$code = 'DEU'; | |
$language = 'Bavarian'; | |
$official = "F"; | |
$percent = 11.2; | |
/* execute prepared statement */ | |
$stmt->execute(); | |
printf("%d Row inserted.\n", $stmt->affected_rows); | |
/* close statement and connection */ | |
$stmt->close(); | |
/* Clean up table CountryLanguage */ | |
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'"); | |
printf("%d Row deleted.\n", $mysqli->affected_rows); | |
/* close connection */ | |
$mysqli->close(); | |
// example 2 | |
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world'); | |
/* check connection */ | |
if (!$link) { | |
printf("Connect failed: %s\n", mysqli_connect_error()); | |
exit(); | |
} | |
$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); | |
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent); | |
$code = 'DEU'; | |
$language = 'Bavarian'; | |
$official = "F"; | |
$percent = 11.2; | |
/* execute prepared statement */ | |
mysqi_stmt_execute($stmt); | |
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt)); | |
/* close statement and connection */ | |
mysqli_stmt_close($stmt); | |
/* Clean up table CountryLanguage */ | |
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'"); | |
printf("%d Row deleted.\n", mysqli_affected_rows($link)); | |
/* close connection */ | |
mysqli_close($link); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment