Dragen Posted April 8, 2007 Share Posted April 8, 2007 How do I take values from a table as an array? I have a table with day, month and year as headers for it and want to read them as an array like this: $booked = array("day", "month", "yeear"); Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/ Share on other sites More sharing options...
Barand Posted April 9, 2007 Share Posted April 9, 2007 <?php $sql = "SELECT day, month, year FROM mytable"; $res = mysql_query($sql); $booked = array(); while ($row = mysql_fetch_assoc($res)) { $booked[] = $row; } ?> Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-224583 Share on other sites More sharing options...
Dragen Posted April 9, 2007 Author Share Posted April 9, 2007 Thanks! I knew it was something simple like that. Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-224584 Share on other sites More sharing options...
Dragen Posted April 9, 2007 Author Share Posted April 9, 2007 um.. Shouldn't mysql_fetch_assoc($res)) be mysql_fetch_array($res)) ?? Also, I'm sure I'm just being stupid here, but it brings up a blank screen which I presume is because I'm not echoing it. So I tried to echo by using: while ($row = mysql_fetch_assoc($res)) { $booked[] = $row; echo $booked; } but that just repeated the words 'array' three times. The same amount of entries in the table.. As I said, I'm sure it's just me being silly.. Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-224598 Share on other sites More sharing options...
Barand Posted April 9, 2007 Share Posted April 9, 2007 Instead of echo, use print_r() for arrays (works better inside pre /pre tags echo '<pre>', print_r($booked, true), '</pre>'; Then try it with mysql_fetch_array Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-224823 Share on other sites More sharing options...
Dragen Posted April 9, 2007 Author Share Posted April 9, 2007 okay thanks. When I change it to print with the <pre> attribute it displays properly, although it seems to be reading from the databse a bit oddly. What I get is: Array ( [0] => Array ( [day] => 2 [month] => 1 [year] => 2007 ) ) Array ( [0] => Array ( [day] => 2 [month] => 1 [year] => 2007 ) [1] => Array ( [day] => 12 [month] => 11 [year] => 2007 ) ) Array ( [0] => Array ( [day] => 2 [month] => 1 [year] => 2007 ) [1] => Array ( [day] => 12 [month] => 11 [year] => 2007 ) [2] => Array ( [day] => 4 [month] => 6 [year] => 2008 ) ) for some reason the code bit above hasn't displayed properly.. Which means that for each array it reads the previous rows as well and adds the next one to the end.. What I'm wanting is a list of arrays that I call call up to check some info. So I think I'd need it to output something like: Array ( [0] => Array ( [day] => 2 [month] => 1 [year] => 2007 ) [1] => Array ( [day] => 12 [month] => 11 [year] => 2007 ) [2] => Array ( [day] => 4 [month] => 6 [year] => 2008 ) ) Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-224890 Share on other sites More sharing options...
Barand Posted April 9, 2007 Share Posted April 9, 2007 try <?php $sql = "SELECT day, month, year FROM mytable"; $res = mysql_query($sql); $booked = array(); while ($row = mysql_fetch_assoc($res)) { $booked[] = $row; } echo '<pre>', print_r($booked, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-225386 Share on other sites More sharing options...
Dragen Posted April 9, 2007 Author Share Posted April 9, 2007 ah. just needed to move the echo. okay. Thanks Link to comment https://forums.phpfreaks.com/topic/46196-getting-array-from-table/#findComment-225395 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.