Thursday 6 September 2018

basic php question - connection to the database

I literally just started looking into php, connecting to a remote database and returning data. I have been following a tutorial however when i apply the same code (as on the tutorial), i get
Server error
The website encountered an error while retrieving http://localhost:8888/file.php. It may be down for maintenance or configured incorrectly. Here are some suggestions: Reload this web page later.
This is the script - the database connection is fine, i believe its the retrieval of data that causes the error but have no idea on how to fix it ..
  <?php

   // Database credentials
   $host = "localhost";
   $db = "json";
   $uid = "json";
   $pwd = "json1";

    // Connect to the database server
    $link = mysql_connect($host, $uid, $pwd) or die("Could not connect");

   //select the json database
   mysql_select_db($db) or die("Could not select database");

// Create an array to hold our results
   $arr = array();
   //Execute the query
   $rs = mysql_query("SELECT id,userid,firstname,lastname,email FROM users");

   // Add the rows to the array
   while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;

?>


At the end of your script, you have this :
// Add the rows to the array
   while($obj = mysql_fetch_object($rs)) {
   $arr[] = $obj;

?>

You are opening { on the while line ; but never closing it.

0 comments:

Post a Comment