|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have a problem in retrieving array in php from MySQL database.
<?php $host='localhost'; $database='npi'; $password=''; $username='root'; $connect=mysql_connect($host, $username, $password); mysql_select_db($database); $colors=serialize($_POST['colors']); //takes the data from a post operation... $name=$_POST['name']; //$query = "INSERT INTO colors VALUES('".$colors."','".$name."') "; $size = sizeof($colors); //$result=mysql_query($query, $connect) or die ("Cannot execute 1"); for($c = 0; $c < sizeof($colors); $c++) { $query = "INSERT INTO colors VALUES('".$colors."','".$name."') "; $result = mysql_query($query) or die(mysql_error()); } $query2="SELECT Color FROM Colors WHERE Name = '$name' "; $result2=mysql_query($query2, $connect) or die ("Cannot execute 2"); $numrows=mysql_num_rows($result2); if($result2){ while($my_colors=mysql_fetch_array($result2)) { $my_colors=unserialize($colors); print "<table border=\"1\">"; foreach($my_colors as $shirt) { print "<tr><td>$shirt</td><tr>"; } } print " and $name"; } else { print 'No colors in database.'; } ?> I run the code successfully. But my problem is: $my_colors=unserialize($colors); <<--- I can do this if $colors was in the same page where I can serialize and unserialize; But if I want to retrieve from database where there is no $colors at the same page, how can I write the code? More addition, how can I actually retrieve an array from MySQL database? Can u explain and give example how can i wrote the code? Can u give me any useful links? another question: what should i do if i wrote this code(for instance): $my_colors=unserialize($colors); the output is array instead of a:2:{i:0;s:3:"SPD";i:1;s:4:"SPD2";} what should i do to ensure that i just display SPD and SPD2 instead of array or a:2:{i:0;s:3:"SPD";i:1;s:4:"SPD2";}. Help2!! ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Array in PHP and MySQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|