Php mysqli\u stmt\u bind\u result()的参数计数错误
我的托管公司不提供MySQLnd,所以我不得不更改代码中的一些内容 这是我的密码:Php mysqli\u stmt\u bind\u result()的参数计数错误,php,mysqli,Php,Mysqli,我的托管公司不提供MySQLnd,所以我不得不更改代码中的一些内容 这是我的密码: <?php include_once 'db.php'; $sql = "SELECT * FROM table1 ORDER BY id DESC"; $stmt = mysqli_stmt_init($connect); if (!mysqli_stmt_prepare($stmt, $sql)) { echo "SQL statement failed"
<?php
include_once 'db.php';
$sql = "SELECT * FROM table1 ORDER BY id DESC";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed";
} else{
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
...
及
我已经在这方面工作了两天,我无法解决它,因为我是一个初学者。我该怎么办?请容忍我的这一点,因为我已经很久没有使用程序性mysqli了 如果您希望动态绑定列参数并执行获取循环(因为mysqlnd不是一个选项),那么这里的代码实现了这一点
mysqli_stmt_execute($stmt);
// now before you loop on $stmt->fetch, you must bind all the columns that exist
// to a row which will hold the values during the looping on fetch (yes, confusing)
$row = array(); // will hold each fetch'd loop result
$params = array(); // something to pass keyed references to $row with
$params[] = $stmt; // add the $stmt object as first param (for procedural way)
$meta = mysqli_stmt_result_metadata($stmt);// get what those columns will be
while($field = $meta->fetch_field()) {
if (!isset($row[ $field->name ])) { $row[ $field->name ] = null; } // set if not set
$params[] = &$row[ $field->name ]; // add reference to keyed row value
}
$meta->close();// metadata no longer needed, close it
call_user_func_array('mysqli_stmt_bind_result', $params);
while (mysqli_stmt_fetch($stmt)) {
// in here you then have $row to use as before
echo $row['id'];
}
现在,你可能认为我在这里做得太过分了。。。对该示例用于处理未知的sql列抓取(使用SELECT*
语法)。但是,如果您知道所有列都会出现,您可以像下面这样简单地在while循环之前绑定每个列:
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $col2, $col3);
while (mysqli_stmt_fetch($stmt)) {
// in here you then use $id, $col2, $col3
echo $id;
}
这就是为什么切换到
PDO
库可以让事情变得非常简单,因为它提供了在一个简单的循环中简单地获取具有关联键名的行的功能,就像您所知道并喜欢的mysqlnd
一样。然而,如果PDO
也不是一个选项,那么你就只能用痛苦而神秘的方式来处理“绑定”和“mysqli”。检查mysqli\u stmt\u bind\u result()
会期望什么-它应该有一个你想要检索的列的列表。如果你不知道会出现哪些列(使用*
),在调用用户函数数组
中执行绑定结果
之前,您需要动态创建一个空数组,并使用循环中结果元数据
和获取字段
中的键名将值绑定到该数组。或者,您可以将其切换到PDO;)简单多了。@难以置信抱歉,作为一个初学者,我无法理解一件对我来说太复杂的事情\@NigelRen if(empty($name)| | empty($desc)){echo“empty name或description.”else{$sql=“SELECT*FROM table1;”“;$stmt=mysqli_stmt_init($connect);if(!mysqli_stmt_prepare($stmt,$sql)){echo”sql语句失败!}else{mysqli_stmt execute($stmt)$result=mysqli\u stmt\u get\u result($stmt);$rowCount=mysqli\u num\u rows($result);$incase=$rowCount+1;你是这个意思吗?
mysqli_stmt_execute($stmt);
// now before you loop on $stmt->fetch, you must bind all the columns that exist
// to a row which will hold the values during the looping on fetch (yes, confusing)
$row = array(); // will hold each fetch'd loop result
$params = array(); // something to pass keyed references to $row with
$params[] = $stmt; // add the $stmt object as first param (for procedural way)
$meta = mysqli_stmt_result_metadata($stmt);// get what those columns will be
while($field = $meta->fetch_field()) {
if (!isset($row[ $field->name ])) { $row[ $field->name ] = null; } // set if not set
$params[] = &$row[ $field->name ]; // add reference to keyed row value
}
$meta->close();// metadata no longer needed, close it
call_user_func_array('mysqli_stmt_bind_result', $params);
while (mysqli_stmt_fetch($stmt)) {
// in here you then have $row to use as before
echo $row['id'];
}
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $col2, $col3);
while (mysqli_stmt_fetch($stmt)) {
// in here you then use $id, $col2, $col3
echo $id;
}