Php 在mysqli fetch循环中无法调用函数
这段代码根本不起作用Php 在mysqli fetch循环中无法调用函数,php,mysqli,Php,Mysqli,这段代码根本不起作用 $queryProduct = $mysqli->prepare("SELECT productID FROM products WHERE productCat=?"); $queryProduct->bind_param('i', $id); $queryProduct->execute(); $queryProduct->bind_result($productID); while ($queryProduct->fetch()) {
$queryProduct = $mysqli->prepare("SELECT productID FROM products WHERE productCat=?");
$queryProduct->bind_param('i', $id);
$queryProduct->execute();
$queryProduct->bind_result($productID);
while ($queryProduct->fetch())
{
smallBlockProduct($productID);
}
$queryProduct->close();
这在调用smallBlockProduct的行中给出了错误“致命错误:对中的非对象调用成员函数fetch_assoc()”
但是,如果我将函数smallBlockProduct更改为简单的echo$productID;它将遍历所有正确的结果
我不能在mysqli fetch循环中调用函数吗?还是有一种特殊的方法来调用函数
编辑:
错误实际上发生在我调用的函数上。该函数执行另一个查询
function smallBlockProduct($productID)
{
global $mysqli;
$query = "SELECT productName FROM products WHERE productID='$productID'";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc())
{
$productName = $row['productName'];
echo $productName.'<br>';
}
}
函数smallBlockProduct($productID)
{
全球$mysqli;
$query=“从productID=“$productID”的产品中选择productName”;
$result=$mysqli->query($query);
而($row=$result->fetch_assoc())
{
$productName=$row['productName'];
echo$productName。“
”;
}
}
那么现在,我是否可以在已经在另一个查询中时执行另一个查询?看起来您正试图调用传递给
smallBlockProduct()
函数的$productID
上的->fetch_assoc()
我建议检查函数中的$productID
function smallBlockProduct($productID){
var_dump($productID);
...
看起来您正试图在传递给
smallBlockProduct()
函数的$productID
上调用->fetch_assoc()
我建议检查函数中的$productID
function smallBlockProduct($productID){
var_dump($productID);
...
对此的回应是:那么现在,我是否可以在另一个查询中执行另一个查询?
不能使用同一数据库连接执行第二个查询,而不丢失第一个查询。要运行第二个查询,您需要打开另一个连接。对此的响应是:那么,现在,我是否可以在已在另一个查询中执行其他查询?
不能使用同一数据库连接执行第二个查询,而不丢失第一个查询。要运行第二个查询,您需要打开另一个连接。参数“i”不存在。你需要把号码传给param。如下所示:
$queryProduct->bind_参数(1,$id)代码>参数“i”不存在。你需要把号码传给param。如下所示:$queryProduct->bind_参数(1,$id)代码>