如何显示一条SQL语句的所有结果?


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

我想显示我所有的衬衫,但我总是得到相同的错误

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

这是我现在的代码

<?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="specialsListBoxContents 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_*()函数如果失败则返回布尔值FALSE,这意味着您的查询调用没有成功。添加以下代码找出原因:

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

示例代码:

<?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
}
?>

这里是更多的信息。注意while内部的现有php模式。