0

So I have been working on this code for a while. I believe I am really close. My if statement that is inside my while loop isn't showing any data in the area it's suppose to show. I know mysql is old and deprecated. I am going to change it once I figure this out.

$result = mysql_query("SELECT * FROM inventoryTable",$db);
$result2 = mysql_query("SELECT * FROM users WHERE username='$username' and sub = 'yes'",$db);
echo "<TABLE style=\"background-color: #FFFFFF; border: 10px solid A4A4A4;\">";
echo"<TR><TD>"."<B>Title</B>"."</td>";
echo"<TD>"."<B>Authors First Name</B>"."</td>";
echo"<TD>"."<B>Authors Last Name</B>"."</td>";
echo"<TD>"."<B>ISBN</B>"."</td>";
echo"<TD>"."<B>Publisher</B>"."</td>";
echo"<TD>"."<B>Course Number</B>"."</td>";
echo"<TD>"."<B>Source</B>"."</td></TR>";
while ($myrow = mysql_fetch_array($result))
    {
        echo "<TR><TD>".$myrow["title"]."</td>";
        echo "<TD>".$myrow["authorsFirst"]."</td>";
        echo"<TD>".$myrow["authorsLast"]."</td>";
        echo "<TD>".$myrow["ISBN"]."</td>";
        echo "<TD>".$myrow["publisher"]."</td>";
        echo "<TD>".$myrow["courseNum"]."</td>";
        while ($subResults = mysql_fetch_row($result2))
            {
                If($subResults == 'yes' )
            {
                echo "<td>".$myrow["source"]."</td>";
            } else {
                echo "<TD>"."Please subscribe to View"."</td>";
        }
                echo "</TABLE>";
        }
    }
?>

This is the part of my code that isn't showing any results. while

while ($myrow = mysql_fetch_array($result))
    {
        echo "<TR><TD>".$myrow["title"]."</td>";
        echo "<TD>".$myrow["authorsFirst"]."</td>";
        echo"<TD>".$myrow["authorsLast"]."</td>";
        echo "<TD>".$myrow["ISBN"]."</td>";
        echo "<TD>".$myrow["publisher"]."</td>";
        echo "<TD>".$myrow["courseNum"]."</td>";
        while ($subResults = mysql_fetch_row($result2))
            {
                If($subResults == 'yes' )
            {
                echo "<td>".$myrow["source"]."</td>";
            } else {
                echo "<TD>"."Please subscribe to View"."</td>";
        }
                echo "</TABLE>";
        }
    }

I want my session user to be able to see the source from my inventory table if they have a yes in the sub field. If they do not have a yes in the sub field, they will see please subscribe to view. Am i doing the mysql_fetch incorrectly or is there a problem because I have 2 while loops going on at once.

5
  • Apart from the =/== issue – $username likely contains a string value, yes? But what you are comparing it to is an array. Commented Apr 9, 2016 at 3:52
  • And of course your inner while loops only once, on the first iteration of the outer while loop. After that, mysql_fetch_array($result2) will continue to return false, so the loop body isn’t executed any more. You’ll need to use mysql_data_seek to reset the record pointer for that result set, if you want to loop over it again. Commented Apr 9, 2016 at 3:55
  • And the MySQL extension has been deprecated for long time now already, and has been completely removed in PHP 7. You should be using MySQLi or PDO instead. Commented Apr 9, 2016 at 3:57
  • @CBroe - the OP knows that and posted this"... I know mysql is old and deprecated. I am going to change it once I figure this out. ..." indicating that will be fixed in due course Commented Apr 9, 2016 at 3:58
  • $username is from the session. mysql_data_seek requires 2 parameters. What should I use as an offset if I am searching the table? Commented Apr 9, 2016 at 4:58

1 Answer 1

1

you need to have "==" to compare two values, otherwise you assign the second value to the first variable:

...If($username == $subResults)...

or to use a strict comparison of type and content, use "==="

If($username === $subResults)

also I am thinking the code should be

...If($subResults ==="yes"){echo"....///desired content";}else{echo"...//alternate content";}...

and you are missing the echo statement and closing </td> in the code

   "<td>".$myrow["source"]; 

should be

echo"<td>".$myrow["source"]."</td>";

in fact - aren't you missing the closing td's in all of the cells?

Sign up to request clarification or add additional context in comments.

8 Comments

Nothing shows under code"<td>".$myrow["source"];code still. The row is just blank but every other row loads data from my inventory table correctly.
answer amended - look at the last half :)
I am getting an error now. The error says syntax error, unexpected '"<TD>"' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' on line 42. Line 42 is code<TD>".$myrow["authorsFirst"]."</td>"code
that would proabably be becuase the preceding TD isn't being closed - you need to go through and close all TD's that have been opened as well as the TR. also - i just noticed that your border styling is wrong too - you need a "#" before the border color declaration ... "#A4A4A4"...
I am no longer getting an error. "Please subscribe to View" is showing up 7 times though. I thought it was showing up 7 times because there may have been 7 users in my users table. There's 10 so it can't be that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.