[SOLVED]How to make array of mysql results
Moderator: General Moderators
[SOLVED]How to make array of mysql results
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!
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.
- johnperkins21
- Forum Contributor
- Posts: 140
- Joined: Mon Oct 27, 2003 4:57 pm
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>";
?>Ok, this way
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!
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!
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>";
?>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?
this what you want?
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.
?>Well, No!
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 ?
And something in the body 
- johnperkins21
- Forum Contributor
- Posts: 140
- Joined: Mon Oct 27, 2003 4:57 pm
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?
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:
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.
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
?>Code: Select all
<?php
$new_data = "first is the first second is second third is third fourth is fourth fifth is boring:)";
?>P.S.
My insert query might be off, sorry, just check http://www.mysql.com for proper insert syntax.
It IS DONE !!!! :)
Thanks, BUT however, I found just a little mistake: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 ?>
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] . " ";
?>This Finally works