<?php
function getnumberRows() {
$db_conn = getConn();
if( ! $db_conn) {
return flase;
}
$sql = "SELECT count(leads_ID) FROM table1";
$result = $db_conn->query($sql);
$db_conn->close();
return $result;
} ?>
<?php $result = getnumberRows(); ?>
<p><a href="agentHome.php">New Lead</a></p>
<?php while ( $rows = $result->fetch_assoc() ): ?>
<p><a href="agentAllLeads.php">All leads <?php echo $rows; ?></a></p>
<?php endwhile; ?>
我想要显示我在All Leads Like All中有多少记录(5)当我将添加另一个时它将是所有潜在客户(6)我想做那样的事情我怎么能这样做一些帮帮我............
答案 0 :(得分:2)
如果是mysqli
的情况,则返回mysqli_result
的实例。您应该使用mysqli_fetch_row
获取查询结果,例如:
$sql = 'SELECT count(leads_ID) FROM table1';
$result = $db_conn->query($sql);
return $result ? mysqli_fetch_row($result)[0] : 0;
另请注意,您不应在每次函数调用时重新连接到数据库。是否使用persistent connections,或者仅在必要时(例如if (!$this->connection) $this->connection = $this->connect();
)创建一个包装类(数据库抽象层)连接到数据库,并在__destruct
方法中断开连接。考虑到这些注意事项,您应该按如下方式修改您的功能:
function getnumberRows() {
$db_conn = getConn();
if (!$db_conn) {
return 0;
}
$sql = "SELECT count(leads_ID) FROM table1";
$result = $db_conn->query($sql);
// You should normally do this in a database abstraction layer
// $db_conn->close();
return $result ? mysqli_fetch_row($result)[0] : 0;
}
<p><a href="agentHome.php">New Lead</a></p>
<p><a href="agentAllLeads.php">All leads <?php echo getnumberRows(); ?></a></p>