Jump to content

[SOLVED] Trying to access a database.


vomitbomb

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.