-1

I have code here that is supposed to print a html table from my mysql database. When I open the page in my web browser, it is a blank page.

<html>
<body>
<?php
$connection = mysql_connect('localhost', 'admin', 'may122000');
mysql_select_db('contacts');

$query = "SELECT * FROM users";
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){ 
echo "<tr><td>" . $row['first_name'] . "</td><td>" . $row['last_name'] . "</td></tr>";  //$row['phone'] the index here is a field name
}

echo "</table>";

mysql_close();
?>
</body>
</html>
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    This question does not appear to have anything to do with programming in Java, and so I've removed the tag. Perhaps you wish to tag this for Javascript? Tags are very important since if you use the right and most specific tags, you'll get the best experts attracted to your question, so care with their use will only help you in your quest to get a decent answer. – Hovercraft Full Of Eels Nov 16 '14 at 15:50
  • 1
    FYI, `mysql` has been deprecated, instead use `mysqli` or `PDO` like service. – thecodeparadox Nov 16 '14 at 15:53
  • possible duplicate of [PHP's white screen of death](http://stackoverflow.com/questions/1475297/phps-white-screen-of-death) – CBroe Nov 16 '14 at 15:58
  • @thecodeparadox PHP's mysql API has been deprecated. MySQL is still alive and kicking! – Strawberry Nov 16 '14 at 16:24
  • @Strawberry yes, should use php `mysql` api. Sorry that I can't edit now. Thanks for point out. – thecodeparadox Nov 16 '14 at 16:29

4 Answers4

1
  1. Remove password
  2. Enable error output

When you use mysql_fetch_array you will get the resulting array with numeric indices. mysql_fetch_assoc will give you an associative array, like you want.

Note: mysql_* is deprecated.

while($row = mysql_fetch_assoc($result)){ 
echo "<tr><td>" . $row['first_name'] . "</td><td>" . $row['last_name'] . "</td></tr>";  //$row['phone'] the index here is a field name
}

If you still want to use mysql_fetch_array you'll have to pass a second parameter:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
msfoster
  • 2,474
  • 1
  • 17
  • 19
  • `mysql_fetch_array` fetches a both associative and numeric array by default. – TiiJ7 Nov 16 '14 at 15:57
  • Nope, then you'll need to pass a second parameter `MYSQL_ASSOC` – msfoster Nov 16 '14 at 15:58
  • 1
    Then you only get an associative array. The default is `MYSQL_BOTH` per the [documentation](http://php.net/manual/en/function.mysql-fetch-array.php) – TiiJ7 Nov 16 '14 at 16:03
  • Standard output for mysql_fetch_array is BOTH, numeric and column key array, that's the default. if this doesn't happen for you there may be a PHP setting somewhere that turns this off, but it is the default. – Martin Nov 16 '14 at 16:44
0

First of all user mysqli or PDO and mysqli_fetch_assoc() so you have only associative array. Blank page is probably result of a hidden error, that's stored in your error.log on your server - take a look at it and get back to us.

LukasS
  • 176
  • 7
0

I prefer using PDO or mysqli but anyway , Are u sure Your connection is established ? to check this and check other connections and query :

if (!connection)
die(mysql_error());

try this and feedback me

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
0

Improvements - some of which already mentioned in other post but all put together in one form:

<?php

$connection = mysqli_connect('localhost', 'admin', '****', 'contacts');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = "SELECT first_name, last_name, phone FROM users";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));

echo "<table>"; // start a table tag in the HTML

while($row = mysqli_fetch_array($result)){ 
echo "<tr><td>" . $row['first_name'] . "</td><td>" . $row['last_name'] . "</td></tr>";  //$row['phone'] the index here is a field name
}

echo "</table>";

mysqli_close($connection);
?>

So, first off the MySQL_* has been upgraded to Mysqli, with some minor reformatting, The select * has been replaced with selecting only the needed columns. The closing statement has been correctly set.

Firstly if your connection fails an error catch will output this to the screen. Remove this upon product launch or public launch of the page.

A (Rather rudimentary) error catch has been put in that if the SQL Query is bad that an error is outputted. Again, this should be removed in production but will help you with finding SQL errors.

If No SQL errors return the you have either an empty table in your database, or some sort of PHP error but from the code sample given the most likely error is that your PHP doesn't run MySQL and would only run PDO or MySQLi.

You also said "when I open the page in my browser it is a blank page", if the Source of the page is blank - as in it DOES NOT show

<html>

etc, then this is a sign the PHP execution failed and you have bad PHP, as detailed in your error log file.

The most likely cause of this from the code sample given is, as stated already, your PHP version does not support MySQL.

If your

<table> 

Tag appears in your HTML source code then this is a sign that the While clause is not running which means your Datbase table is empty and there is no data to output.

Hope this helps. But first point of call is to upgrade to MySQLi :)

Martin
  • 22,212
  • 11
  • 70
  • 132