这段 php 代码只选取了我数据库中的第一行.帮助? [英] This php code is only picking up the first row in my database. Help?

查看:49
本文介绍了这段 php 代码只选取了我数据库中的第一行.帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<tr>';while($pic = mysql_fetch_array($pic1)){if($cell % 4 == 0){回声'</tr><tr>';}{回声'<td><a href="/' . $link["link"] . '"><div class="image"><img src="' . $pic["pic"] . '"alt="' . $alt["alt"] . '"height="' . $height["height"] .'"width="' . $width["width"] . '"/>

</a><div class="timeago"><abbr class="timeago" title="' . $time["time"] .'"></abbr>&nbsp;在 <a href="/' . $folder["folder"] . '"><span class="filedat">'.$filed["filed"] .'</div></a>

</td>';}$细胞++;}回声'</tr></table></div>';?>

我是个菜鸟,我不知道为什么这段代码只选取了我的 mysql 数据库中的第一行.这就是我的意思.我的 phpmyadmin 数据库看起来像:

thumbnailID |链接 |图片 |替代 |时间 |身高|宽度 |文件夹|归档1 博客 random.png 描述 3:45 300 200 emm weewr2 about etc.png desc 4:15 130 150 wer ewrre3 misc er.png desc 2:30 324 435 sdf dcv4 misc etc.png 信息 6:50 203 034 sdf qwd5 关于 meh.png 哇 10:12 395 234 tb asd

如您所见,有五个不同的行.但是由于某种原因,第 2、3、4 和 5 行都具有与第 1 行相同的链接、alt、时间高度、宽度、文件夹和文件.唯一不同的是图片.

因为如果这听起来令人困惑,但我不知道如何用其他方式表达.

解决方案

您正在发布多个 mysql_query 请求,它总是将指针重置为第一行.但是 mysql_fetch_array 将在循环中使用.它还查询行,而不是按列查询.

$result = mysql_query("SELECT * FROM thumbs");而 ($row = mysql_fetch_assoc($result)) {打印 " <td> $row[link] + $row[pic] + $row[alt] ";//这里只使用循环变量,而不是静态请求数组//从以前}

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

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

I'm a noobie, and I have no idea why this code is only picking up the first row in my mysql database. Here's what I mean. My phpmyadmin db looks like:

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

So as you can see, there is five different rows. But for some reason, row 2,3,4, and 5, all have the same link, alt, time height, width, folder, and filed, as row 1. The only thing different is the pic.

For if this sounds confusing, but I don't know how to put it in any other way.

解决方案

You are issuing multiple mysql_query requests, which always resets the pointer to the first row. But mysql_fetch_array is to be used in a loop. It also queries for rows, not column-wise.

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

Your problem is that you are using previous result arrays $link[], $pic[], $alt[], $width[], $height[] for your output code - where you should be using just $row (or $pic in your code).

这篇关于这段 php 代码只选取了我数据库中的第一行.帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