2

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>";
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pachi
  • 25
  • 6
  • 4
    Do not use mysql_*? It is deprecated. mysqlpi or PDO is the replacement and the newest beast in town. – Ed Heal Apr 01 '13 at 07:36
  • and if you are using mysql than please dont do it .. reason is exactly what Mr. @Ed Heal is telling form more information check http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя Apr 01 '13 at 07:38
  • @NullPonyPointer - You make me sound like a horse Wilber – Ed Heal Apr 01 '13 at 07:56

4 Answers4

4

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.

Daedalus
  • 7,586
  • 5
  • 36
  • 61
  • @NullPonyPointer actually, I'm just a noob at this, slowly learning. Thanks for your help though! – Pachi Apr 01 '13 at 07:37
  • @Pachi If this answer solved your problem, please consider accepting it by clicking the checkmark under the down arrow next to it, when it lets you. – Daedalus Apr 01 '13 at 07:39
  • @Pachi please noob doesnot mean we dont do debuting i am sure you are getting error and not trying(give enough attention ) to solve it also dont use `@` operator to ignore error its always bad you must solve them – NullPoiиteя Apr 01 '13 at 07:39
  • @Daedalus I'm working through this. Thank you very much for the information you've given me! – Pachi Apr 01 '13 at 07:40
  • ignoring error is very bad .. and caused UN-identified defects and can rune whole project so its good to start solving and handling them properly – NullPoiиteя Apr 01 '13 at 07:41
  • @NullPonyPointer I'm not ignoring errors. I never ignore errors. I work through the code until I stop getting warnings and errors. – Pachi Apr 01 '13 at 07:43
  • @Pachi i dont see any reason of `@` here `@mysql_select_db($db) or die( "Unable to access database");` – NullPoiиteя Apr 01 '13 at 07:45
  • @NullPonyPointer Removed it. I don't recall meaning to type that, though the connection is working fine regardless. Thank you for pointing that out. – Pachi Apr 01 '13 at 07:46
0
// 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.

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
0

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

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76
0

Just change:-

while($row = mysqli_fetch_array($result))

to

while($row = mysql_fetch_array($result))
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49