PHP/HTML Table output fails?

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
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

PHP/HTML Table output fails?

Post by Flashart »

HI Everyone

I'm going slightly insane trying to figure out why this code fails to retrieve any data.

Code: Select all

<html><head><title>All Data</title></head><body>
<?php
$db_host = 'host';
$db_user = 'user';
$db_pwd = 'pwd';
 
$database = 'new_database';
$table = 'alldata';
 
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 {$adgroup_data}");
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>
It fails at this part.

Code: Select all

// sending query
$result = mysql_query("SELECT * FROM {$adgroup_data}");
if (!$result) {
    die("Query to show fields from table failed");
}
I have tried numerous iterations of php code to retrieve data from a mysql database (php noob ya' see..) all to no avail.

Connection parameters are fine as far as I am aware (triple checked after I had triple checked) and presumably it would fail before the above code block if any of the connection parameters were incorrect.

So I hand this to expert sages (you!) in the hope that you may be able to help?

Kindest regards
Peter
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP/HTML Table output fails?

Post by AbraCadaver »

What is $adgroup_data? It hasn't been defined anywhere?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Flashart
Forum Commoner
Posts: 71
Joined: Tue Oct 06, 2009 12:12 pm

Re: PHP/HTML Table output fails?

Post by Flashart »

Oops my mistake,

took code from another page.

That should read "alldata" not "adgroup_data" like so

Code: Select all

// sending query
$result = mysql_query("SELECT * FROM {$alldata}");
if (!$result) {
    die("Query to show fields from table failed");
}
It seems to fail here but I don't know why!

Regards
Peter
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP/HTML Table output fails?

Post by McInfo »

Code: Select all

$result = mysql_query("SELECT * FROM `{$table}`");

Code: Select all

$result = mysql_query("SELECT * FROM `alldata`");
Edit: This post was recovered from search engine cache.
Post Reply