I got a problem in my code where I started to get lost. My coding shows the different result in MySQL and website display
.
this is my code, I query it in MySQL database,
it fetches the total of student + employer = 1341
. But when I wrote in PHP, it displays the total of 1 on the website. why is this happen? For information, both codes below here are the function in MySQL but not displaying right amount on the website.
Am I missing something? I'm still learning the right way to query.
First code:
<?php
session_start();
$conn = new mysqli ('localhost', 'root', '', 'dashboard');
$stddboard = $conn ->query ("SELECT (SELECT COUNT(*) FROM student) + (SELECT
COUNT(*) FROM employer) FROM dual");
$totstddboard = mysqli_num_rows($stddboard);
?>
<?php echo mysqli_num_rows($stddboard) ?>
Second code:
<?php
session_start();
$conn = new mysqli ('localhost', 'root', '', 'dashboard');
$stddboard = $conn ->query ("SELECT ( (SELECT COUNT(*) FROM student) +
(SELECT COUNT(*) FROM employer)
) AS 'Column' ");
$totstddboard = mysqli_num_rows($stddboard);
?>
<?php echo mysqli_num_rows($stddboard) ?>
The mysqli_num_rows
returns the number of rows that are returned. The count()
function returns 1 row with a count of the matching rows in it. So what you really need to do is fetch
your result object. For code block 1 you'd use:
<?php
session_start();
$conn = new mysqli ('localhost', 'root', '', 'dashboard');
$stddboard = $conn ->query ("SELECT (SELECT COUNT(*) FROM student) + (SELECT
COUNT(*) FROM employer) as da_count FROM dual");
$row = $result->fetch_array(MYSQLI_ASSOC);
$totstddboard = $row['da_count'];
echo $totstddboard;
?>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments