patsypoopa Posted April 18, 2008 Share Posted April 18, 2008 Hi I'm a php newbie and am currently stuck with a problem on an assignment i've been given. The problem is that I am struggling to display columns and records from a table. The thing thats confusing is that due to the nature of my site every table is different so I cannot display columns just by saying their names and using col["bleh"] . I've managed to get the code so I can display 1 record from the table but I cat get it to display more than one. I'm convinced I've made a simple mistake but i've been staring at this all day and am now brain dead. Thanks either way <? $i = 0; $j = 0; do{ echo "<table>"; while ($row = mysql_fetch_array($result_rec, MYSQL_NUM)) { while ($i < mysql_num_rows($result_col)) { $col[$i] = mysql_tablename($result_col, $i); echo( "<tr><td><b>" . $col[$i] . ":</b></td><td>" . $row[$i] . "</td></tr>"); $i++; } } echo "</table>"; $j++; }while ($j < mysql_num_rows($result_rec)); Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/ Share on other sites More sharing options...
atl_andy Posted April 18, 2008 Share Posted April 18, 2008 Where is your select statement? Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/#findComment-520552 Share on other sites More sharing options...
patsypoopa Posted April 18, 2008 Author Share Posted April 18, 2008 Sorry I must of cut it when copying and pasting: $sql = "SELECT * FROM $chosencollection"; $result_rec = mysql_query($sql); $sql_col = "SHOW COLUMNS FROM $chosencollection"; $result_col = mysql_query($sql_col); Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/#findComment-520555 Share on other sites More sharing options...
atl_andy Posted April 19, 2008 Share Posted April 19, 2008 Here is a good tutorial on displaying results with headers from the column name. I don't think you need a separate statement to show the column names, as shown in the tutorial. http://www.anyexample.com/programming/php/php_mysql_example__display_table_as_html.xml Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/#findComment-521302 Share on other sites More sharing options...
atravotum Posted April 19, 2008 Share Posted April 19, 2008 I feel your pain I was writing a program that did a similar function. What I ended up doing was writing an entire relay script to interact with my databases. This is the function i setup to pull data out of tables dynamically. // get - retrieve data from the database. Gets stored to a singe array. function f_get_all($db_name, $db_user, $db_pword, $db_table) { /// Connect To Database $con = mysql_connect($db_name, $db_user, $db_pword, $col); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db_user, $con); //Pull the data from the database, and store it to an array. $result=mysql_query("SELECT * FROM $db_table"); $i = 0; while($rows = mysql_fetch_array($result)) { for($j = 0; $j<$col; $j++) { $rows_temp[$i][$j] = $rows[$i][$j]; } $i++; } // Disconnect from database. mysql_close($con); //Return the array return $rows_temp; } //This is the call to the function, which stores data in a multi-dimensional array $rows = f_get_all($db_name, $db_user, $db_pword, $db_table); That should pull all fields out of the database and store it to a multi-dimensional array. You then run a loop to count how many rows, and how many columns the array has. Set up an (i,j) for loop to push them all to a table. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/#findComment-521318 Share on other sites More sharing options...
patsypoopa Posted April 19, 2008 Author Share Posted April 19, 2008 Thanks, problems solved. I used the tutorial atl_andy sent and it worked perfect. Thanks anyway tho atravotum! Link to comment https://forums.phpfreaks.com/topic/101743-solved-php-newbie-stuck-with-displaying-seperate-sets-of-data-from-sql-database/#findComment-521378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.