1

I have the following code. What I have labelled block 1 and block 2 don't seem to work together but one does if i get rid of the other; when checking using print_r. I am fairly new to php and was wondering if somebody could point out where I went wrong. I had it working somewhere else before but lost the file. Many thanks in advance.

P.S: I am aware it is a good idea to get into PDO and mysqli sooner rather than later. But I just want to get to grips with the basics first.

<?php


//Connect to database

$connect = mysql_connect ("localhost","root","password");
$db = mysql_select_db("project");


//Find top 100 most popular images

$pop = mysql_query("


SELECT * FROM users ORDER BY pop DESC LIMIT 2

");

//Define local variables for images to show

//block 1//

$images = array();
while ($row = mysql_fetch_array($pop)) {
    $images[] = $row['image'];
    }

//block2//

$links = array ();
while ($row = mysql_fetch_array($pop)){
$links[] = $row['username'];
}

?>
aLearner
  • 1,051
  • 14
  • 30
Roy
  • 49
  • 6
  • 2
    You are not doing any error checking so it's no wonder you're not getting any meaningful error messages when things go wrong. See the manual on mysql_query for an example how to properly do it: http://php.net/mysql_query – Pekka Dec 08 '12 at 12:05

4 Answers4

4

mysql_data_seek will work:

$images = array();
while ($row = mysql_fetch_array($pop)) {
    $images[] = $row['image'];
}

mysql_data_seek( $pop, 0 );

$links = array ();
while ($row = mysql_fetch_array($pop)){
    $links[] = $row['username'];
}

However, a better / cleaner solution to your problem would be to put both values into their respective arrays during the first loop:

$links = array();
$images = array();
while ($row = mysql_fetch_assoc($pop)) {
    $images[] = $row['image'];
    $links[] = $row['username'];
}

Or, even cleaner - add an array to your array:

$avatars = array();
while ($row = mysql_fetch_assoc($pop)) {
    array_push(
        $avatars, 
        array('image' => $row['image'], 'username' => $row['username'])
    );
}

var_dump($avatars);
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
1

First off, Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Try with mysql_data_seek($pop, 0) between the two while loops or replace your code with this:

$images = array();
$links = array();
while ($row = mysql_fetch_array($pop)) {
    $images[] = $row['image'];
    $links[] = $row['username'];
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
jacoz
  • 3,508
  • 5
  • 26
  • 42
  • Thanks. ill give it ago. You mean to replace mysql_fetch_array with "mysql_data_seek">? – Roy Dec 08 '12 at 12:09
0

You've already iterated over the entire result set in the first block, you need to re-seek the pointer to the begining of the the result set between the two blocks of code by doing:

mysql_data_seek($pop, 0);
shannonman
  • 841
  • 4
  • 8
-2

Wrong syntax.

while ($row = mysql_fetch_array($pop, MYSQL_ASSOC))
  • `MYSQL_BOTH` is assumed by default when result_type is unspecified, which is both numerically indexed and associative. See [**`mysql_fetch_array`**](http://php.net/manual/en/function.mysql-fetch-array.php) – Anirudh Ramanathan Dec 08 '12 at 12:18
  • Call me a sucker for faster code which uses less memory over lazy programming. – Chevy372 Dec 08 '12 at 12:31
  • No. The syntax isn't wrong. Your answer says *Wrong syntax*, which isn't true at all. Also, the issue lies elsewhere (as you can see from the other answers). – Anirudh Ramanathan Dec 08 '12 at 12:43
  • Well, the issue is incorrect syntax in my opinion, and the other answers are overly complicated. I don't understand why only one other person said to just populate both arrays in the same while loop. No need for redundant code. – Chevy372 Dec 08 '12 at 12:51
  • That isn't the issue. Please read the first line of my answer. Populating the two in the same loop makes a difference, not just to performance, but correctness as well. – Anirudh Ramanathan Dec 08 '12 at 12:53