开发者

How do I show all the results from an SQL statement?

I wanted to display all of my shirts but I always get the same error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/36/d362778419/htdocs/home/specials.php on line 34

Here is the code I have currently in place

<?php
require "connect2.php";

$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
$runrows = mysql_fetch_array($sql); 
$title = $runrows['title'];

$picture = $runrows['picture'];

$newp = $runrows['newp'];

$date = strftime("%b %d, %Y %l:%M %p" ,strtotime($runrows['date']));

$oldp=$runrows['oldp'];

$viewl=$runrows['viewl'];

$shirtl = $runrows['shirtl'];
echo "";
?>

<div class="specialsListBoxConte开发者_运维技巧nts centeredContent back" style="width:25%;"><div class="product-col" >             <div class="img">           

        <a href="detail.php?id=<?php echo $id; ?>"><img src="<?php echo $picture; ?>" alt="<?php echo $title; ?>" title=" <?php echo $title; ?> " width="190" 

height="160" /></a>             </div>              <div class="prod-info">                 <div class="wrapper">       

                <div class="price">                         <strong><span class="normalprice"><?php echo $oldp; ?

></span><br /><span class="productSpecialPrice"><?php echo $newp; ?></span></strong>                        </div>                  

    <div class="button"><a href="detail.php?id=<?php echo $id; ?>"><img src="images/button_add_to_cart.gif" alt="Add to Cart" title=" Add to Cart " width="54" 

height="49" /></a></div>                    </div>              </div>          </div></div>

<?php
?>


mysql_*() functions return boolean FALSE if they fail, which means your query call did not succeed. Add this code to find out the reason why:

$sql = mysql_query("SELECT * FROM shirts WHERE all='1'");
if ($sql === FALSE) {
    die(mysql_error());
}


Here's some sample code:

<?php

$sql = mysql_query("SELECT * FROM shirts WHERE `all`='1'");

if (!$sql ) {
    echo "Could not successfully run query from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($sql) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
while ($row = mysql_fetch_assoc($sql)) 
{
?>
    <a href="detail.php?id=<?php echo $row['id']; ?>"><img src="<?php echo $row['picture']; ?>" alt="<?php echo echo $row['title']; ?>" title=" <?php echo $row['title']; ?> " width="190" height="160" />
<?php
}
?>

And here is more info. Notice the exiting php mode inside the while.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