records not been displayed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
naturepoonam
Forum Newbie
Posts: 3
Joined: Sat Oct 29, 2011 1:20 pm

records not been displayed

Post by naturepoonam »

Hi,

I am using the below code to display records from pricing table which has 2 fields amount and premium

the table is not been displayed can someone tell me whats wrong with the code

it just prints my code to the page.

Thanks

poonam

Code: Select all

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'mysql12.abc.com';
$db_user = 'test1';
$db_pwd = 'Test123';

$database = 'testdatabase';
$table = 'pricing';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
Last edited by califdon on Sat Oct 29, 2011 3:46 pm, edited 1 time in total.
Reason: Moderator added syntax=php tags to make code readable. Note to poster, please always do this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: records not been displayed

Post by califdon »

When you see the raw PHP code, it means that the server does not recognize that the file contains PHP code. Is this script being served by a web server, such as Apache? Is the server configured properly to recognize the filetype of the file you are trying to view? Typically the filetype must be .php, although other filetypes can be specified.
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: records not been displayed

Post by dsnraju »

Code: Select all

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'mysql12.abc.com';
$db_user = 'test1';
$db_pwd = 'Test123';

$database = 'testdatabase';
$table = 'pricing';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM pricing");
if (!$result) {
    die("Query to show fields from table failed");
}



echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers

    echo "<td>Field Name1</td><td>Field Name2</td>";

echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_array($result))
{
    echo "<tr>";

    echo "<td>".$row[0]."</td><td>".$row[1]."</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
Last edited by Benjamin on Sun Oct 30, 2011 4:12 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: records not been displayed

Post by dsnraju »

try this code it should help you to display the records

i was not closing the table tag </table> put i your code
Post Reply