PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,404 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,619 people online right now. Registration is fast and FREE... Join Now!




display data from database

 

display data from database

mycraz

2 Jul, 2009 - 07:04 PM
Post #1

D.I.C Head
**

Joined: 10 Jun, 2009
Posts: 57



Thanked: 1 times
My Contributions
hi,
I had a database table named 'templates' and the I only want to display one row from it. The data in the table is inserted through a fckeditor. I had no problem displaying it back into the fckeditor but I can't display it in a the page.

CODE

$sql="SELECT template_details FROM templates WHERE template_title = Senarai Staf Sokongan";
$result=mysql_query($sql);
$templatecontent = html_entity_decode(stripslashes('template_details'));
echo "$templatecontent";


The codes above only display the words 'template_details'. Anyone can help?

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Display Data From Database

2 Jul, 2009 - 07:07 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
You have to loop through the $results variable, & fill an array.

This example is from http://php.net.

CODE

<?php
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname  = 'fox';

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>

User is online!Profile CardPM
+Quote Post

mycraz

RE: Display Data From Database

2 Jul, 2009 - 07:28 PM
Post #3

D.I.C Head
**

Joined: 10 Jun, 2009
Posts: 57



Thanked: 1 times
My Contributions
I tried it before but it output this error. Same goes when I put mysql_fetch_array. I think I can't loop it because I only wanted to display one row and one column from the table.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program Files\PHP\xampp\htdocs\SPPS PORTAL\test_displaytemp_sc.php on line 15

This post has been edited by mycraz: 2 Jul, 2009 - 07:28 PM
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: Display Data From Database

2 Jul, 2009 - 07:29 PM
Post #4

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Can you supply your code, so we can see what is on line 15?
User is online!Profile CardPM
+Quote Post

PsychoCoder

RE: Display Data From Database

6 Jul, 2009 - 06:37 PM
Post #5

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
The reason it only displays template_details is because that's what you're telling it to display. You put the results of the query into $result so that's what you want to display. Try this


CODE

$sql="SELECT template_details FROM templates WHERE template_title = Senarai Staf Sokongan";
$result=mysql_query($sql) or die(mysql_error());
$templatecontent = html_entity_decode(stripslashes($result));
echo "$templatecontent";

User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Display Data From Database

6 Jul, 2009 - 08:41 PM
Post #6

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
try this code:
php
$sql="SELECT template_details FROM templates WHERE template_title = Senarai Staf Sokongan";
$result=mysql_query($sql);
$templatecontent = html_entity_decode(stripslashes('template_details'));

while($fetch = mysql_fetch_assoc($result))
{
echo $fetch['template_details'];
}
// or simply
$fetch = mysql_fetch_assoc($result);
echo $fetch['template_details'];

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:34PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month