0

I have some PHP that's pulling MySQL entries into an HTML table. The table headers are still visible when there are no results provided by the query so I wanted to use an IF function to display an alternate message on the front end.

My so far limited knowledge of PHP means I'm struggling to find any way to check the results. Every method I've come across online had been a dead end.

Can anybody see what I'm missing please?

$result = mysqli_query($con,"SELECT * FROM stock WHERE (SKU='$SKUsearch1' AND Location='London Store') OR (SKU='$SKUsearch2' AND Location='London Store') OR (SKU='$SKUsearch3' AND Location='London Store') OR (SKU='$SKUsearch4' AND Location='London Store') OR (SKU='$SKUsearch5' AND Location='London Store') OR (SKU='$SKUsearch6' AND Location='London Store') OR (SKU='$SKUsearch7' AND Location='London Store') OR (SKU='$SKUsearch8' AND Location='London Store') OR (SKU='$SKUsearch9' AND Location='London Store') OR (SKU='$SKUsearch10' AND Location='London Store') OR (SKU='$SKUsearch11' AND Location='London Store') OR (SKU='$SKUsearch12' AND Location='London Store') ");


if (!$row['Location']) {

                echo "<table class='availableTable' border='1'>
                <tr>
                <th>Location</th>
                <th>Item</th>
                <th>Availability</th>
                </tr>";

                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {
                echo "<tr>";
                echo "<td>" . $row['Location'] . "</td>";
                echo "<td>" . $row['Name'] . "</td>";

                if(($row['Available']) > 0) {
                echo "<td> In Stock </td>";
                }
                else {
                echo "<td> Out of Stock </td>";
                }

                echo "</tr>";
                }
                echo "</table>";




}

else {

        echo "<div class='availableText'>No instore stock information is currently available for this product.</div>";

}
4
  • I strongly recommend changing that SELECT statement to something like: Location='London Store' AND SKU IN (....) Commented Sep 19, 2018 at 18:54
  • 2
    Where is $row defined? It looks like it's being used in the IF statement before it's defined Commented Sep 19, 2018 at 18:56
  • I suspect @dustytrash nailed it. If not, please edit your question to include the output you are getting. Commented Sep 19, 2018 at 19:03
  • @dustytrash Gotcha. I was looking for a better method. Cheers! Commented Sep 19, 2018 at 19:14

1 Answer 1

1

$row['Location'] $row is not available before while loop

$result = mysqli_query($con,"SELECT * FROM stock WHERE (SKU='$SKUsearch1' AND Location='London Store') OR (SKU='$SKUsearch2' AND Location='London Store') OR (SKU='$SKUsearch3' AND Location='London Store') OR (SKU='$SKUsearch4' AND Location='London Store') OR (SKU='$SKUsearch5' AND Location='London Store') OR (SKU='$SKUsearch6' AND Location='London Store') OR (SKU='$SKUsearch7' AND Location='London Store') OR (SKU='$SKUsearch8' AND Location='London Store') OR (SKU='$SKUsearch9' AND Location='London Store') OR (SKU='$SKUsearch10' AND Location='London Store') OR (SKU='$SKUsearch11' AND Location='London Store') OR (SKU='$SKUsearch12' AND Location='London Store') ");


    if (mysqli_num_rows($result)>0) {

                    echo "<table class='availableTable' border='1'>
                    <tr>
                    <th>Location</th>
                    <th>Item</th>
                    <th>Availability</th>
                    </tr>";

                    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                    {
                    echo "<tr>";
                    echo "<td>" . $row['Location'] . "</td>";
                    echo "<td>" . $row['Name'] . "</td>";

                    if(($row['Available']) > 0) {
                    echo "<td> In Stock </td>";
                    }
                    else {
                    echo "<td> Out of Stock </td>";
                    }

                    echo "</tr>";
                    }
                    echo "</table>";




        }

        else {

                echo "<div class='availableText'>No instore stock information is currently available for this product.</div>";
    }

try mysqli_num_rows($result) http://php.net/manual/en/mysqli-result.num-rows.php

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

Comments

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.