Page 1 of 1
[SOLVED]How to make array of mysql results
Posted: Thu Jun 24, 2004 10:30 am
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!
Posted: Thu Jun 24, 2004 10:39 am
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.
Posted: Thu Jun 24, 2004 10:45 am
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.
Ok, this way
Posted: Thu Jun 24, 2004 2:19 pm
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
Thanks Ahead!
Posted: Thu Jun 24, 2004 2:21 pm
by tim
mysql_fetch_array()
Posted: Thu Jun 24, 2004 2:22 pm
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>";
?>

Posted: Thu Jun 24, 2004 2:26 pm
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?
Well, No!
Posted: Thu Jun 24, 2004 4:28 pm
by Calimero
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.
Is it possible to make this true ?
Posted: Fri Jun 25, 2004 1:37 am
by Calimero
And something in the body

Posted: Fri Jun 25, 2004 11:02 am
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.
It IS DONE !!!! :)
Posted: Sat Jun 26, 2004 3:06 am
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
<?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
?>
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

.
Posted: Sat Jun 26, 2004 7:28 am
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