this PHP code, is supposed to get the amount of mails received by the user (internal mail system). It works fine, until i add the "AND deleted = '0'" in the SQL code below. And in the SQL Table, all mails is set to "0" in the deleted input.
$user = $get2['username'];
$sql = "SELECT COUNT(*) FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['COUNT(*)']. "";
}
} else {
echo "0";
}
$conn->close();
Try with below code..
`$user = $get2['username']`;
$sql = "SELECT COUNT(*) AS COUNTS FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['COUNTS']. "";
}
} else {
echo "0";
}
$conn->close();
Already solved.
Did not connect properly to the database which i was sure it did.
You have written query with just a little mistake.
So please try following query :
$sql = "SELECT COUNT(*) AS counted FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "". $row['counted']. "";
}
} else {
echo "Sorry";
}
$conn->close();
I hope this will work.
Change AND deleted = '0' ORDER BY to AND deleted = 0 ORDER BY
I'm guessing that the column DELETED is some sort of number data format instead of a string format.
Related
/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
I am trying to accomplish the following:
Get the project id from a database where the username =...
-> Users can be signed into different projects and it is being tracked in a database
Next step is to get information of the project database to display it for the user
-> But since the user can be signed into different projects I want him to see them underneath each other in a not yet specific order (I a
This is basically what I was trying to get to work and it displays the right content but unfortunately stops at the first project. My assumption is that it stops at the first project id and doesn't continue
-> I checked to see what code of the code works and as soon as is cut the information gathering query I get all project_id's
Here is basically the code that I am working with right now:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//OUTPUT
echo "
$row['project_name'];
$row['count'];
$row['sport'];
$row['manufacturer'];
$row['creation_datetime'];
";
}
}
}
}
It looks like you ought to be able to do this with a single query - and as you are taking user supplied input you ought to consider using a prepared statement
$sql='select `project_name`, `sticker_count`, `manufacturer`, `sport`, `creation_datetime`
from `project_db` where `project_id` = ( select `project_id` from `project_members` where `user_id` = ? )';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s',$user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($project_name,$sticker_count,$manufacturer,$sport,$creation_datetime);
while( $stmt->fetch() ){
echo $project_name,$sticker_count,$manufacturer,$sport,$creation_datetime;
}
You overwrite the first result with the second result. Change to:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result2 = $conn->query($sql);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
//OUTPUT
echo $row2['project_name'] . ' '
$row2['count'] . ' '
$row2['sport'] . ' '
$row2['manufacturer'] . ' '
$row2['creation_datetime'];
}
}
}
}
Note putting arrays in your string doesn't work, so i have concatenated the echo instead.
I'm working on attendance base on login and logout, Is there a way I can make this code work, I'm having a problem on after successfully update the data it will go to another notepad and in there it has a select query that will display the image and basic info of user. But when I tried to logout it displays different user info and images.
Here is my code for php:
<?php
include_once ('connection.php');
if(isset($_POST['submit']))
{
$username2 = $_POST['username2'];
$password1= $_POST['password1'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"SELECT tbl_visitor.Birthday,tbl_visitor.School_Company,tbl_visitor.Contact_number,tbl_visitor.Visitor_Address,tbl_visitor.image,tbl_visitor.Visitor_username,tbl_visitor.Visitor_id,tbl_visitor.Visitor_password,concat(Visitor_first_name,'',Visitor_last_name) as name,
tbl_visitor_form.Time_in,tbl_visitor_form.Time_out , tbl_visitor_form.Number FROM tbl_visitor LEFT JOIN tbl_visitor_form on tbl_visitor.Visitor_id = tbl_visitor_form.Visitor_id WHERE tbl_visitor.Visitor_username = '$username2' and tbl_visitor.Visitor_password = '$password1'
order by Number DESC limit 1");
$count = mysqli_num_rows($sql);
if ($count == 0) {
$_SESSION['error_message'] = "Incorrect username or password";
header("Location:logout.php?");
} else{
while ($row = mysqli_fetch_array($sql)) {
$username2 = $row['Visitor_username'];
$password1 = $row['Visitor_password'];
$name=$row['name'];
$id=$row['Visitor_id'];
$image=$row['image'];
if(empty($row['Time_in'])) {
header("location:visitorvalidate1.php");
} else if(empty($row['Time_out'])){
$InsertSql = "Update tbl_visitor_form set Time_out = '$time' where Visitor_username='$username2' and Visitor_password = '$password1' order by Number DESC limit 1 ";
$res = mysqli_query($conn, $InsertSql);
header("location:outsuccess.php?");
}else{
header("location:visitorvalidate1.php");
}
}
}
}
?>
here is my outsuccess.php:
<?php
include_once('connection.php');
$sql = "select Visitor_username, image, Visitor_name from tbl_visitor_form
order by Number DESC limit 1 ";
$result = mysqli_query($conn,$sql);
while( $row = mysqli_fetch_array($result)) {
$uname=$row['Visitor_username'];
$name=$row['Visitor_name'];
$image=$row['image'];
}
?>
the first query above is working but the second query in select has the problem, it selects different value after the update. for example, there is two user has already login user1 and user2 when I try to logout user1 it successfully updated but it displays the info of user2 instead of user1 on outsuccess.php. Hope you can help me fix this. Advance Thanks
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help
I want to display banner after first result of MySQL query, after banner it will display the remaining results.
Here is my code:
$result = mysqli_query($con,"SELECT id, name FROM database
WHERE id > '".$id."' LIMIT 5");
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_array($result)) {
$name = $row['name'];
echo $name;
}
}
Example Result:
Peter
Banner Code Here
Steve
David
Adam
As you not explained your question in well manner so tried on the basis of my understandings try this maybe helpful,
$isDispaly_Banner = true;
$result = mysqli_query($con,"SELECT id, name FROM database WHERE id > '".$id."' LIMIT 5");
if ( mysqli_num_rows($result) > 0 ) {
while($row = mysqli_fetch_array($result)) {
$name = $row['name'];
echo $name;
if($isDispaly_Banner){
echo "Banner COde Here";
$isDispaly_Banner = false;
}
}
}