user758287
user758287

Reputation: 137

This php code is only picking up the first row in my database. Help?

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

Upvotes: 1

Views: 293

Answers (4)

msmafra
msmafra

Reputation: 1714

Teste with MYSQLI and fetch_object:

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

Upvotes: 0

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

Read this small example I handpicked for you. Read it and look at it closely until you understand how it gets the table printed. This will help you do the script you posted in a more concise and clear manner.

Happy coding, my friend.

Upvotes: 0

mario
mario

Reputation: 145472

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).

Upvotes: 1

Dennis Kreminsky
Dennis Kreminsky

Reputation: 2089

As the documentation states,

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

One row, you see.

Upvotes: 0

Related Questions