I need to return some HTML code stored as a text data type in my database. Is there a way to return this HTML effiencly through PHP that will execute this code when HTML parses the page? Currently I have this:

$result = @mysql_query("SELECT * FROM sponsors");
        $num_rows = mysql_num_rows($result);
        ?>
        document.write('<table align="center">');
        <?
        if($num_rows == 0) 
        { 
            ?>
            document.write('<p>No sponsors at this time</p>');
            <?
        }
        else
        {
            for($i=0; $i<$num_rows; ++$i)
            {
                $text = mysql_result($result,$i,"html");
                ?>
                document.write('<tr align="center"><td>');
                document.write('<p><?=$text;?></p>');
                document.write('</td></tr>');
                <?
            }
        }
        ?>
        document.write('</table>');
        <?

This works for normal text but not HTML or jscript etc. Any suggestions?

Correction, straight HTML works however scripts will not work.

Member Avatar for Rhyan

Dude, are you trying to use javascript with the php?
Instead of using javascript the way you want - make the php echo the results for you.
Another thing is - you can optimize your query to the mysql db, as I see you want a single cell from the table to be displayed. Instead retrieving all records using * in the select statement, better do it like this.
$result = mysql_query("select html from sponsor");
$rows = mysql_num_row($result) -> this will give you information how many records you have in the db.

if ($rows=0)
{ echo '<p>No sponsors at this time!</p>';}
else
{
echo '<table>';
for ($i=0; $i<$rows; $i++)
{
$text = mysql_result($result, $i); -> you don't need the column name as it is only 1 column
echo "<tr> /n /t<td>$text</td> /n /t</tr>";
}
}
echo '</table>';

And that's that.

Try it and advise what happens.

Thanks for the response. I know it looks dumb but for some reason php echo commands do not work on the server (its not my server, im doing this fo a friend). So I resorted to js commands to print results. Thanks for the optimization tips. I changed up the code a bit and i have some code to work but not others and for some reason (its a very strange server) it wont throw the errors when there is one, it just wont run the script. What works right now is text like this:

<img src="IMAGE SOURCE URL">

But what doesn't work is this:

<p>TEXT HERE! -
<a target="_blank" href="HTML LINK ETC.">Click Here</a></p>

Anyone know why this would not work?

Thanks.

UPDATE: What i am doing is takign the html string, performing the php function, addslashes($html); and then inserting that into the database. This works however when I tried to print to the screen it shows up as white space. I'm assuming its throwing an error however idk where its coming from. Any help would be appreciated.

Member Avatar for Rhyan

UPDATE: What i am doing is takign the html string, performing the php function, addslashes($html); and then inserting that into the database. This works however when I tried to print to the screen it shows up as white space. I'm assuming its throwing an error however idk where its coming from. Any help would be appreciated.

What kind of server does your friend have? Advise versions of php and Apache as well.

Apache 2.x does not show errors along with the code.
To view the errors you have, check the apache error log or change the config file to show php errors. - I am not 100% sure whether you have to change the httpd.conf or the php.ini - I have to check.

If you have errors in your mysql syntax, then you should use the mysql_error() function of php. It goes something like this
e.g.
$query = mysql_query("your_query", link);
if (!$query){die mysql_error();}

For the echo function, it is really awkward, still you can always try the print() function, that is basically the same.

Good luck.

p.s. use <?php ?> as of versions abve 4.x

Don't know if this will be any help. But i'd suggest using a inline frame to print the table onscreen.

Use this script to print the table

<?php
$db_host = 'HOST';
$db_user = 'USER';
$db_pwd = 'PASS';

$database = 'NAME';
$table = 'NAME';

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 "<table border='0'><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);
?>

Then just link the url to the inline frame
NB. Remember to save your html as PHP if your phasing isn't setup.
This is probably the easiest way i've come up with without the use of phasing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.