Pachi
Pachi

Reputation: 25

Trouble displaying MySQL table in HTML/PHP

I'm having a hard time figuring out how to get my table to display as a web page.

Everything else is working fine. I've been able to use a form to write records TO the table, but I'm simply unable to display the table.

Here's my code:

$host = "***";
$userName = "***";
$passWord = "***";
$db = "doctorWho";

mysql_connect($host,$userName,$passWord);
mysql_select_db($db) or die( "Unable to access database");
$query = "SELECT * FROM patients";

$result = mysql_query($query);

echo "<table border='1'>
    <tr>
    <th>Last Name</th>
    <th>First Name</th>
    <th>Address</th>
    <th>Age</th>
    <th>Sex</th>
    <th>Marital Status</th>
    <th>Medication</th>
    <th>Date Rx'd</th>
    <th>Quantity</th>
    </tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['lastName'] . "</td>";
  echo "<td>" . $row['firstName'] . "</td>";
  echo "<td>" . $row['address'] . "</td>";
  echo "<td>" . $row['age'] . "</td>";
  echo "<td>" . $row['sex'] . "</td>";
  echo "<td>" . $row['maritalStatus'] . "</td>";
  echo "<td>" . $row['medication'] . "</td>";
  echo "<td>" . $row['medsWhen'] . "</td>";
  echo "<td>" . $row['medsQuant'] . "</td>";
  echo "</tr>";

  }
echo "</table>";

Upvotes: 2

Views: 524

Answers (4)

Vivek Sadh
Vivek Sadh

Reputation: 4268

Just change:-

while($row = mysqli_fetch_array($result))

to

while($row = mysql_fetch_array($result))

Upvotes: 0

Jhonathan H.
Jhonathan H.

Reputation: 2713

// I think your connection should look like this
$conn = mysql_connect($host,$userName,$passWord);
mysql_select_db($conn,$db) or die( "Unable to access database");

// another is your using mysql as connection so you can use mysqli functions on that rofl.
//Instead use mysql functions
mysql_fetch_array();

note: Another one is instead of using @ sign to handle error. Its much more best practice to use.. ini_set() or configuring php.ini in handling displaying of errors.

Upvotes: 0

dev.meghraj
dev.meghraj

Reputation: 9110

From php ref.

mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )

mysqli_fetch_array need to mysqli_result, but you are using mysql_query, you should try mysql_fetch_array

Upvotes: 0

Daedalus
Daedalus

Reputation: 7722

You want mysql_fetch_array(). Currently, you are mixing two different extensions. Each takes something different, relevant to the extension.

That aside, you should consider either PDO or MySQLi, which is where the wrong function you attempted to use is from. This article should help you decide which you could possibly use.

Upvotes: 4

Related Questions