Problem with mysqli

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Problem with mysqli

Post by Epiales »

Hey all, I've been trying to make it where there are no message in my inbox, that it shows that you have no messages, but nothing at all seems to work.

I have it setup where you have your inbox, where all u're messages are at. Then there is a place to write messsages and a place to save messages. The saved messages do not show up in the inbox; only the new messages and the read messages that are not saved.

How would I go about using the if statement for when there are NO messages displayed in the inbox. I guess it would mean there were no NEW messages, and no messages that one has already read. I have a readm in the database that equals 0 if the message has been read, and a saved in the database that equals yes if they message has been saved and no if it's not been saved. Below is the working code I have for the inbox. But when I have saved or deleted all messages, I get these errors, which I assume it's wanting something to be taken from the database, but nothing is there, so it gives this error:

Code: Select all

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 702
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 725
Line 702 is the $numrows:

Code: Select all

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' ORDER BY date DESC, readm=1 DESC $limit"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query); 
Line 725:

Code: Select all

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
Here is the working code for the message inbox upon clicking on your messages:

Code: Select all

<?php //******************** BEGINNING OF USERS MESSAGING INBOX SYSTEM ********************// ?>


<?php
if(isset($_GET['messages'])) { 

?>

<!DOCTYPE html>
<html>
<head>
    <title>Tabbed Content</title>
    <script src="tabcontent.js" type="text/javascript"></script>
    <link href="style/tabcontent.css" rel="stylesheet" type="text/css" />
</head>
<body style=" font-family:Arial;">
    <div style="width: 700px; margin: 0 auto; padding: 120px 0 40px;">
        <ul class="tabs" data-persist="true">
            <li><a href="#view1"><b><big>Inbox</a></big></b></li>
            <li><a href="#view2"><b><big>Send Messages</a></big></b></li>
            <li><a href="#view3"><b><big>Saved Messages</a></big></b></li>                        
        </ul>
        <div class="tabcontents">
            <div id="view1">
               <p>
           
<?php
$bypass = 0;
if($bypass != 1)

{
if(isset($_GET['messages']) || isset($_POST['messages'])) 
{
$bypass = 1;

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' ORDER BY date DESC, readm=1 DESC"; 
$user_query = mysqli_query($db_conx, $sql);
$nr = mysqli_num_rows($user_query);

echo "<left>";
echo "<table border = '0' width = '100%' cellspacing = '5'>";

if (isset($_GET['pn'])) {  
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);  

} else { 
    $pn = 1;
} 

$itemsPerPage = 5; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {  
    $pn = 1;  
} else if ($pn > $lastPage) {  
    $pn = $lastPage;  
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
} else if ($pn == $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $sub2 . '">' . $sub2 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $add2 . '">' . $add2 . '</a>  ';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' ORDER BY date DESC, readm=1 DESC $limit"; 
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);

$paginationDisplay = "";  

if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '        ';

    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?messages=1&pn=' . $nextPage . '"> Next</a> ';
    } 
}

$outputList = '';

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $row['subject'] = left($row['subject'], 20) . "...";
    
    if($row['readm'] == 1) {

    $outputList .= "
    
    <td><left><font color = 'red'>NEW!</font></td>
    
    <td><left>$row[sender]          <td>
    
    <td><left>$row[subject]   </td>
    
    <td><left>$row[date]      </td>
    
    <td><right><form method='post' action='mafiawarskingdom.php?messages=1'><input type='submit' value='Read'>
       <input type='hidden' name='readmessage' value='1'>
       <input type='hidden' name='randid' value='$row[randid]'>
       <input type='hidden' name='date' value='$row[date]'></td></tr>
        </form>";
         
         }
         else
         {

    $outputList .= "
    
    <td></td>
    
    <td><left>$row[sender]          <td>
    
    <td><left>$row[subject]   </td>
    
    <td><left>$row[date]      </td>
    
    <td><right><form method='post' action='mafiawarskingdom.php?messages=1'><input type='submit' value='Read'>
       <input type='hidden' name='readmessage' value='1'>
       <input type='hidden' name='randid' value='$row[randid]'>
       <input type='hidden' name='date' value='$row[date]'></td></tr>
        </form>";

        }
    }
}

