How to save arrays in MySQL with PHP?
-
-
Answer:
If you are using the native mysql drivers in PHP, then there are no MySQL arrays, you are creating an object that you iterate over. The idea is to iterate over the object's results and put those results into an array. Observe: <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> The line you are looking for is: while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) Basically, mysql_fetch_array will return an associtive array containing the column name as the key, and the data for that column into $line. Then you can further push that into an array container. var $data = array(); ... $data[] = $line; ... You cannot otherwise take a mysql result and make it an array, you must iterate over the object. FYI - I do not reccomend using the native PHP drivers. Using an abstraction library makes your code so much more portable. Using native drivers may make for quick coding, but you loose so much more in the end. I've included links to Pear::DB, ADODB, and PDO as possible libraries to look at. There are more, these are just examples. Hope that helps.
Elijah at Yahoo! Answers Visit the source
Other answers
If you are using the native mysql drivers in PHP, then there are no MySQL arrays, you are creating an object that you iterate over. The idea is to iterate over the object's results and put those results into an array. Observe: <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Printing results in HTML echo "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?> The line you are looking for is: while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) Basically, mysql_fetch_array will return an associtive array containing the column name as the key, and the data for that column into $line. Then you can further push that into an array container. var $data = array(); ... $data[] = $line; ... You cannot otherwise take a mysql result and make it an array, you must iterate over the object. FYI - I do not reccomend using the native PHP drivers. Using an abstraction library makes your code so much more portable. Using native drivers may make for quick coding, but you loose so much more in the end. I've included links to Pear::DB, ADODB, and PDO as possible libraries to look at. There are more, these are just examples. Hope that helps.
Mutant
If I understand your question correctly, you want to store the content of a php array in a MySQL database. You will need to create a MySQL table with columns that represent the contents of your php array. I would recommend using phpmysqladmin to do this. Then you will need to make several MySQL insert calls to insert the data into the table. You will need the following php calls: mysql_connect mysql_select_db then one or more mysql_query calls using an argument of the form: "INSERT INTO <table> (<col1>, <col2>...) VALUES (<val1 for col1>, val1 for col2>...), (<val2 for col1>,<val2 for col2>...)...;" then mysql_close.
Steve
If I understand your question correctly, you want to store the content of a php array in a MySQL database. You will need to create a MySQL table with columns that represent the contents of your php array. I would recommend using phpmysqladmin to do this. Then you will need to make several MySQL insert calls to insert the data into the table. You will need the following php calls: mysql_connect mysql_select_db then one or more mysql_query calls using an argument of the form: "INSERT INTO <table> (<col1>, <col2>...) VALUES (<val1 for col1>, val1 for col2>...), (<val2 for col1>,<val2 for col2>...)...;" then mysql_close.
Steve
Store the "array" WITH A SPACE BEFORE THE FIRST ITEM AND AFTER THE LAST ITEM. Then you can use LIKE ' 73 '
Helen
Related Q & A:
- How to completely backup all mysql databases?Best solution by Server Fault
- How to prevent duplicate entries in MySQL and PHP?Best solution by Stack Overflow
- How to do a local mysql database replication on an online server?Best solution by howtoforge.com
- How do you create temporary MySQL table in SQLAlchemy?Best solution by Stack Overflow
- How to save journal as draft in PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.