PHP - Returning incorrect value - php

I am trying to add real-time notifications to my website using PHP, Ajax, JS.
It requests the PHP file fine and loads it onto my page so there is nothing wrong there, however the PHP is returning an incorrect number of rows and there doesn't seem to be any problems in the file so I am really confused.
Here is my PHP:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num == 0){
print "";
} else if($num !== 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
If there aren't any rows returned, then it echoes "", however there is a notification set for my user session to test and it is marked as unread. It loads perfectly with the exact same query if used on the page itself, so don't know what's going on at all here.

This line should be like:
else{
print "<span class='notification'> " . $nNum . " </span>";
}
?>
Or
else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
------------------EDITED---------------------------
Please do an echo to this:
<?php
include("config.php");
$query = "SELECT * FROM notifications WHERE user = '$myUser' AND isread = '0'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo $query; //THIS and copy that and put this directly on your mysql manager
if($num == 0){
print "";
} else if($num != 0){
print "<span class='notification'> " . $num . " </span>";
}
?>
PS: Dont use mysql extension...go for mysqli or PDO...your code is vulnerable to sql injection

Related

data output from the database does not work

I have an authorization page, it works everything is ok, but when I log in I want to see additional data from the database for this user.
code
knocks out only one user and everything, when I exit the session and switch on as a new user, nothing is knocked out .... connection to the database works
session_start();
require ('vendor/connect.php');
$FIRSTNAME=$_SESSION['FIRSTNAME'];
$sql = "SELECT BIRTHDAY from users WHERE FIRSTNAME='$FIRSTNAME'";
$result = ibase_query($db, $sql);
if (ibase_fetch_row($result) > 0) {
while($row = ibase_fetch_assoc($result)) {
echo "You BIRTHDAY: " . $row["BIRTHDAY"]. " ";
}
} else {
echo "0 results";
}
Your code should be like below :
$sql = "SELECT BIRTHDAY from users WHERE FIRSTNAME='$FIRSTNAME'";
$result = ibase_query($db, $sql);
$row = ibase_fetch_row($result);
while ($row) {
echo $row[0] . "\t";
}

I want the code to limit what it echos to only echo results that state "online"

I want the code to limit what it echos to only echo results that state "online"
when it sends to unity however still shows all the results
if(mysqli_num_rows($result) > 0){
//show data for each row
while($row = mysqli_fetch_assoc($result)){
if($row['online'] = "online")
{
echo "ip:".$row['ip'] . "|name:".$row['name']. "|usercount:".$row['usercount'] . ";";
}
}
}
You just have one very simple mistake. Change this line:
if($row['online'] = "online")
to:
if($row['online'] == 'online')

Display messages from both users ORDER By time

my problem is that I want to display the messages between two users and order them by time.
I nmanaged to display the messages from each user but it displays first for the user George and the for user Niklakis as it shows in the image below...
What I want is to display both of the messages order by time.
This is part of my code...
$query = "SELECT * FROM messages WHERE recip='$view' ORDER BY time DESC";
$query2 = "SELECT * FROM messages WHERE recip='$username' ORDER BY time DESC";
$result = queryMysql($query);
$result2 = queryMysql($query2);
$num = mysql_num_rows($result);
$num2 = mysql_num_rows($result2);
for ($j = 0 ; $j < $num ; ++$j)
{
$row = mysql_fetch_row($result);
if ($row[3] == 0 || $row[1] == $username || $row[2] == $username)
{
echo date('M jS \'y g:ia:', $row[4]);
echo " <a href='messages.php?view=$row[1]'>$row[1]</a> ";
if ($row[3] == 0)
echo "wrote: "$row[5]" ";
else echo "private message: <span class='whisper'>" .
""$row[5]"</span> ";
if ($row[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row[0]'>erase</a>]";
echo "<br>";
}
}
for ($j = 0 ; $j < $num2 ; ++$j)
{
$row2 = mysql_fetch_row($result2);
if ($row2[3] == 0 || $row2[1] == $username || $row2[2] == $username)
{
echo date('M jS \'y g:ia:', $row2[4]);
echo " <a href='messages.php?view=$row2[1]'>$row2[1]</a> ";
if ($row2[3] == 0)
echo "wrote: "$row2[5]" ";
else echo "private message: <span class='whisper'>" .
""$row2[5]"</span> ";
if ($row2[2] == $username)
echo "[<a href='messages.php?view=$view" ."&erase=$row2[0]'>erase</a>]";
echo "<br>";
}
}
I can understand that my problem happens because I have two different for loops and I probably need only ONE for loop to do this work. But I tried and I could find a way to do this.
Can you help?
Thank you.
You use the same code for both query results (tip for the future: PHP supports something like functions :D try it out) and your queries have the same structure so why don't you just fecht all entries for both users and sort them? Have you tried this out?
$query = "SELECT * FROM messages WHERE (recip='$username' AND auth='$view') OR (recip='$view' AND auth='$username') ORDER BY time DESC";

If value in certain cell equals "Certain Text" echo "Text"

I'm trying to get this to echo a warning message when the cell contains a certain text like "0" or "N/A". It would work when there was no value entered in the first place, but I can't get it to echo when there is already a certain value. Any help would be great. Thanks!
<?php
$listing = "$_POST[listing]";
$sql = "SELECT open_house_attended FROM property_flyers WHERE street_property = '$listing' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "</span><span class='report_bignumber'><br>". $row["open_house_attended"]."</span>";
}
} else {
echo "<br> ". $noresults . "</span>";
}
?>
You can use a little regex -
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
preg_match(trim($row["open_house_attended"]), '/[0]|[N\/A]/', $matches); // try to match '0' or 'N/A'
if(count($matches) == 0) { // do we have matches?
echo "</span><span class='report_bignumber'><br>". $row["open_house_attended"]."</span>";
} else {
echo "<br> ". $noresults . "</span>";
}
}
}
?>
Or you can go a little more directly -
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$ohCount = $row["open_house_attended"];
if(( $ohCount != '0') && ($ohCount != 'N/A')) { // do we have matches?
echo "</span><span class='report_bignumber'><br>". $ohCount ."</span>";
} else {
echo "<br> ". $noresults . "</span>";
}
}
}
?>
preg_match()
$sql = "SELECT open_house_attended FROM property_flyers WHERE street_property = '$listing' AND open_house_attended NOT IN ('0', 'N/A')";
use NOT IN and a list of values to reject. It's one option anyway. The preg match option works as well, just it asks php to do the work and this asks sql to do it.

