2

PLATFORM:

PHP, mySQL

WHAT I HAVE:

I have a Database table. Within my application, I am able to fetch all the rows. When I am querying the database, I have set the records fetch limit dynamically.

WHAT I AM TRYING TO DO:

I am trying to pull out all the rows of data until the record fetch limit is reached, in a loop. I want to assign these results to another array(in the loop) so that I can access these values via this new array, outside the loop. I need the PHP code to do so. I want to be able to apply the same logic of coding in Javascript. Will that be possible?

EXAMPLE:

//TABLE STRUCTURE
fname   lname    city
Ed       Al      SA
Bob     B       MN
Chris     V       KJ

PHP QUERY:

$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");

while( $row = mysql_fetch_array($result) ) {    

        $new_rows_data['fname'] .= $row['fname'];
        $new_rows_data['lname'] .= $row['lname'];
        $new_rows_data['city']  .= $row['city'];
    }

DESIRED OUTPUT:

echo $new_rows_data['fname'][0].'   '.$new_rows_data['lname'][0].'   '.$new_rows_data['city'][0].
//Want the above to show: Ed        Al      SA
echo $new_rows_data['fname'][1].'   '.$new_rows_data['lname'][1].'   '.$new_rows_data['city'][1].
//Want the above to show: Bob       B       MN
echo $new_rows_data['fname'][2].'   '.$new_rows_data['lname'][2].'   '.$new_rows_data['city'][2].
//Want the above to show: Chris     V       KJ

Thank you in advance.

Devner
  • 6,417
  • 11
  • 58
  • 99

2 Answers2

3

If you change the PHP query bits to...

$new_rows_data['fname'][] = $row['fname'];
$new_rows_data['lname'][] = $row['lname'];
$new_rows_data['city'][]  = $row['city'];

...you should be good to go. (At the moment, you're appending the contents into a single string each time.)

Incidentally, I presume this data is 'known good', as you don't appear to be output escaping it at all. (Cross site scripting is bad, etc.)

John Parker
  • 53,316
  • 11
  • 128
  • 128
  • Thank you for the answer. Any ideas on how can I implement the same in jQuery using the $.each function? – Devner Jan 09 '10 at 13:14
  • @middaparka Thank you for the security alert. As I am fetching the data directly from the database, do I still need to escape it. If so, could you please tell me how? Thanks much! – Devner Jan 09 '10 at 15:05
  • As a general rule, use mysql_real_escape_string when putting data into the database and htmlentities when outputting. That said, this is a deep topic - see http://stackoverflow.com/questions/71328/what-are-the-best-practices-for-avoid-xss-attacks-in-a-php-site – John Parker Jan 09 '10 at 15:47
3

Although middaparka's answer is right, be sure to first declare the individual new arrays before entering the while loop:

$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");

$new_rows_data['fname'] = array();
$new_rows_data['lname'] = array();
$new_rows_data['city'] = array();
while( $row = mysql_fetch_array($result) ) {
    // the following (two brackets []), automatically pushes a value on the end of the array
    $new_rows_data['fname'][] = $row['fname'];
    $new_rows_data['lname'][] = $row['lname'];
    $new_rows_data['city'][]  = $row['city']
}
Community
  • 1
  • 1
Decent Dabbler
  • 21,869
  • 8
  • 72
  • 103
  • +1 He speaks the truth. Whilst PHP will let you do 'lazy initialisation', defining things prior to usage is good practice. – John Parker Jan 09 '10 at 12:11