Forum Moderators: coopster

Message Too Old, No Replies

Displaying Mysql data in HTML table format

PHP , MySQL, HTML tables

         

naiquevin

7:00 am on Mar 10, 2009 (gmt 0)

10+ Year Member



I want to display data stored in Mysql tables on my webpage in tabular format

I am using w3schools.com for learning purpose and as per the tutorials there I coded as follows

$result = mysql_query("SELECT * FROM table_name") ;

echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";

}

echo "</table>";

The page loads properly but no table is displayed... while everything within HTML tags is displayed properly
as if PHP script is completely bypassed

Whats wrong here? Have spent quite some time on it now but I am not able to figure out any error
Thanks

deejay

9:14 am on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this - it will give you some more information about what's potentially going wrong.

$result = mysql_query("SELECT * FROM table_name") ;
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";

while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";

}

echo "</table>";
} //end close off the if statement

Habtom

9:14 am on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am assuming the PHP script above is enclosed in <?php ?>

Change:
while($row = mysql_fetch_array($result))
to
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

naiquevin

2:53 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Still not working ... tried both of the above ..

script is enclosed in <?php ?

This is the full script again

<html>

<head>

<title> View contacts </title>

<link rel="stylesheet" type="text/css" href="phbook.css">

</head>

<body>

<?php include("backlinks.php"); ?>

<form action="result.php" method="post">

<b> Search for: </b>
<input type="text" size="40" name="searchfor"/>

<input type="submit" value="Find"/>

<input type="hidden" name="user" value="<?php echo $_POST["user"]; ?>"/>

</form>


<?php

// Establishing connection or displaying error msg in case of a failure

$con=mysql_connect("localhost","username","");

if(!$con)
{
die('Could not Connect:' . mysql_error());
}

// Selecting the database

mysql_select_db("myapp",$con);


$u = $_POST["user"];

if($u=="user1")
{
$result = mysql_query("SELECT * FROM user1");

$result2 = mysql_query($result);

if(!$result)
{
die('Error:' . mysql_error());
}

if(mysql_num_rows == 0)
{
echo "No records found";
}
else
{

echo "<table border='1'>
<tr>
<th> Name </th>
<th> Std code </th>
<th> TelePhone </th>
<th> Mobile </th>
<th> Email </th>
</tr>";


while($row = mysql_fetch_array($result2,MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['std_code'] . "</td>";
echo "<td>" . $row['Phone'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "</tr>";

}

echo "</table>";
}
}
//....... similar script for other users 2,3, and 4 using else if
//closing the connection

mysql_close($con);

?>
</body>

</html>

------------------------------------------------------------

Thanks again for your help ..

Habtom

3:02 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your problem is here:
$result = mysql_query("SELECT * FROM user1");
$result2 = mysql_query($result);

Change to:

$result = "SELECT * FROM user1";
$result2 = mysql_query($result);

naiquevin

3:16 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



thanks Habtom but still no luck !

Actually, initially it was showing an error on this line in the above script

echo "<td>" . $row['Name'] . "</td>";

but there was also another error which was
echo "<table border="1"... >";
which I changed to echo "<table border='1'... >";

After doing this it stopped showing any error .. not even the one which it was showing earlier.. so neglected it.

Is there anything wrong in this particular line
echo "<td>" . $row['Name'] . "</td>";

PS> The fieldname in the table is 'Name' only..

rocknbil

4:59 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All of these should work.

Have you tried checking your error logs?

I see you have adequate error checks, but even so, put this at the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Remove it after going live.

also I don't see a header, probably not important, but put this somewhere before output, top of script is fine
header("content-type: text/html\n\n");

So your table name is user1, right?

At various points do a var dump. Top of script,

var_dump($_POST);
or
var_dump($GLOBALS);

Something else is going on here, these should work.

andrewsmd

5:29 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On your if u == user1 try this first

//check to see if they clicked the button
if(isset($_POST['user'])){

//then do your check
if($u == "user1"){

//do your code

}//if u = user

}//if isset

Also, the var_dump is a good debugging tool. Run it on all of your variables and see if they are the same type and value you think they should be. One last thing for readability. You can write strings like this

$str = "<th>{$row['Name']}</th>";
I just think that is easier to read, just a personal preference.