?>
   </div> 
      <div style="padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
      <div align="left"><?php echo "$outputList"; ?></div>

<?php

}
      echo "</table>";
      echo "</td></tr></table>";    
      echo "</small>";    
?>
      <div style="padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>

<?php //******************** ENDING OF USERS MESSAGING INBOX SYSTEM ********************// ?>

I've tried tons of combinations of the if and else statement to get it where it shows "There are no messages in your inbox at this time" or something like that, but all I ever get is that error. And I'm not sure how to go about it because of the row readm and saved in the database. Thanks for any help!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Problem with mysqli

Post by Sim »

hmm.

The real error is line "700"

Code: Select all

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' ORDER BY date DESC, readm=1 DESC $limit"; 
Try

Code: Select all

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' AND readm='1' ORDER BY date DESC $limit"; 

you tried to do a where clause on a ORDER
Last edited by Sim on Sat Oct 04, 2014 2:11 am, edited 1 time in total.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Problem with mysqli

Post by Sim »

Also, I am not sure if you know

Code: Select all

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
If your query has 0 RESULTS, anything inside this WHILE loop will never execute, so technically you don't have to check if it has any results.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Problem with mysqli

Post by Epiales »

Sim wrote:hmm.

The real error is line "700"

Code: Select all

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' ORDER BY date DESC, readm=1 DESC $limit"; 
Try

Code: Select all

$sql = "SELECT * FROM messages WHERE pid='$user_username' AND saved='no' AND readm='1' ORDER BY date DESC $limit"; 

you tried to do a where clause on a ORDER
Okay, I see what happened on that row. I fixed it accordingly, but still get the error when there are no messages to display:

Code: Select all

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 702

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 725
I'll try some more if statements tomorrow when I have time. Thank you!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Problem with mysqli

Post by Epiales »

Okay, I've edited code for the below, but nothing is working. Still giving the errors above:

Code: Select all

<?php
$outputList = '';


while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
--------------------------------------------------------------------------------------------------------------------------------------
If the player has read the inbox, saved the inbox, or there are no messages, I wrote the next lines for that


    if($row['readm'] == 0 && $row['saved'] == 'yes' || $numrows < 1) {
    
        $outputList .= "test";
        }else{


--------------------------------------------------------------------------------------------------------------------------------------

    $row['subject'] = left($row['subject'], 20) . "...";
    
    if($row['readm'] == 1) {

    $outputList .= "
    
    
    
    <td><left><font color = 'red'>NEW!</font></td>
    
    
    <td><left>$row[sender]          <td>
    
    <td><left>$row[subject]   </td>
    
    <td><left>$row[date]      </td>
    
    <td><right><form method='post' action='mafiawarskingdom.php?messages=1'><input type='submit' value='Read'>
       <input type='hidden' name='readmessage' value='1'>
       <input type='hidden' name='randid' value='$row[randid]'>
       <input type='hidden' name='date' value='$row[date]'></td></tr>
        </form>";
         
         }
         else
         {

    $outputList .= "
    
    <td></td>
    
    <td><left>$row[sender]          <td>
    
    <td><left>$row[subject]   </td>
    
    <td><left>$row[date]      </td>
    
    <td><right><form method='post' action='mafiawarskingdom.php?messages=1'><input type='submit' value='Read'>
       <input type='hidden' name='readmessage' value='1'>
       <input type='hidden' name='randid' value='$row[randid]'>
       <input type='hidden' name='date' value='$row[date]'></td></tr>
        </form>";

        }
    }
}
}
?>
I think with it being inside the loop is an issue too :(

But the way it works now is a player sends the message...the message shows as new....the player reads the message.....the NEW goes away and it remains in their inbox until they either save it or delete it. If they save it, it moves over to their saved messages. But when they have either deleted or saved everything, I get the mysql errors because there is nothing in their inboxes at that point.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Problem with mysqli

Post by Winawer »

Echo your query and you should immediately see the problem ($limit).
Post Reply

Return to “Beginner Help and Support”