[SOLVED]How to make array of mysql results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[SOLVED]How to make array of mysql results

Post by Calimero »

When I query the db, I get say 20 results.

What I need is to take all 20 and insert them into one field in the table.

Can this be done trough creating an array and then with FOREACH insert into this array, or is there a better way. Array is then inserted back in another field.

I know to make a query, but don't know part of the code for the above problem.

Thanks ahead!
Last edited by Calimero on Sat Jun 26, 2004 3:07 am, edited 1 time in total.
Noodleman
Forum Newbie
Posts: 5
Joined: Thu Jun 24, 2004 10:26 am

Post by Noodleman »

when you say into the table, do you mean a HTML table.

If you do, you can use a while statement to build the table and insert each record at a time.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Code: Select all

<?php

$query = mysql_query("SELECT * FROM Table WHERE Condition = Rule");

echo "<table>";
while ($result = mysql_fetch_array($query)) {

  echo "<tr><td>" . $result[0] . "</td><td>" . $result[1] . "</td></tr>";
}

echo "</table>";

?>
For html table anyway.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Ok, this way

Post by Calimero »

20 results returned from the DB,
all of them need to be put in an array
and then that array should be put back into the DB/TABLE1/SOMEFIELD

The problem is how to put them in an array - from 20 recors out to make one to put back into the DB.

Serious Brainstorming and Knowledge Needed :D

Thanks Ahead!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

mysql_fetch_array()
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Code: Select all

<?php
$query = mysql_query("SELECT * FROM Table WHERE Condition = Rule"); 

echo "<table>"; 
while ($result = mysql_fetch_array($query)) { 
    $myarr[count($myarr)] = $result
} 

echo "</table>"; 
?>

8)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

the WHERE clause is not need

to my understanding, he/she wants to put the data from one table into an array and put them into another table?

Code: Select all

<?php
$sql = mysql_query("SELECT * FROM table_name");
$result = mysql_fetch_array($sql);

// $result is your variable that holds the array of data from the table.


?>
this what you want?
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Well, No!

Post by Calimero »

:lol:

Ok, here is the Exmple:

Ex of table1:

ID | desc | addinfo |
-------------------------------
1 | first | is the first
2 | second | is second
3 | third | is third
4 | fourth | is fourth
5 | fifth | is boring:)


When I query SQL db with this:

[syntax=php]<?php
$sql = mysql_query("SELECT * FROM table_name");
$result = mysql_fetch_array($sql);
?>[/syntax]

I get 5 results.

NOW the big one !

How do I put all of these 5 results into one array or variable to insert into the table2 to look like this>

Ex of table2:
ID | Column1
---------------------------
1 | 1 first is the first 2 second is
| second 3 third is third 4 fourth
| is fourth 5 fifth is boring:)

all of this (text in the column1) is in one field.

ID column is irrelevant, just to look nice :)

I hope i clarified it now.

And if possible, because the Column1 is of type BLOB, how to preserve/edit the content that I insert - lets say - every record in a new line, but still inside that one field.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Is it possible to make this true ?

Post by Calimero »

And something in the body :)
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

If I'm gathering you correctly you basically have a db result with say 3 columns and a bunch of records. So what you want is to take everything from table one and put it into one big field in table 2?

Like this?

Code: Select all

<?php
$query = mysql_query("SELECT * FROM Table1 ORDER BY ID"); //pull data

$new_data = ''; //creates new blank string that we'll use later

while ($result = mysql_fetch_array($query)) { //puts results from db into array $result
$new_data += $result[1] . " " . $result[2] . " "; //adds each row's data into $new_data
}

mysql_query("INSERT INTO Table2 SET Column1 = '$new_data'"); //creates new row in Table2 with whatever is in $new_data

?>
That will take everything in Table1 (minus the ID since I started with $result[1]) and put it into with $new_data with spaces in between all of the columns, you could use periods, commas, <br> or whatever. So taking your example of table1, you'll have this:

Code: Select all

<?php
$new_data = "first is the first second is second third is third fourth is fourth fifth is boring:)";

?>
And that line will be added to Table2's column1. Is that what you were looking for?

P.S.
My insert query might be off, sorry, just check http://www.mysql.com for proper insert syntax.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

It IS DONE !!!! :)

Post by Calimero »

johnperkins21 wrote:If I'm gathering you correctly you basically have a db result with say 3 columns and a bunch of records. So what you want is to take everything from table one and put it into one big field in table 2?

Like this?

Code: Select all

&lt;?php
$query = mysql_query("SELECT * FROM Table1 ORDER BY ID"); //pull data

$new_data = ''; //creates new blank string that we'll use later

while ($result = mysql_fetch_array($query)) { //puts results from db into array $result
$new_data += $result&#1111;1] . " " . $result&#1111;2] . " "; //adds each row's data into $new_data
}

mysql_query("INSERT INTO Table2 SET Column1 = '$new_data'"); //creates new row in Table2 with whatever is in $new_data

?&gt;
Thanks, BUT however, I found just a little mistake:

Code: Select all

<?php
$new_data += $result[1] . " " . $result[2] . " ";

// this is only for numerical values = adding - integers, here is the correction for alfanumerical characters - joining

$new_data .= $result[1] . " " . $result[2] . " ";

?>
Thanks johnperkins21 very much.

This Finally works :D 8) .
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

ooooooooh that is what you wanted.

Lol for the life of me I couldnt bring my fragile brain to comprehend what you request.

Kudos john
Post Reply