I’m trying to do something very basic but can’t quite figure this one out. I’m using PHP and MySQLi (OOP) rather than MySQL. I could use multiple single select statements then put the count from each number into a variable, but I think it’s inefficient.
Okay, there are a few tables;
TABLE NAME: UCC_STAFF
id
email
username
password
status
TABLE NAME: UCC_TEMP_STAFF
id
email
username
password
status
TABLE NAME: UCC_TEMP_STUDENTS
id
email
username
password
status
TABLE NAME: UCC_VISITORS
id
email
username
password
status
What I’m trying to do is count the number of rows from each table, each row represents a user. As I’ve explained; It’s easy enough to do with one table but I’d like to combine results with a single request into a an array rather than multiple statements into multiple variables.
An example of the desired result would be;
$a = results('UCC_STAFF' => '35',
'UCC_TEMP_STAFF' => '12',
'UCC_STUDENTS' => '543',
'UCC_VISITORS' => '6',);
What I would imagine this to look like would be;
$db_host = "dbhost";
$db_username = "username";
$db_pass = "password";
$db_name = "dbname";
$mysqli = new mysqli("$db_host", "$db_username", "$db_pass", "$db_name");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM UCC_STAFF;";
$query .= "SELECT * FROM UCC_TEMP_STAFF;";
$query .= "SELECT * FROM UCC_STUDENTS;";
$query .= "SELECT * FROM UCC_VISITORS";
if ($mysqli->multi_query($query)) {
// Results into $results
// How do I do this, what goes here?
}
// Print Array
print_r ($results);
i think you looking for something like this with
UNIONDEMO SQLFIDDLE HERE