Getting all email addresses from table

The code below shows one email address as a output but I want to get a list of all
email addresses (seperated by comma) of customer table. How can I get that?
<?php
$SQLstring = "SELECT email FROM customers";
$QueryResult = #mysqli_query($DBConnect, $SQLstring)
or die("<p>Unable to execute the query.</p>" .
"<p> Error code " . mysqli_errno($DBConnect) . ":" . mysqli_error ($DBConnect))."</p>";
$NumRows = mysqli_num_rows($QueryResult);
if ($NumRows == 0)
{
echo "<p>No email found.</p>";
}
else
{
for($i = 1; $i <= $NumRows; $i++)
{
$Row = mysqli_fetch_row($QueryResult);
$email = stripslashes($Row[0]);
echo $email;
}
}
?>
This is mysqli not mysql you're using so things work a little differently...
Assuming you've created your mysqli connection with something like $DBConnect = new msqli( ... );
It's probably better to store the result before you manipulate it; try something like:
$success = $DBConnect->real_query($SQLstring);
if($success) {
$result = $DBConnect->store_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo $row['email'] . "<br />\n"; //for debugging purposes
}
}
$result->free();
Change:
$email = stripslashes($Row[0]);
To:
$email = stripslashes($Row[$i]);
And you should be set
The PHP docs say that mysqli_num_rows may not return the correct number of rows until you've retrieved all rows, so perhaps, instead of using a row count, just keep fetching rows until you run out:
while ($Row = mysqli_fetch_row($QueryResult))
{
$email = stripslashes($Row[0]);
echo $email;
}
EDIT: If you want to store the emails in an array rather than just echoing them, simply change it to this:
while ($Row = mysqli_fetch_row($QueryResult))
{
$email[] = stripslashes($Row[0]);
}
Now $email will be an array containing all of the emails.
use mysql_fetch_assoc in while loop
$NumRows = mysqli_num_rows($QueryResult);
if ($NumRows == 0)
{
echo "<p>No email found.</p>";
}
else
{
while($row = mysql_fetch_assoc($QueryResult)){
$email = stripslashes($row['email']);
echo $email;
}
}

Categories