timmah1 Posted June 1, 2009 Share Posted June 1, 2009 I want to get each line in a different variable. This what I tried, and I'm getting no results This is for a database that the admin can add custom fields to forms <?php $result=mysql_query('SELECT * FROM `main`'); for ($n=1;$row=mysql_fetch_array($result);$n++) { $var='variable'.$n; $var=$row['columnname']; //replace columnname with real column name } echo $va[$n]r; ?> How can I do this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/ Share on other sites More sharing options...
jxrd Posted June 1, 2009 Share Posted June 1, 2009 If I understand what you're asking, you want to place the results into an array? $array = array(); while($row = mysql_fetch_array($result)) { $array[] = $row['column']; } Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846820 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 So essentially this <?php require("config.php"); $result=mysql_query('SELECT * FROM `main`'); $array = array(); while($row = mysql_fetch_array($result)) { echo $array[] = $row['column']; } ?> I'm still not getting any results, and right now there are 15 fields in the database, I don't understand why? Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846826 Share on other sites More sharing options...
jxrd Posted June 1, 2009 Share Posted June 1, 2009 What? What are you trying to do? If you want each result in an array, do this: require("config.php"); $result=mysql_query('SELECT * FROM `main`'); $array = array(); while($row = mysql_fetch_array($result)) { $array[] = $row['column']; } print_r($array); Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846845 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 ok, I'm trying to make a contact database for a friend. He wants to store information , kinda like a rolodex. He wants to be able to add custom fields to the database, not just your standard name, address, phone, etc.. I need to be able to pull all of the custom field names, as well the data out to view it, and possibly edit it. So, right now, in the database, these are the fields he has in there now group1 first middle last title company birthday Since I will not know the field names, how can I call of the fields, with their data, from the database to view them? I hope that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846854 Share on other sites More sharing options...
KevinM1 Posted June 1, 2009 Share Posted June 1, 2009 So essentially this <?php require("config.php"); $result=mysql_query('SELECT * FROM `main`'); $array = array(); while($row = mysql_fetch_array($result)) { echo $array[] = $row['column']; } ?> I'm still not getting any results, and right now there are 15 fields in the database, I don't understand why? Your echo statement makes no sense. You're trying to output an assignment. Try: $array = array(); $result = mysql_query("SELECT * FROM `main`"); $count = 1; while($row = mysql_fetch_assoc($result)) { $array[] = $row['column']; echo "Row $count value: {$array[$count - 1]}<br />\n"; $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846856 Share on other sites More sharing options...
KevinM1 Posted June 1, 2009 Share Posted June 1, 2009 ok, I'm trying to make a contact database for a friend. He wants to store information , kinda like a rolodex. He wants to be able to add custom fields to the database, not just your standard name, address, phone, etc.. I need to be able to pull all of the custom field names, as well the data out to view it, and possibly edit it. So, right now, in the database, these are the fields he has in there now group1 first middle last title company birthday Since I will not know the field names, how can I call of the fields, with their data, from the database to view them? I hope that makes sense Will there ever be a finalized database? Or will he be adding fields indefinitely? Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846858 Share on other sites More sharing options...
trq Posted June 1, 2009 Share Posted June 1, 2009 I would be more inclined to simply have 3 fields in the database. An id, a key and a value, then you would simply do something like. <?php if ($result = mysql_query("SELECT k, v FROM tbl")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result) { echo "{$row['k']} = {$row['v']}\n"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846862 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 Will there ever be a finalized database? Or will he be adding fields indefinitely? I'm really not sure Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846864 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 Nightslyr,doing this <?php require("config.php"); $array = array(); $result = mysql_query("SELECT * FROM `main`"); $count = 1; while($row = mysql_fetch_assoc($result)) { $array[] = $row['column']; echo "Row $count value: {$array[$count - 1]}<br />\n"; $count++; } ?> Returns this Row 1 value: So nothing is being shown Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846871 Share on other sites More sharing options...
trq Posted June 1, 2009 Share Posted June 1, 2009 Nightslyr,doing this <?php require("config.php"); $array = array(); $result = mysql_query("SELECT * FROM `main`"); $count = 1; while($row = mysql_fetch_assoc($result)) { $array[] = $row['column']; echo "Row $count value: {$array[$count - 1]}<br />\n"; $count++; } ?> Returns this Row 1 value: So nothing is being shown Because you have no field called column! Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846873 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 I'm probably not explaining properly. How can I list all of the 'Field' names that are in the database 'main'? Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846878 Share on other sites More sharing options...
trq Posted June 1, 2009 Share Posted June 1, 2009 I still think your easiest solution is to go with my 3 field approuch. That way your user can add as many different key / value pairs without ever needing to alter the tables structure. Anyway, if you want field names try.... <?php require("config.php"); $array = array(); $result = mysql_query("SELECT * FROM `main`"); $count = 1; while($row = mysql_fetch_assoc($result)) { foreach ($row as $k => $v) echo "{$k} = {$v} "; } echo "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846884 Share on other sites More sharing options...
timmah1 Posted June 1, 2009 Author Share Posted June 1, 2009 That's exactly what I needed. Thank you Thorpe. I might just try what you said with the 3 field approch, sounds easier. Thank you everyone for your help Quote Link to comment https://forums.phpfreaks.com/topic/160472-solved-fetching-variables/#findComment-846889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.