gpt4 book ai didi

php - 使用 PHP 的 mysqli 执行准备好的语句的正确和最简单的方法是什么?

转载 作者:行者123 更新时间:2023-11-29 03:48:42 26 4
gpt4 key购买 nike

我已经在 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();

最佳答案

这是封装 mysqli 的半 self 解释类的内容,包括准备好的语句,这非常棘手。它经过了很好的测试 - 我已经使用它一年了,没有任何变化。

它只实现准备好的语句来执行 SQL 命令,因为它们会更改数据,否则通常需要讨厌的编码技巧。如果你想要 SELECTs,它留给读者作为练习 - 它更容易。 :)

<?php

class Db
{
var $_mysqli;
var $_result;
var $_error_msg;

public function __construct($server, $user, $password, $name)
{
$this->_mysqli = new mysqli("p:".$server, $user,
$password, $name);
if($this->_mysqli->connect_errno)
{
$this->_error_msg = $this->_mysqli->connect_error;
}
}

public function __destruct()
{
}

private function sql_select($sql)
{
$this->_mysqli->query("SET NAMES 'utf8'"); // a little help for UTF8 io
$this->_result = $this->_mysqli->query($sql);
}

private function sql_close()
{
$this->_mysqli->close();
}


public function ErrorMessage()
{
return $this->_error_msg;
}

public function SqlRows($sql)
{
$rows = array();
$result = $this->sql_select($sql);
if($this->IsError())
{
return $rows;
}
while($row = $result->fetch_array())
{
$rows[] = $row;
}
$result->free();
return $rows;
}

public function SqlObjects($sql)
{
$objects = array();
$result = $this->sql_select($sql);
while($object = $this->_result->fetch_object())
{
$objects[] = $object;
}
$result->free();
return $objects;
}

public function SqlOneObject($sql)
{

$result = $this->sql_select($sql);
$obj = $result->fetch_object();
$result->free();
return $obj;
}

public function SqlOneRow($sql)
{
$result = $this->sql_select($sql);
if(! is_object($result))
return null;
if($result->num_rows > 0)
$row = $result->fetch_array();
else
$row = null;
$result->free();
return $row;
}

public function SqlOneValue($sql)
{
$result = $this->sql_select($sql);
if(!empty($result))
{
$row = $result->fetch_array();
}
$result->free();
return empty($row) ? null : $row[0] ;
}

// returns number of affected rows
public function SqlExecute($sql)
{
$this->_result = $this->_mysqli->query($sql);
return $this->affected_rows();
}

private function affected_rows()
{
return $this->_mysqli->affected_rows;
}

private function IsError()
{
if(empty($this->_mysqli))
return false;
return !empty($this->_mysqli->error);
}

// arguments are sql and an array of
// argument references (not values).
public function SqlExecutePS($sql, $args)
{
$stmt = $this->_mysqli->prepare($sql);

// make the type-string
$typestr = make_typestring($args);
$params = array($typestr);
$params = array_merge($params, $args);

call_user_func_array(array($stmt, 'bind_param'), $params);
$stmt->execute();

$ret = $this->affected_rows();
$stmt->close();
return $ret;
}

public function SqlExists($sql)
{
$result = $this->SqlOneRow($sql);
return !empty($result[0]);
}


function make_typestring($args)
{
assert(is_array($args));
$ret = "";
foreach($args as $arg)
{
switch(gettype($arg))
{
case "boolean":
case "integer":
$ret .= "i";
break;
case "double":
$ret .= "d";
break;
case "string":
$ret .= "s";
break;
case "array":
case "object":
case "resource":
case "NULL":
default:
// call it a blob and hope
// you know what you're doing.
$ret .= "b";
break;
}
}
return $ret;
}
}

?>

关于php - 使用 PHP 的 mysqli 执行准备好的语句的正确和最简单的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5839418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com