| | |
Returning HTML code through SQL query?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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:
This works for normal text but not HTML or jscript etc. Any suggestions?
PHP Syntax (Toggle Plain Text)
$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?
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.
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.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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:
But what doesn't work is this:
Anyone know why this would not work?
Thanks.
PHP Syntax (Toggle Plain Text)
<img src="IMAGE SOURCE URL">
But what doesn't work is this:
PHP Syntax (Toggle Plain Text)
<p>TEXT HERE! - <a target="_blank" href="HTML LINK ETC.">Click Here</a></p>
Anyone know why this would not work?
Thanks.
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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.
•
•
•
•
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.
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.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
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
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.
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.
![]() |
Similar Threads
- retrieving a particular value with a sql query (PHP)
- C# VS 2005 - SQL Query Parameters to an ODBC DataSource (C#)
- Retreiving variables from a sql query into a form (PHP)
Other Threads in the PHP Forum
- Previous Thread: Insert Into
- Next Thread: dosent like query
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube





