mysqli_query和mysql_query的一点区别
/* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); }
/* Create table doesn"t return a resultset */ if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) { printf("Table myCity successfully created. "); }
/* Select queries return a resultset */ if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows. ", $result->num_rows);
/* free result set */ $result->close(); }
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */ if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can"t execute any functions which interact with the server until result set was closed. All calls will return an "out of sync" error */ if (!$mysqli->query("SET @a:="this will not work"")) { printf("Error: %s ", $mysqli->error); } $result->close(); }
$mysqli->close();
?>
2、过程化方式:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
/* Create table doesn"t return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.
");
}
/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.
", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can"t execute any functions which interact with the server until result set was closed. All calls will return an "out of sync" error */
if (!mysqli_query($link, "SET @a:="this will not work"")) {
printf("Error: %s
", mysqli_error($link));
}
mysqli_free_result($result);
}
mysqli_close($link);
?>
- 上一篇: PHP Nginx MySQL 高并发调优 小试
- 下一篇: mysqli_query 的定义和用法