PrettyHotProgrammer Posted June 24, 2008 Share Posted June 24, 2008 Here is my code... Configuration.php has all the variables for Database Hostname username password... I know the connection works.. <?php include("configuration.php"); if (!($db = mysql_pconnect($hostname, $username , $password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } $result = mysql_query("SELECT * FROM Assignment"); $num_rows = mysql_num_rows($result); $i = 0; echo"<table width="100%" border="0">"; echo"<tr> <td></td> <td>Assignment ID</td> <td>Title</td> <td>School ID</td> <td>Description</td> </tr>"; while($i < $num_rows+1) { $query = "Select * from Assignment where AssnID = $i"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo" <tr> <td>$row[1]</td> <td>$row[2]</td> <td>$row[3]</td> <td>$row[4]</td> </tr> "; } echo"</table>"; ?> Thanks in advanced!! Quote Link to comment Share on other sites More sharing options...
fenway Posted June 24, 2008 Share Posted June 24, 2008 First, don't run two queries... you're not even using the first result. Second, don't refer to columns by their position... that's bad form. Third, what doesn't work? Quote Link to comment Share on other sites More sharing options...
PrettyHotProgrammer Posted June 25, 2008 Author Share Posted June 25, 2008 I will take the first Query out... I am getting a blank white page... Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 25, 2008 Share Posted June 25, 2008 are your column names numbers? If so(which is a horrible idea and might not even work correctly), you need to make your while statement into: while($row = mysql_fetch_array($result)) { stuff with $row[1] and so on } but if your column names are things like id, name, email, etc then you'd have to do $row[id], $row[name], etc Quote Link to comment Share on other sites More sharing options...
PrettyHotProgrammer Posted June 25, 2008 Author Share Posted June 25, 2008 My column names are not numbers they are id schoolit assnid etc. I was doing the numbers as in first thing in the array... 2nd in the array... 3rd and so on. I changed it to the column names and still nothing Quote Link to comment Share on other sites More sharing options...
fenway Posted June 25, 2008 Share Posted June 25, 2008 Let's start again... post the code you have now. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 Ok I have bolded the errors or things you need to fix <?php include("configuration.php"); if (!($db = mysql_pconnect($hostname, $username , $password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } $result = mysql_query("SELECT * FROM Assignment"); $num_rows = mysql_num_rows($result); $i = 0; echo"<table width="100%" border="0">"; echo"<tr> <td>[b]$nbsp;[/b]</td> <td>Assignment ID</td> <td>Title</td> <td>School ID</td> <td>Description</td> </tr>"; while([b]$row = mysql_fetch_array($result)[/b]) { $query = "Select * from Assignment where AssnID = [b]'$i'[/b]"; // you need single quotes around the variable $result = mysql_query($query); ; echo" <tr> [b]<td>$row['column_name']</td> <td>$row['column_name']</td> <td>$row['column_name']</td> <td>$row['column_name']</td> <td>$row['column_name']</td>[/b] </tr> "; [b]$i++[/b] } echo"</table>"; ?> EDIT: well, apparently it won't bold if the text is in code tags. Oh well, look for the [ b] tags Quote Link to comment Share on other sites More sharing options...
fenway Posted June 26, 2008 Share Posted June 26, 2008 How is this different from your initial code? Quote Link to comment Share on other sites More sharing options...
PrettyHotProgrammer Posted June 26, 2008 Author Share Posted June 26, 2008 This is now my code... Still White Screen... I am used to programming in C# where when there is a problem you can step through it and find where it is not doing what it is supposed to... This is the Reason I should stay with my Windows Forms Programming haha <?php include("config.php"); if (!($db = mysql_pconnect($hostname, $username , $password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } $result = mysql_query("SELECT * FROM Assignment"); $num_rows = mysql_num_rows($result); $i = 0; echo"<table width="100%" border="0">"; echo"<tr> <td>$nbsp;</td> <td>Assignment ID</td> <td>Title</td> <td>School ID</td> <td>Description</td> </tr>"; while($row = mysql_fetch_array($result)) { $query = "Select * from Assignment where AssnID = '$i'"; $result = mysql_query($query); echo" <tr> <td>$nbsp;</td> <td>$row['Assignment']</td> <td>$row['Title']</td> <td>$row['SchoolID']</td> <td>$row['Description']</td> </tr> "; $i++ } echo"</table>"; ?> Quote Link to comment Share on other sites More sharing options...
PrettyHotProgrammer Posted June 26, 2008 Author Share Posted June 26, 2008 I went through and did some debugging the old fashion way and it looks like I cannot split up a table in between echo statments Quote Link to comment Share on other sites More sharing options...
fenway Posted June 26, 2008 Share Posted June 26, 2008 You need to run this: $result = mysql_query("SELECT * FROM Assignment"); while($row = mysql_fetch_array($result)) { echo" <tr> <td>$nbsp;</td> <td>$row['Assignment']</td> <td>$row['Title']</td> <td>$row['SchoolID']</td> <td>$row['Description']</td> </tr> "; } Quote Link to comment Share on other sites More sharing options...
PrettyHotProgrammer Posted June 26, 2008 Author Share Posted June 26, 2008 Thank you all for all of you help... I finally figured it out it was a pain but i found it on ScriptPlayground here is the code for future people looking for this!! <?php include("config.php"); if (!($db = mysql_pconnect($hostname, $username , $password))){ die("Can't connect to database server."); }else{ // select a database if (!(mysql_select_db("$database",$db))){ die("Can't connect to database."); } } function display_db_query($query_string, $header_bool, $table_params) { // perform the database query $result_id = mysql_query($query_string) or die("display_db_query:" . mysql_error()); // find out the number of columns in result $column_count = mysql_num_fields($result_id) or die("display_db_query:" . mysql_error()); // Here the table attributes from the $table_params variable are added print("<TABLE $table_params >\n"); // optionally print a bold header at top of table if ($header_bool) { print("<TR>"); for ($column_num = 0; $column_num < $column_count; $column_num++) { $field_name = mysql_field_name($result_id, $column_num); print("<TH>$field_name</TH>"); } print("</TR>\n"); } // print the body of the table while ($row = mysql_fetch_row($result_id)) { print("<TR ALIGN=LEFT VALIGN=TOP>"); for ($column_num = 0; $column_num < $column_count; $column_num++) { print("<TD>$row[$column_num]</TD>\n"); } print("</TR>\n"); } print("</TABLE>\n"); } function display_db_table($tablename, $header_bool, $table_params) { $query_string = "SELECT * FROM $tablename"; display_db_query($query_string,$header_bool, $table_params); } ?> <TABLE border="2"> <TR> <TD> <?php //In this example the table name to be displayed is static, but it could be taken from a form $table = $assignmentTable; display_db_table($table, $global_dbh, TRUE, "border='0'"); ?> </TD> </TR> </TABLE> </body> </html> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 26, 2008 Share Posted June 26, 2008 There was 2 errors in the code I gave. one was you need to remove the single quotes in the $row array when it's been put in double quotes. Second was remove the query that was called within the while statement. That should do it. I'm sorry you had to find the script somewhere and that it works... bc that means you probably have no idea why your previous code wasnt working. not a good way to solve problems Quote Link to comment Share on other sites More sharing options...
fenway Posted June 26, 2008 Share Posted June 26, 2008 Exactly. Quote Link to comment 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.