Count multiple columns from different tables php - php

I'm trying to count two different tables but it's not working, and I cannot find out where the problem is, and how to implement it with PHP.
$sql = "SELECT COUNT(users.user_id) AS totalUsers FROM users, SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<td><td>" . $row["totalUsers"] . "</td></tr>";
echo "<td><td>" . $row["totalBikes"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();

Get the amounts from each table separately. Each SELECT is a unique resultset from your query, listing them with commas won't retrieve all data as you are expecting.
$result = $conn->query("SELECT COUNT(users.user_id) AS totalUsers FROM users");
$totalUsers = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalUsers = $row["totalUsers"];
}
$result = $conn->query("SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes");
$totalBikes = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalBikes = $row["totalBikes"];
}
$conn->close();
Then you can work with the values stored in $totalUsers and $totalBikes:
echo "<tr><td>" . $totalUsers . "</td></tr>";
echo "<tr><td>" . $totalBikes . "</td></tr>";

Related

Select Data by Id PHP

I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>

Display row that is not repeated linking with database

I am new in php. I make a quiz app and I want to show questions that is not repeated again . here's my code.
Please help me to show require result.
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
echo "
<h2>" . $row["question"]. "</h2>";
break;
}
}
$check_id = array ($row['id']);
echo $check_id['0'];
if(array ($row['id']) == $check_id){
echo "no question ";
}
else{
echo "
<h2>" . $row["question"]. "</h2>";
}
?>
Your question is not clear. But I guess, you can solve it by array_unique($array).
array_unique($array);
<?php
include('connect.php');
$sql = "SELECT * FROM quiz_question WHERE theme_id= 2 ORDER BY RAND ()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$question = $row['question'];
echo "
<h2>" . $row["question"]. "</h2>";
$check_id = array($id);
}
}
$check_id_unique=array_unique($check_id);
?>

build an array of an array from SQL query with php

I have the following working array of an array in PHP:
$explore1 = array
(
array("a.html", "A"),
array("b.html", "B"),
array("c","C")
);
$arrlength = count($explore1);
for($x = 0; $x < $arrlength; $x++) {
echo '<li>'.$explore1[$x][1].'</li>';
} }
I want to populate the explore1 array from SQL. If i simply change the code like below it has errors but I don't know what I'm suppose to do instead?
$sql = 'SELECT url, name FROM explore_items WHERE menuID="item1"';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$explore1 = array //IT DOESN'T LIKE THIS LINE
(
while($row = $result->fetch_assoc()) {
// BUILD SAME LINES AS ABOVE WITH ECHO
echo "array('" . $row["url"]. ", '" . $row["name"]. "'),";
}
);
Can anybody help?
Either you don't need $explore1 at all:
$sql = 'SELECT url, name FROM explore_items WHERE menuID="item1"';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//echo "array('" . $row["url"]. ", '" . $row["name"]. "'),";
echo '<li>'.$row["name"].'</li>';
}
}
or if you need you can fetch_all():
$sql = 'SELECT url, name FROM explore_items WHERE menuID="item1"';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$explore1=$result->fetch_all(MYSQLI_ASSOC);
foreach($explore1 as $row ) {
//echo "array('" . $row["url"]. ", '" . $row["name"]. "'),";
echo '<li>'.$row["name"].'</li>';
}
}
Please learn some php basics.
Defining an array of arrays is something like:
$explore1 = array();
while($row = $result->fetch_assoc()) {
$explore1[] = array($row["url"], $row["name"]);
}
You must populate the array inside the loop:
while($row = $result->fetch_assoc()) {
$explore1[$row["url"]] = $row["name"];
}

While nested lops in php fetching data from sql database

$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>".while($row = $result1->fetch_assoc())
{echo'<img src='".$row["image"]."'>;}
.</aside>}";
I have been using while loops for fetching data from table. My connection is working is perfect but I need another loop in the first that is not working. Isn't it the good way?
Update: I tried to finish echo and again started as follow but still an error
while($row = $result->fetch_assoc()) {
echo "<div id='post'><h1>"
.$row["heading"].
"</h1><div class='post-side'><img class='post-image' src='"
.$row["image"].
"'><div class='post-data'><p><strong>Age: </strong><span>$age</span></p><p><strong>Date of birth: </strong><span>"
.$row["day"].
"-"
.$row["month"].
"-"
.$row["year"].
"</span></p></div></div></div><div class='description'><p>"
.$row["description"].
"</p></div><div class='bottom-related'><aside class='related-post'>";
while($row = $result1->fetch_assoc())
{echo"<img src='"
.$row["image"].
"'>/";}.echo"</aside><aside class='ad2'>".$includead."</aside></div>";
}
echo "</div>";
} else {
echo "No table found";
}
$conn->close();
You're trying to concatenate to a string a WHILE loop; this is wrong.
You should echo your first part, end with it and then do your while loop, and echo the end afterwards:
Your quotes are a bit messed up as well
if ($result->num_rows > 0)
{
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>";
while($row = $result1->fetch_assoc())
{
echo'<img src="'.$row["image"].'">';
}
echo '</aside>';
}
You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:
echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;
But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :
<?php
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";
while($row = $result1->fetch_assoc()) {
echo'<img src="' . $row["image"] .'">';
}
echo "</aside>";
}

Adding a row to another row

I am trying to get the firstname and lastname rows together in one row.
This is the code I have so far ,
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]."</td><td>" .$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
the firstname and lastname are now shows seperatly I want them to be together as one ?
Almost there! You just needed to place first name and second name in the same td
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td><td>" . $row["phone"] . "</td><td>" . $row["department"] . "</tr>";
I think you mean this:
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]." " .$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Huum..
Like that ?
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]." ".$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
I believe you mean in the same row/cell? Like this?
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"] . $row["lastname"]."</td><td>" .$row["phone"]."</td>
<td>".$row["department"]."</tr>";
}
write the query in this manner :
$sql = "SELECT firstname, lastname, phone, department,concat(firstname,'',lastname) fullname FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
to get fullname you need to use: $row["fullname"]

Categories