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

时间:2011-09-15 17:05:22

标签: php sql

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

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

2 个答案:

答案 0 :(得分:1)

mysql _ *()函数如果失败则返回boolean FALSE,这意味着您的查询调用没有成功。添加此代码以找出原因:

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

答案 1 :(得分:0)

以下是一些示例代码:

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

here更多信息。请注意php内的退出while模式。