这个PHP代码只占用我数据库的第一行。救命?

时间:2011-05-29 18:16:14

标签: php mysql

<?php
$objConnect = mysql_connect("localhost","","root") or die(mysql_error());
$objDB = mysql_select_db("mydb");
$pic2 = "SELECT * FROM thumbs";
if (!isset($_GET['Page']))  $_GET['Page']='0';
$pic1 = mysql_query($pic2);
$Num_Rows = mysql_num_rows($pic1);
$Per_Page = 16;   // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
    $Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
    $Num_Pages =1;}
    else if(($Num_Rows % $Per_Page)==0)
    {
        $Num_Pages =($Num_Rows/$Per_Page) ;
    }
    else
    {
        $Num_Pages =($Num_Rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;}
        $pic2 .=" order  by thumbnailID ASC LIMIT $Page_Start , $Per_Page";
        $pic1  = mysql_query($pic2);
        $cell = 0;
        $link1 = "SELECT * FROM thumbs";
        $result_link1 = mysql_query($link1);
        $link = mysql_fetch_array($result_link1);
        $alt1 = "SELECT * FROM thumbs";
        $alt = mysql_fetch_array(mysql_query($alt1));
        $height1 = "SELECT * FROM thumbs";
        $height = mysql_fetch_array(mysql_query($height1));
        $width1 = "SELECT * FROM thumbs";
        $width = mysql_fetch_array(mysql_query($width1));
        $time1 = "SELECT * FROM thumbs";
        $time = mysql_fetch_array(mysql_query($time1));
        $folder1 = "SELECT * FROM thumbs";
        $folder = mysql_fetch_array(mysql_query($folder1));
        $filed1 = "SELECT * FROM thumbs";
        $filed = mysql_fetch_array(mysql_query($filed1));
        echo '
         <div id="tablediv">
         <table border="0" cellpadding="17" cellspacing="0" class="table">
         <tr>';

        while($pic = mysql_fetch_array($pic1))
        {
            if($cell % 4 == 0)
            {
                echo '</tr><tr>';
            }
        {
        echo'
         <td>
          <a href="/' . $link["link"] . '">
            <div class="image"><img src="' . $pic["pic"] . '"
                   alt="' . $alt["alt"] . '" 
                   height="' . $height["height"] . '" 
                   width="' . $width["width"] . '" 
              />
            </div>
          </a>
          <div class="timeago">
            <abbr class="timeago" title="' . $time["time"] .'">
           </abbr>&nbsp;in <a href="/' . $folder["folder"] . '">
           <span class="filedat">' . $filed["filed"] . '</div></a>
          </div>
        </td>'; 
    }
    $cell++;
  }
  echo '</tr></table></div>';
?>

我是一个noobie,我不知道为什么这段代码只占用我的mysql数据库的第一行。这就是我的意思。 我的phpmyadmin db看起来像:

thumbnailID |    link    |     pic     |   alt   | time | height | width | folder | filed
     1           blog      random.png    descrip   3:45    300      200      emm     weewr 
     2           about      etc.png      desc      4:15    130      150      wer     ewrre
     3           misc       er.png        desc     2:30    324      435      sdf     dcv
     4           misc        etc.png      info     6:50    203      034      sdf      qwd
     5           about       meh.png       whoa    10:12   395      234      tb      asd

正如您所看到的,有五行不同。但由于某种原因,行2,3,4和5都具有相同的链接,alt,时间高度,宽度,文件夹和字段,如第1行。唯一不同的是图片。

如果这听起来令人困惑,但我不知道如何以任何其他方式使用它。

4 个答案:

答案 0 :(得分:1)

您正在发出多个mysql_query请求,这些请求始终会将指针重置为第一行。但是mysql_fetch_array将在循环中使用。它还查询行,而不是按列查询。

$result = mysql_query("SELECT * FROM thumbs");
while ($row = mysql_fetch_assoc($result)) {

    print "   <td> $row[link] + $row[pic] + $row[alt] ";

    // use only the loop variable here, not the static request arrays
    // from before
}

您的问题是您使用以前的结果数组$ link [],$ pic [],$ alt [],$ width [],$ height []作为输出代码 - 您应该只使用{{ 1}}(或代码中的$row)。

答案 1 :(得分:0)

正如documentation所述,

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

你看,有一排。

答案 2 :(得分:0)

请阅读small example我为您精心挑选。阅读并仔细查看,直到您了解它是如何打印表格的。这将帮助您以更简洁明了的方式完成您发布的脚本。

快乐的编码,我的朋友。

答案 3 :(得分:0)

使用MYSQLI和fetch_object的Teste:

$host = "localhost";
$user = "user";
$password = "12345";
$database = "database";

try {
    $mysqli = new mysqli($host, $user, $password, $database);
    if (!$mysqli) {
        throw new Exception('Fail to connect to the database!');
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
$mysqli->set_charset("utf8");

/* check connection */
if (mysqli_connect_errno()) {
    printf("No connection: %s\n", mysqli_connect_error());
    exit();
}

    $sql = "SELECT * FROM table";

    $result = $mysqli->query($sql);

    if($result->num_rows > 0)
    {
       while($row = $result->fetch_object())
       {
          $i++;
          echo "line$i $row->field_name";
       }
    }