Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP';编写语句的正确且最简单的方法是什么;那是mysqli吗?_Php_Mysql_Mysqli_Prepared Statement - Fatal编程技术网

使用PHP';编写语句的正确且最简单的方法是什么;那是mysqli吗?

使用PHP';编写语句的正确且最简单的方法是什么;那是mysqli吗?,php,mysql,mysqli,prepared-statement,Php,Mysql,Mysqli,Prepared Statement,我已经在PHP中使用老的mysql api很长时间了,我想在我正在进行的一个新项目中开始使用mysqli来提高速度和安全性。我已经阅读了手册并阅读了一些教程,但是我发现了很多关于如何在mysql中编写语句的相互矛盾且有些混乱的信息 这段代码中是否有不需要的内容,是否有遗漏的内容?另外,这是做这样简单的事情的最简单的方法吗(对于这样一个简单的任务来说似乎有些复杂) 程序性: // build prepared statement $query = mysqli_prepare($link, "SE

我已经在PHP中使用老的mysql api很长时间了,我想在我正在进行的一个新项目中开始使用mysqli来提高速度和安全性。我已经阅读了手册并阅读了一些教程,但是我发现了很多关于如何在mysql中编写语句的相互矛盾且有些混乱的信息

这段代码中是否有不需要的内容,是否有遗漏的内容?另外,这是做这样简单的事情的最简单的方法吗(对于这样一个简单的任务来说似乎有些复杂)

程序性:

// build prepared statement
$query = mysqli_prepare($link, "SELECT email FROM users WHERE id = ?");

// bind parameters to statement
mysqli_stmt_bind_param($query, 's', $_GET['id']);

// execute statement
mysqli_stmt_execute($query);

// bind the variables to the result
mysqli_stmt_bind_result($query, $email);

// print the results
while (mysqli_stmt_fetch($query)) {
    echo $email;
}

// close the statement
mysqli_stmt_close($query);

// close connection
mysqli_close($link);
// build prepared statement
$query = $link->prepare("SELECT email FROM users WHERE id = ?");

// bind parameters to statement
$query->bind_param('s', $_GET['id']);

// execute statement
$query->execute();

// bind the variables to the result
$query->bind_result($email);

// print the results
while ($query->fetch()) {
    echo $email;
}

// close the statement
$query->close();

// close connection
$link->close();
面向对象:

// build prepared statement
$query = mysqli_prepare($link, "SELECT email FROM users WHERE id = ?");

// bind parameters to statement
mysqli_stmt_bind_param($query, 's', $_GET['id']);

// execute statement
mysqli_stmt_execute($query);

// bind the variables to the result
mysqli_stmt_bind_result($query, $email);

// print the results
while (mysqli_stmt_fetch($query)) {
    echo $email;
}

// close the statement
mysqli_stmt_close($query);

// close connection
mysqli_close($link);
// build prepared statement
$query = $link->prepare("SELECT email FROM users WHERE id = ?");

// bind parameters to statement
$query->bind_param('s', $_GET['id']);

// execute statement
$query->execute();

// bind the variables to the result
$query->bind_result($email);

// print the results
while ($query->fetch()) {
    echo $email;
}

// close the statement
$query->close();

// close connection
$link->close();

下面是一个封装mysqli的半自解释类的精髓,包括准备好的语句,这相当棘手。它经过了很好的测试——我已经用了一年了,没有任何变化

它只实现准备好的语句来执行SQL命令,因为它们会更改数据,并且通常需要糟糕的编码技巧。如果您想要选择,它将留给读者作为练习-这样更容易。:)


下面是一个半自解释类的精髓,它封装了mysqli,包括准备好的语句,这非常棘手。它经过了很好的测试——我已经用了一年了,没有任何变化

它只实现准备好的语句来执行SQL命令,因为它们会更改数据,并且通常需要糟糕的编码技巧。如果您想要选择,它将留给读者作为练习-这样更容易。:)


我的建议是跳过MySQLi直接转到PDO。
PDOStatement
类有一个非常清晰和一致的界面,您应该跳过mysqli,直接转到。你为什么坚持使用程序代码@Phil,谢谢,我来看看PDO。@teresko,我一直都在做程序性的工作,所以我不知道在这个项目上跳入OO是否是一个好主意(在时间紧迫的情况下)。哦,刚刚注意到PDO只是OO,所以也许我应该继续尝试。不过,从学习的角度来看,我仍然对我的原始问题感兴趣。我的建议是跳过MySQLi,直接转到PDO。
PDOStatement
类有一个非常清晰和一致的界面,您应该跳过mysqli,直接转到。你为什么坚持使用程序代码@Phil,谢谢,我来看看PDO。@teresko,我一直都在做程序性的工作,所以我不知道在这个项目上跳入OO是否是一个好主意(在时间紧迫的情况下)。哦,刚刚注意到PDO只是OO,所以也许我应该继续尝试。不过,从学习的角度来看,我仍然对我原来的问题感兴趣。