欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

帮我看看,这段数据库连接代码有没有什么问题?

程序员文章站 2022-05-10 10:54:06
...
新手学php,书上一直没有好的例子,网上的也有错误,自己照着php手册写了一个完整点的
高手帮我看一下,有没有哪里写错了...谢谢了
//面向对象
$mysqli = new mysqli("localhost", "user", "pass", "database");  
if(mysqli_connect_errno())) {  //这边判断方式和下面面向过程中的直接判断变量有什么区别?
    printf("Connect failed: %s\n",  mysqli_connect_error());
    exit();
}
$query =  "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?";
if($stmt = $mysqli->prepare($query) {
    $stmt->bind_param("si", $gender, $age);  //这边插入参数是不是写在这里?
    $stmt->execute();
    $stmt->bind_result($name, $address, $phone);
     
    while($stmt->fetch()) {
         printf ("%s (%s,%s)
", $name, $address, $phone); } $stmt->close(); } $mysqli->close(); /面向过程 $mysqli = mysqli_connecti("localhost", "user", "pass", "database"); if(!$mysqli) { printf ("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if ($stmt = mysqli_prepare($query)){ mysqli_stmt_bind_param($stmt, "si", $gender, $age); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $name, $address, $phone); while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s,%s)
", $name, $address, $phone); } mysqli_stmt_close($stmt); } $mysqli_close();

回复内容:

新手学php,书上一直没有好的例子,网上的也有错误,自己照着php手册写了一个完整点的
高手帮我看一下,有没有哪里写错了...谢谢了

//面向对象
$mysqli = new mysqli("localhost", "user", "pass", "database");  
if(mysqli_connect_errno())) {  //这边判断方式和下面面向过程中的直接判断变量有什么区别?
    printf("Connect failed: %s\n",  mysqli_connect_error());
    exit();
}
$query =  "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?";
if($stmt = $mysqli->prepare($query) {
    $stmt->bind_param("si", $gender, $age);  //这边插入参数是不是写在这里?
    $stmt->execute();
    $stmt->bind_result($name, $address, $phone);
     
    while($stmt->fetch()) {
         printf ("%s (%s,%s)
", $name, $address, $phone); } $stmt->close(); } $mysqli->close(); /面向过程 $mysqli = mysqli_connecti("localhost", "user", "pass", "database"); if(!$mysqli) { printf ("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT `name`, `address`, `phone` FROM Users WHERE gender=? AND age=?"; if ($stmt = mysqli_prepare($query)){ mysqli_stmt_bind_param($stmt, "si", $gender, $age); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $name, $address, $phone); while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s,%s)
", $name, $address, $phone); } mysqli_stmt_close($stmt); } $mysqli_close();

代码是写出来跑的,代码写的对不对,得问编译器或者解析器,它们才是真正的老师。

你自己跑一遍呀。。。

这肉眼真不好看,最后一句好像有问题, 得传入参数($mysqli)吧.

prepare+execute后bind_result+循环fetch是传统的做法.
PHP从5.4开始底层默认采用mysqlnd实现mysql,mysqli,pdo_mysql.
这时你可以使用 get_result 和 fetch_all 快速拿到查询结果.
prepare+execute后 $stmt->get_result()->fetch_all(MYSQLI_ASSOC) 即可拿到结果集.
比如:

post.php?id=1024

MySQLi普通查询:
$db = new mysqli('127.0.0.1','user','pass','dbname',3306);
var_export($db->query('SELECT * FROM posts WHERE id='.intval($_GET['id']))->fetch_all());

MySQLi预处理绑定参数查询:
$db = new mysqli('127.0.0.1','user','pass','dbname',3306);
$stmt = $db->prepare('SELECT * FROM posts WHERE id=?'); //预处理
$stmt->bind_param('i', $_GET['id']); //绑定参数(可以防止SQL注入)
$stmt->execute(); //查询
var_export($stmt->get_result()->fetch_all());