Php $num_rows 安装 Mysqlnd 后不工作
Php $num_rows not working after installing Mysqlnd
我正在努力让我的 $num_rows
正常工作。它一直在工作,现在不行了。阅读此站点时,有人建议我在我的服务器上安装 MySqlnd
,因为 fetch_array
和 get_result
之类的东西无法正常工作,现在我希望我从未安装过它。 (我应该卸载它吗?)
我一直在尝试很多变体来尝试让 $num_rows
正常工作 - 以及其他功能 - 我已经不知道什么是对什么是错了。你能帮我吗 ?我得到的错误是:
Fatal error: Call to undefined method mysqli::stmt() in /var/www/html/checkcontact.php on line 34
第 34 行是:$result = $con->stmt($query);
这是我的代码:
<?php
require('dbConnect.php');
//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
//decode the JSON
$array = json_decode($json);
//bind
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param('s', $phonenumber);
//for each value of phone_number in the array, call it $phonenumber
foreach ($array as $value)
{
$phonenumber = $value->phone_number;
$stmt->execute();
$result = $con->stmt($query);
//$result = $stmt->get_result();
//$stmt->store_result();
//$num_rows = $stmt->num_rows();
//$result = mysqli_query($con, $query);
if ($result->$num_rows > 0) {
// while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
// while($row = mysqli_fetch_assoc($result)){
// $contact_id = $row['user_id'];
// echo $contact_id;
echo "number of phone numbers in the user table is " . $num_rows . "<br>";
}
}
?>
编辑:
根据要求,这是我的 dbConnect.php 文件:
<?php
define ('HOST', 'localhost');
define ('USER', 'bob');
define ('PASS', 'password');
define ('DB', 'mydb');
$con = NEW MySQLi("localhost", "bob", "password", "mydb");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
你的第一个错误是这一行
$result = $con->stmt($query);
您可能想要 get_result()
。
没有mysqli::stmt()
方法。然后,没有 $result->$num_rows
,但它是结果的 属性,这意味着它应该是 $result->num_rows
(松开 $
)。
修改了这些更改,这很可能是您的代码的样子。
//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
//decode the JSON
$array = json_decode($json);
//bind
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die($con->error);
$stmt->bind_param('s', $phonenumber) or die ("MySQLi-stmt binding failed ".$stmt->error);
//for each value of phone_number in the array, call it $phonenumber
foreach ($array as $value) {
$phonenumber = $value->phone_number;
$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
if ($stmt->num_rows > 0) { // Check if there are any rows matching this value
$result = $stmt->get_result(); // Convert from MySQLi_stmt to MySQLi_result (to use fetch_assoc())
echo "Number of rows matching username '".$value->phone_number."' from user-table is " . $result->num_rows . " rows.<br>";
while ($row = $result->fetch_assoc()) {
echo $row['user_id']."<br />";
}
} else {
echo "No rows matching username ".$value->phone_number.".<br />";
}
}
$stmt->close();
请注意,mysqli_stmt::get_result()
方法需要 mysqlnd 驱动程序。如果您没有该驱动程序,则必须使用 mysqli_stmt::bind_result()
和 mysqli_stmt::fetch()
以不同方式获取结果(除此之外,安装该驱动程序 - 如果安装正确 - 不会影响您的代码) .
我正在努力让我的 $num_rows
正常工作。它一直在工作,现在不行了。阅读此站点时,有人建议我在我的服务器上安装 MySqlnd
,因为 fetch_array
和 get_result
之类的东西无法正常工作,现在我希望我从未安装过它。 (我应该卸载它吗?)
我一直在尝试很多变体来尝试让 $num_rows
正常工作 - 以及其他功能 - 我已经不知道什么是对什么是错了。你能帮我吗 ?我得到的错误是:
Fatal error: Call to undefined method mysqli::stmt() in /var/www/html/checkcontact.php on line 34
第 34 行是:$result = $con->stmt($query);
这是我的代码:
<?php
require('dbConnect.php');
//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
//decode the JSON
$array = json_decode($json);
//bind
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param('s', $phonenumber);
//for each value of phone_number in the array, call it $phonenumber
foreach ($array as $value)
{
$phonenumber = $value->phone_number;
$stmt->execute();
$result = $con->stmt($query);
//$result = $stmt->get_result();
//$stmt->store_result();
//$num_rows = $stmt->num_rows();
//$result = mysqli_query($con, $query);
if ($result->$num_rows > 0) {
// while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
// while($row = mysqli_fetch_assoc($result)){
// $contact_id = $row['user_id'];
// echo $contact_id;
echo "number of phone numbers in the user table is " . $num_rows . "<br>";
}
}
?>
编辑: 根据要求,这是我的 dbConnect.php 文件:
<?php
define ('HOST', 'localhost');
define ('USER', 'bob');
define ('PASS', 'password');
define ('DB', 'mydb');
$con = NEW MySQLi("localhost", "bob", "password", "mydb");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
你的第一个错误是这一行
$result = $con->stmt($query);
您可能想要 get_result()
。
没有mysqli::stmt()
方法。然后,没有 $result->$num_rows
,但它是结果的 属性,这意味着它应该是 $result->num_rows
(松开 $
)。
修改了这些更改,这很可能是您的代码的样子。
//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
//decode the JSON
$array = json_decode($json);
//bind
$query = "SELECT * FROM user WHERE username = ?";
$stmt = $con->prepare($query) or die($con->error);
$stmt->bind_param('s', $phonenumber) or die ("MySQLi-stmt binding failed ".$stmt->error);
//for each value of phone_number in the array, call it $phonenumber
foreach ($array as $value) {
$phonenumber = $value->phone_number;
$stmt->execute() or die ("MySQLi-stmt execute failed ".$stmt->error);
if ($stmt->num_rows > 0) { // Check if there are any rows matching this value
$result = $stmt->get_result(); // Convert from MySQLi_stmt to MySQLi_result (to use fetch_assoc())
echo "Number of rows matching username '".$value->phone_number."' from user-table is " . $result->num_rows . " rows.<br>";
while ($row = $result->fetch_assoc()) {
echo $row['user_id']."<br />";
}
} else {
echo "No rows matching username ".$value->phone_number.".<br />";
}
}
$stmt->close();
请注意,mysqli_stmt::get_result()
方法需要 mysqlnd 驱动程序。如果您没有该驱动程序,则必须使用 mysqli_stmt::bind_result()
和 mysqli_stmt::fetch()
以不同方式获取结果(除此之外,安装该驱动程序 - 如果安装正确 - 不会影响您的代码) .