这是我第一次使用mysqli。它似乎是在mysqli_num_rows()的圆括号之间查找结果集的名称。然而,当我尝试$stmt、$conn,但什么也没有得到时,我得到了同样的错误。令人沮丧!下面最后一行中$WHAT的位置是什么?
也许我尝试的策略是错误的。我所要做的就是检查是否返回了结果。我真的不需要行数。我应该只做一个带有错误消息的else语句吗?这是最好的方法吗?有没有一种好的方法来编写一个函数来连接并接受查询及其参数?我为mysql写了一个,但是这个太不一样了!我并不期待重写几十个查询!
$conn = mysqli_connect($host, $user, $pwd, $db,$port=$port_nbr);
if ($mysqli_connect_errno) {
printf("Connect failed: %s\n",
mysqli_connect_error());
exit;
}
if($stmt=$conn->prepare("SELECT id, name, status, type FROM organization")) {
$stmt->execute();
$stmt->bind_result($org_id, $orgname, $orgstatus, $orgtype);
$num=mysqli_num_rows($WHAT);
}发布于 2011-04-23 11:46:27
当你只想要面向对象时,你可以将过程化和面向对象的方法结合起来。变化
$num=mysqli_num_rows($WHAT);至
$num = $stmt->num_rows();发布于 2011-04-23 11:45:57
mysqli_num_rows将查询结果作为参数http://us.php.net/manual/en/mysqli-result.num-rows.php
您也可以在OOP样式中使用它,如$result->num_rows;
发布于 2019-08-02 14:36:37
出现此错误的原因是您可能正在向mysqli_num_rows()传递$stmt对象。
此外,我所看到的是,您在php中混淆了两种使用mysql数据库的不同方法-面向对象方法和过程方法。你必须选择其中的一个,然后像这样做-
过程方法(使用mysqli扩展)
$con = mysqli_connect("localhost", "user", "pass", "test") or die("Connection Error: " . mysqli_error($conn));
$query = "SELECT id, name, status, type FROM organization";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$count = mysqli_num_rows($result);
// $count now stores the number of rows returned from that query面向对象的方法(使用mysqli扩展)
<?php
$host = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, status, type FROM organization";
$result = $conn->query($sql);
$count = $result->num_rows;
// $count now stores the number of rows returned
$conn->close();
?>你可以在这里阅读更多信息- https://www.w3schools.com/php/php_mysql_select.asp
https://stackoverflow.com/questions/5761992
复制相似问题