SELECT total from two different table in mysql but getting different answer

stareen

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) ?>
chris85

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to select two different values from a table with different conditions in mysql?

Mysql Select two column in different table

Getting data from two different tables in mysql

MySQL select by tag from different table

MYSQL Select different records from same table

Mysql Select by name from different table

mySQL: Select and a count(*) from different table?

MYSQL select if exists (from different table)

MySQL: Create table from select with engine different from source table

MySQL query to select one table based on values from two different tables.

Getting the value from one table with inputs of two different tables

Getting two different student data from a numpy table

MySQL - count open items and getting dates from different table

MySQL: Select max from group using the value from a different table?

MySQL select from the same table but from different category with the same limit

SQL- How to select data from two different table?

select query from two different table with matching id

Combining two SELECT queries from same table but different WHERE clause

How to select two parent and one child from 2 different table

subtract two different sum of column from two different table in select query

SELECT * FROM with two different tables

In MySQL, how do I select * from two different tables?

Find total number of data written in two same table from two different databases

Getting Percentage and Total from SELECT GROUP BY Mysql

Select different field from same table field - Mysql

MySQL select photos linked with a single row from different table

Two MYSQL statements with different columns names but from same table

how to combine results of two different queries from one table mysql

getting different answer for sizeof(arr) in different scenario?;