Author Topic: PHP mySQL does not return an array  (Read 873 times)

Offline dbest

  • Game Owner
  • Level 20
  • *
  • Posts: 211
  • Reputation: +3/-0
    • View Profile
    • Tennis Masters
PHP mySQL does not return an array
« on: October 26, 2009, 07:41:38 AM »
Ok here is another PHP - mySQL related pain area...

This is my code....
Code: [Select]
$leagueids = array();
$query = "SELECT idleague FROM league WHERE idcountry = 1 ORDER BY nextleagueid ASC";
$r = mysql_query($query);
while ($row = mysql_fetch_row($r)) {
           $leagueids = $row[0];
}

print_r($leagueids);

My table consists of 4 rows numbered 1,2,3,4 with idcountry = 1. The print_r statement however prints only 1 value instead of the 4. Again the query works fine in phpMyAdmin, so I suppose its only a PHP issue.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: PHP mySQL does not return an array
« Reply #1 on: October 26, 2009, 07:52:19 AM »
Well, just looks closely and literally at what you wrote... on row 5 you rewrite whole variable with an array

Code: [Select]
$leagueids[] = $row[0];
Plus... wouldn't it be better to use id_league, id_country, next_league_id or nextLeagueId and $league_ids or something...this way it's difficult to read... at first I thought it's something french :) (the array; I don't speak french, just it seemed to me that way)

EDITED!
« Last Edit: October 26, 2009, 08:01:48 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: PHP mySQL does not return an array
« Reply #2 on: October 26, 2009, 08:07:01 AM »
You have trouble with basic array operations, I would revise PHP knowledge first. These will haunt you over and over again till you learn them by heart.

Short fix:
$leagueids .= $row[0];
or
$leagueids = $leagueids.$row[0];

Offline dbest

  • Game Owner
  • Level 20
  • *
  • Posts: 211
  • Reputation: +3/-0
    • View Profile
    • Tennis Masters
Re: PHP mySQL does not return an array
« Reply #3 on: October 26, 2009, 08:21:17 AM »
Thanks Nox and Chris for your help. I admit to being very new to PHP and learning as I go along, maybe not learning in the right fashion, but still learning a few bits here and there.

I will test the solutions provided. It should work. I didnt know about the "." operator, but something new to learn. I am more used to methods provided by Nox.

Also, my naming convention not the best eh? It gets me confused all the time.. :(

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: PHP mySQL does not return an array
« Reply #4 on: October 26, 2009, 08:26:40 AM »
The "." operator is for concatenation. Personally, I always include spaces around this operator simply because in many other languages it is the property accessor (-> in PHP) so I try not to get it confused. ;)
Idiocy - Never underestimate the power of stupid people in large groups.


Offline dbest

  • Game Owner
  • Level 20
  • *
  • Posts: 211
  • Reputation: +3/-0
    • View Profile
    • Tennis Masters
Re: PHP mySQL does not return an array
« Reply #5 on: October 26, 2009, 08:41:08 AM »
Just to confirm that it works. I added the [] after the $leagueids variable.


Offline pavansss91

  • Level 18
  • *
  • Posts: 185
  • Reputation: +1/-0
    • View Profile
Re: PHP mySQL does not return an array
« Reply #6 on: October 27, 2009, 06:12:06 AM »
Dude, I suggest you to use some mysql db class for php.
instead of writing 5 lines to get a query into rows, the mysql classes have a single function.
Also they prevent mysql injection which is also important thing to consider.

The simplest single file mysql classes , i have though as useful are:
1) XCDB class (By Codestryke: Available in forums)
2) Sundb class (By me: Link to sourceforge in my signature)

Although i copied the main function from codestryke's class but i have added many utilities to it and i corrected many bugs in the 3rd version.
bbgFramework v0.1.3
Sun Database Class v0.3

Offline dbest

  • Game Owner
  • Level 20
  • *
  • Posts: 211
  • Reputation: +3/-0
    • View Profile
    • Tennis Masters
Re: PHP mySQL does not return an array
« Reply #7 on: October 27, 2009, 08:35:55 AM »
I am sanitizing input before I receive it and also performing output sanitization.... I would prefer to not use another db class, as it would be something new for me. I am a bit more comfortable with the normal sql statements.

Maybe sometime in the near future, I will consider using the db classes.

Thats also a reason why I am not using any PHP frameworks. Once I get learn more of PHP, then I will consider moving onto a framework, which I supposed should have classes to handle db related stuff.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal