Page 1 of 1

Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 12:40
by garfee
Hi there.

Appologies if this is has an obvious answer, I have looked everywhere for an answer but cannot find one and it is driving me crazy !

I have installed xampp and created a simple database with some information stored in a table. I have also created a php page that connects to the database and should then draw the information out and display it on a php page.

The connection works fine. However, no matter what I do, the data will not display on the php page.

Is there something obvious that I am missing ?

The php code works fine on my remote server.

I have also tried installing another similar testing server, with phpmysql, and the problem is the same.

Any help would be greatly appreciated.

Re: Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 13:49
by Altrea
Hi garfee,

garfee wrote:no matter what I do, the data will not display on the php page.

Is there something obvious that I am missing ?

How should we know without knowing the sourcecode?

garfee wrote:Any help would be greatly appreciated.

Help us helping you.

best wishes,
Altrea

Re: Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 15:24
by garfee
It does work on my remote server. So would that be a source code issue ?

But anyway. Here it is

Code: Select all
<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>

 
<table width="400" border="1" cellspacing="0" cellpadding="3">

 

<?php

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td width="10%"><? echo $rows['id']; ?></td>
<td width="30%"><? echo $rows['name']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['email']; ?></td>
</tr>

<?php
// close while loop
}


?>


<?php
// close MySQL connection
mysql_close();
?>


The connection to the database is working fine. But my page just displays an empty html table.

Re: Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 16:05
by Altrea
garfee wrote:It does work on my remote server. So would that be a source code issue ?

It is always a mix of environment (os, configuration, components, etc) and the code itself that provide a functionality.
If your code relies on specific configuration parameters, the sourcecode is very important to know.

In your case it is the short_open_tag setting of php your code relies on:

Code: Select all
[...]
<td width="10%"><? echo $rows['id']; ?></td>
<td width="30%"><? echo $rows['name']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['email']; ?></td>
[...]


You can use something like this (it is independend of short_open_tag setting):
Code: Select all
[...]
<td width="10%"><?php echo $rows['id']; ?></td>
<td width="30%"><?php echo $rows['name']; ?></td>
<td width="30%"><?php echo $rows['lastname']; ?></td>
<td width="30%"><?php echo $rows['email']; ?></td>
[...]


or something like this (working on all versions with enabled short_open_tag setting or PHP >=5.4)
Code: Select all
[...]
<td width="10%"><?=$rows['id']?></td>
<td width="30%"><?=$rows['name']?></td>
<td width="30%"><?=$rows['lastname']?></td>
<td width="30%"><?=$rows['email']?></td>
[...]

(<?= is a short open tag too, but since PHP 5.4 it will work even with disabled short_open_tag setting).

There is also a solution by activating short_open_tag in your php.ini, but that is not recommend. It is always better if the code is independend from any settings (unless there is no way around that).

best wishes,
Altrea

P.S::
Code: Select all
mysql_connect("$host", "$username", "$password")

The quotes around the variables are not needed.

Re: Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 16:17
by garfee
Hey Altrea.

I cannot thank you enough. That has done the trick.

Sorry, it was a rather lame problem. I have not been involved with php very long and learning as I go. All of that information was very useful.

Thanks again for helping me out :D

Re: Tables from SQL database not displayed on php page

PostPosted: 17. May 2013 16:26
by Altrea
You are welcome :D
Have fun with XAMPP and all that stuff 8)