TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-09-2007, 12:38 AM   #1 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default [PHP/MySQL] Function to return ALL selected rows

Hi, I can't figure this out myself so maybe you've got an answer to my issue.

I want to create a function which returns (as the title says) all selected rows in an array. The next thing I want to do is loop through it and display all rows.

Something like this should do probably:

PHP Code:
public function query($szQuery) {
    
$pResult mysql_query($szQuery);
    while (
$aRow mysql_fetch_array($pResult)) {
        
$aResult[] .= $aRow;
    }
    return 
$aResult;

To enter the query..

PHP Code:
$aResult $pDatabase->query('SELECT * FROM Members'); 
Now my problem, how to loop through all the rows and displaying them?
maZtah is offline  
Reply With Quote
Old 11-09-2007, 12:48 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

You can just use the regular loop structures (foreach, for, while, and so on) since $aResult is just a normal array.

PHP Code:
foreach ($aResult as $aRow)
{
    
// Do something for each row, perhaps
    
echo $aRow['username'], ' smells!';

Salathe is online now  
Reply With Quote
Old 11-09-2007, 01:18 AM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

No, $aResult exists out of Arrays.
PHP Code:
print_r($aResult); 
will echo

Code:
Array ( [0] => Array [1] => Array [2] => Array )
maZtah is offline  
Reply With Quote
Old 11-09-2007, 01:33 AM   #4 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

I may be wrong but i think:
$aResult[] .= $aRow;

Is your problem, it should be:
$aResult[] = $aRow;

But im not sure how much of an effect that will have, Salathe is correct in how he recommends it, but why would you want to add them all to an array and then loop through them AGAIN to access them? Seems a little odd to me, unless you have a certain situation.
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-09-2007, 01:43 AM   #5 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

That will return only the last row, bluesaga.

The point is that I want to output all the results found, without a mysql_fetch part outside the Database class.
maZtah is offline  
Reply With Quote
Old 11-13-2007, 09:03 AM   #6 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Erm, no it won't.

Using array[] means it will create a row in the index incrementing the previous array index.

ie first time its ran:
array[0];

second time:
array[1];

However you can use something like this instead:
PHP Code:
class database
{

  function 
query($szQuery)
  {
     return 
mysql_query($szQuery);
  }

  function 
result($pQuery)
  {
    return 
mysql_fetch_array($pQuery);
  }
}

$db     = new database;
$query $db->query("SELECT * FROM table");

while(
$result $db->result($query))
{
   
print_r($result);

__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design