943,662 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6922
  • PHP RSS
Jun 12th, 2007
0

Returning HTML code through SQL query?

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. $result = @mysql_query("SELECT * FROM sponsors");
  2. $num_rows = mysql_num_rows($result);
  3. ?>
  4. document.write('<table align="center">');
  5. <?
  6. if($num_rows == 0)
  7. {
  8. ?>
  9. document.write('<p>No sponsors at this time</p>');
  10. <?
  11. }
  12. else
  13. {
  14. for($i=0; $i<$num_rows; ++$i)
  15. {
  16. $text = mysql_result($result,$i,"html");
  17. ?>
  18. document.write('<tr align="center"><td>');
  19. document.write('<p><?=$text;?></p>');
  20. document.write('</td></tr>');
  21. <?
  22. }
  23. }
  24. ?>
  25. document.write('</table>');
  26. <?

This works for normal text but not HTML or jscript etc. Any suggestions?
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jun 12th, 2007
0

Re: Returning HTML code through SQL query?

Correction, straight HTML works however scripts will not work.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jun 13th, 2007
0

Re: Returning HTML code through SQL query?

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.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Jun 13th, 2007
0

Re: Returning HTML code through SQL query?

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:

PHP Syntax (Toggle Plain Text)
  1. <img src="IMAGE SOURCE URL">

But what doesn't work is this:

PHP Syntax (Toggle Plain Text)
  1. <p>TEXT HERE! -
  2. <a target="_blank" href="HTML LINK ETC.">Click Here</a></p>


Anyone know why this would not work?

Thanks.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jun 13th, 2007
0

Re: Returning HTML code through SQL query?

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jun 14th, 2007
0

Re: Returning HTML code through SQL query?

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
Last edited by Rhyan; Jun 14th, 2007 at 2:26 am. Reason: Added post scriptum.
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Jun 18th, 2007
0

Re: Returning HTML code through SQL query?

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.
Last edited by macneato; Jun 18th, 2007 at 4:36 am.
Reputation Points: 46
Solved Threads: 48
Posting Pro in Training
macneato is offline Offline
410 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Insert Into
Next Thread in PHP Forum Timeline: dosent like query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC