0

I am trying to run a query on getting the last 9 entries from the database, something simple I did in text, but trying to do it in a table form seems to mess it up. Here is my query code:

<?
$con = Omitted
    // create query
$query = "SELECT * FROM libvid ORDER BY title DESC LIMIT 9";

// execute query
$result = mysql_query("SELECT * FROM libvid ORDER BY title DESC LIMIT 9");

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
    echo "<table border='0'>";
     echo "<tr>";
     $col=0;
     while($row = mysql_fetch_row($result)) {
    $col++;
    print '
        <td width="170px" height="130px">
          <font size="-3"><strong>'. $row['title'].'</strong></font><br>
          <a href="#'.$row['title'].'"    onClick="javascript:loadwindow(\'popup.php?id=' .$row['id']. '\')">
            <img class="imgb" style="border: 2px solid black; border-radius: 30px; -moz-border-radius: 30px;-khtml-border-radius: 30px; -webkit-border-radius: 30px;" src=http://i3.ytimg.com/vi/'.$row['emurl'].'/0.jpg width="162" height="110" />
          </a>
        </td>';
    if($col % 3 ==0) print '</tr><tr>';

        }  //end foreach $newarr
 print '</tr></table>';
}
?>

I am getting the resource id #13 error and will not display anything

4

2 回答 2

0

为了更好地调试您的问题,您应该添加错误处理。按如下方式更新您的mysql_query调用以查看您遇到的错误:

$result = mysql_query("SELECT * FROM libvid ORDER BY title DESC LIMIT 9");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

除了您遇到的错误之外,我看到的另一个问题是您使用 mysql_fetch_row,它返回一个数字数组并且您正在关联地访问它,即$row['title']。请参阅链接的文档。

我认为您想使用mysql_fetch_arraymysql_fetch_assoc,如下所示,您定义了 while 循环:

while($row = mysql_fetch_array($result)) {

当您阅读链接文档时,请记下有关弃用mysql_函数的警告消息。您应该考虑使用mysqlipdo

于 2013-08-11T05:23:23.500 回答
0

使用mysql_fetch_arrayormysql_fetch_assoc代替mysql_fetch_row.

于 2016-12-27T06:28:57.120 回答