2 Replies - 726 Views - Last Post: 12 March 2012 - 04:04 PM Rate Topic: -----

#1 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

[SOLVED] Mysql_fetch_row() only returns first match

Posted 12 March 2012 - 03:24 PM

Hi!

I'm fetching a list of "favorites" from a MySQL database using PHP. Unfortunately, only the first result of the database is returned... I'd appreciate if anyone would have a look:

I have the following code:
if($userid != 0){
      $favq = mysql_query(
          "SELECT storyid 
           FROM storyfavorites 
           WHERE userid ='$userid'",$conn) 
      or die(mysql_error());
     
      $fav = mysql_fetch_row($favq);
}



Querying the following table in the database:

Quote

| userid | storyid |
| 29 | 144 |
| 29 | 143 |
| 29 | 130 |
| 29 | 142 |
| 29 | 140 |
| 29 | 141 |
| 29 | 36 |


When I query the database using the PHP code above, I get this in return (vardump):

Quote

array(1) { [0]=> string(3) "144" }

As you can see, that's only the first row of the table. HOWEVER, when I query the database directly WITHOUT the PHP code, I get all the correct values returned.

It's late and I'm probably missing something very simple, but I'd appreciate if anyone would help me fix it.

Thanks ;)
- Cbeppe

P.S the PHP variable '$userid' correctly equals 29. Checked using var_dump();

This post has been edited by Cbeppe: 12 March 2012 - 04:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] Mysql_fetch_row() only returns first match

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,239
  • Joined: 08-June 10

Re: [SOLVED] Mysql_fetch_row() only returns first match

Posted 12 March 2012 - 03:27 PM

View PostCbeppe, on 12 March 2012 - 11:24 PM, said:

It's late and I'm probably missing something very simple,

something like

The PHP Manual said:

Description

array mysql_fetch_row ( resource $result )

Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.

?

This post has been edited by Dormilich: 12 March 2012 - 03:28 PM

Was This Post Helpful? 1
  • +
  • -

#3 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Re: [SOLVED] Mysql_fetch_row() only returns first match

Posted 12 March 2012 - 04:04 PM

Exactly! That's what a lack of sleep does to one's brain...

Thanks Dormilich!

To anyone else with the same problem, this is one possible solution:
// Define the array that you want to get out
$favorites = array();

// Query the database if the User ID is set
if($userid != 0){

      $favq = mysql_query(
           "SELECT storyid 
            FROM storyfavorites 
            WHERE userid ='$userid'"
      ,$conn) or   die(mysql_error());

// Construct the array using the results of your query
     while($fav = mysql_fetch_row($favq)){
          $favorites[] = $fav[0];
     }
}

return $favorites;


This post has been edited by Cbeppe: 12 March 2012 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1