Reputation: 57
<?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; ?>
I want to show how many records i have in All Leads Like All leads(5) when i will add another one it will be All leads(6) i want to do like that thing how way i can do it some one help me............
Upvotes: 1
Views: 50
Reputation: 21492
If the case of mysqli
, you are returning an instance of mysqli_result
. You should fetch the result of the query with mysqli_fetch_row
, for instance:
$sql = 'SELECT count(leads_ID) FROM table1';
$result = $db_conn->query($sql);
return $result ? mysqli_fetch_row($result)[0] : 0;
Also note that you shouldn't re-connect to the database on every function call. Whether use persistent connections, or make a wrapper class (a database abstraction layer) connecting to the database only when necessary (something like if (!$this->connection) $this->connection = $this->connect();
) and disconnecting in the __destruct
method, for example. With these considerations in mind, you should modify your function as follows:
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>
Upvotes: 2