我的网站相当广泛,我最近刚切换到PHP5(称我为大器晚成).
我之前的所有MySQL查询都是这样构建的:
"SELECT * FROM tablename WHERE field1 = 'value' && field2 = 'value2'";
Run Code Online (Sandbox Code Playgroud)
这使它非常简单,友好.
我出于明显的安全原因,现在正试图切换到mysqli,并且SELECT * FROM在bind_param需要特定参数时,我很难弄清楚如何实现相同的查询.
这句话是否已成为过去?
如果是,如何处理涉及大量列的查询?我真的需要每次都输入它们吗?
我的表字符集是utf8,它的校对是utf8.now我有这个代码:
$mysqli = new mysqli("localhost", "root", "", "Amoozeshgah");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
}
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
mysql_set_charset('utf8');
if ($stmt = $mysqli->prepare("SELECT About_Title FROM Tbl_About WHERE About_Id=?")) {
$city = 8;
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$result = $stmt->get_result();
/* fetch value */
while ($myrow = $result->fetch_assoc()) {
// …Run Code Online (Sandbox Code Playgroud) 无法弄清楚,是什么导致错误参数3到mysqli_stmt :: bind_param()应该是一个参考,值在...中给出
PDO
$query = "INSERT INTO test (id,row1,row2,row3) VALUES (?,?,?,?)";
$params = array(1,"2","3","4");
$param_type = "isss";
$sql_stmt = mysqli_prepare ($mysqli, $query);
call_user_func_array('mysqli_stmt_bind_param', array_merge(array($sql_stmt, $param_type), $params));
mysqli_stmt_execute($sql_stmt);
Run Code Online (Sandbox Code Playgroud)
还尝试了OOP
OOP
$insert_stmt = $mysqli->prepare($query);
array_unshift($params, $param_type);
call_user_func_array(array($insert_stmt, 'bind_param'), $params);
$insert_stmt->execute();
Run Code Online (Sandbox Code Playgroud)
但同样的错误,只是现在参数2导致问题.
那么,$ params有什么问题?我需要$ params作为一个值数组.
我有一个mysql查询,但我不能绑定它的参数
SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email =? OR users.handle =? OR userprofile.mobile=?
Run Code Online (Sandbox Code Playgroud)
我试过下面的线
$query = "SELECT users.email,users.handle,userprofile.mobile FROM users,userprofile WHERE users.email =? OR users.handle =? OR userprofile.mobile=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("sss",$email,$username,$mobile);
if ($stmt->execute()) {
if($stmt->num_rows){
echo '......';
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到并错误:
警告:mysqli_stmt :: bind_param():类型定义字符串中的元素数与绑定变量数不匹配
我有一段这样的代码:
$conn = new mysqli($host, $username, $passwd, $dbname);
...
$stmt = $conn->prepare('SELECT ...');
$stmt->bind_param(...);
$stmt->execute();
$stmt->bind_result(...);
while($stmt->fetch())
{
// do something here
}
$stmt->close();
...
// do something more here that has absolutely nothing to do with $stmt
Run Code Online (Sandbox Code Playgroud)
这完全没问题.我得到了我期望的结果,没有错误或任何不应该发生的事情.
但是,如果我设置一个断点(Xdebug 2.2.5/2.2.6/2.2.8/2.3.2和PHP 5.5.3/5.5.15/5.6.0/5.6.6/5.6.10)之后 的一行$stmt->close();,我得到很多警告
不允许财产访问
要么
无法获取mysqli_stmt
我以为我错过了关闭另一个mysqli声明,但我得到了所有结果.在我的代码中似乎没有问题......
有没有办法摆脱这个错误的警告?
更新:此问题仍存在于PHP 7.0.1/Xdebug 2.4.0 RC3中.
PHP 手册mysqli_connect()建议检查返回值并在屏幕上显示错误消息。
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Run Code Online (Sandbox Code Playgroud)
同样,对于 OOP 样式的构造函数,建议这样做:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
Run Code Online (Sandbox Code Playgroud)
Stack Overflow 上的一些用户甚至使用了mysqli_error($conn)这样的代码:
$conn = mysqli_connect('localhost', 'a', 'a');
if (!$con) {
die('Could not connect: …Run Code Online (Sandbox Code Playgroud) 我正在努力学习在PHP中使用mysqli的预处理语句,通常,如果我遇到查询问题,我只需将它回显到屏幕上,看看它的第一步看起来是什么样子.
如何用准备好的声明做到这一点?
我希望在替换变量后看到SQL语句.
我正在尝试运行以下内容.
<?php
$db = mysqli_connect("localhost","user","pw") or die("Database error");
mysqli_select_db($db, "database");
$agtid = $_POST['level'];
$sql = sprintf("call agent_hier(%d)", $agtid);
$result = mysqli_query($db, $sql) or exit(mysqli_error($db));
if ($result) {
echo "<table border='1'>
<tr><th>id</th>
<th>name</th>
<th>parent_id</th>
<th>parent_name</th>
<th>level</th>
<th>email</th></tr>";
while ($row = mysqli_fetch_assoc($result))
{
$aid = $row["id"];
$sql2 = "SELECT * FROM members WHERE MEMNO = '$aid'";
$result2 = mysqli_query($db,$sql2) or exit(mysqli_error($db));
while ($newArray = mysqli_fetch_array($result2)) {
$fname = $newArray['FNAME'];
$lname = $newArray['LNAME'];
$mi = $newArray['MI'];
$address = $newArray['ADDRESS'];
$city = $newArray['CITY'];
$state …Run Code Online (Sandbox Code Playgroud)