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.

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer. Any ideas on how can I implement the same in jQuery using the $.each function?
@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!
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 stackoverflow.com/questions/71328/…
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']
}

1 Comment

+1 He speaks the truth. Whilst PHP will let you do 'lazy initialisation', defining things prior to usage is good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.