Home  >  Article  >  Backend Development  >  Please help me see if there is anything wrong with this database connection code?

Please help me see if there is anything wrong with this database connection code?

WBOY
WBOYOriginal
2016-08-04 09:19:571227browse

I am a novice learning PHP. There are no good examples in books, and there are also mistakes on the Internet. I wrote a complete one according to the PHP manual
Experts can help me take a look and see if there are any mistakes...Thank you

<code>//面向对象
$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)<br />", $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)<br />", $name, $address, $phone); 
    }
    mysqli_stmt_close($stmt);
}
$mysqli_close();</code>

Reply content:

I am a novice learning PHP. There are no good examples in books, and there are also mistakes on the Internet. I wrote a complete one according to the PHP manual
Experts can help me take a look and see if there are any mistakes...Thank you

<code>//面向对象
$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)<br />", $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)<br />", $name, $address, $phone); 
    }
    mysqli_stmt_close($stmt);
}
$mysqli_close();</code>

Code is written and run. Whether the code is written correctly or not, you have to ask the compiler or parser, they are the real teachers.

Run it yourself. . .

This doesn’t look good to the naked eye. There seems to be something wrong with the last sentence. You have to pass in the parameter ($mysqli).

bind_result+loop fetch after prepare+execute is the traditional approach.
Starting from PHP 5.4, the bottom layer uses mysqlnd by default to implement mysql, mysqli, pdo_mysql.
At this time, you can use get_result and fetch_all to quickly get the query results.
After prepare+execute $stmt->get_result()->fetch_all(MYSQLI_ASSOC) You can get the result set.
For example:

<code>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());</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn