vomitbomb Posted May 8, 2008 Share Posted May 8, 2008 I am starting to learn some php and have installed XAMPP to get Mysql and php running. I've created a database and I am trying to access the records with the following program I got from a tutorial book. <html> <head> <title>Show Heros</title> </head> <body> <h1>Show Heros</h1> <? //make the database connection $conn = mysql_connect("localhost", "", ""); mysql_select_db("chapter7", $conn); //create a query $sql = "SELECT * FROM hero"; $result = mysql_query($sql, $conn); print "<table border = 1>\n"; //get field names print "<tr>\n"; while ($field = mysql_fetch_field($result)){ print " <th>$field->name</th>\n"; } // end while print "</tr>\n\n"; //get row data as an associative array while ($row = mysql_fetch_assoc($result)){ print "<tr>\n"; //look at each field foreach ($row as $col=>$val){ print " <td>$val</td>\n"; } // end foreach print "</tr>\n\n"; }// end while print "</table>\n"; ?> </body> </html> The problem is when I run this in my browser (firefox) the print statement seems to be causing me problems shown below. Show Heros \n"; //get field names echo "\n"; while ($field = mysql_fetch_field($result)){ echo " $field->name\n"; } // end while print "\n\n"; //get row data as an associative array while ($row = mysql_fetch_assoc($result)){ echo "\n"; //look at each field foreach ($row as $col=>$val){ echo " $val\n"; } // end foreach echo "\n\n"; }// end while echo "\n"; ?> Anyone got any idea's how I can fix this? haven't I configured php properly or something? Pretty annoying when the tutorials themselves don't work :-\ Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/ Share on other sites More sharing options...
conker87 Posted May 8, 2008 Share Posted May 8, 2008 Use <?php not <? to open the script. Looks like that short tags are not enabled on your server (which is good thing IMHO). Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535818 Share on other sites More sharing options...
vomitbomb Posted May 8, 2008 Author Share Posted May 8, 2008 Unfortunately that didn't make a difference Thanks for the help though. Any other idea's? Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535820 Share on other sites More sharing options...
vomitbomb Posted May 8, 2008 Author Share Posted May 8, 2008 Ok I've just tried this with other code and it seems to be printing half of it straight to the screen rather than executing it. in IE the entire code is displayed. Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535826 Share on other sites More sharing options...
vomitbomb Posted May 8, 2008 Author Share Posted May 8, 2008 ok stupid problem and I fixed it, nevermind Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535828 Share on other sites More sharing options...
conker87 Posted May 8, 2008 Share Posted May 8, 2008 Did you not save it as .php? *giggle* Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535833 Share on other sites More sharing options...
vomitbomb Posted May 8, 2008 Author Share Posted May 8, 2008 had it in the wrong folder Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535887 Share on other sites More sharing options...
revraz Posted May 8, 2008 Share Posted May 8, 2008 Not sure how that would make a difference but glad you figured it out. Mark as solved please. Link to comment https://forums.phpfreaks.com/topic/104698-solved-trying-to-access-a-database/#findComment-535893 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.