5 Replies - 1080 Views - Last Post: 06 July 2009 - 09:41 PM Rate Topic: -----

#1 mycraz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 10-June 09

display data from database

Posted 02 July 2009 - 08:04 PM

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.

$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?

Is This A Good Question/Topic? 0
  • +

Replies To: display data from database

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • Posts: 30,868
  • Joined: 10-May 07

Re: display data from database

Posted 02 July 2009 - 08:07 PM

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

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

<?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);
?>


Was This Post Helpful? 0
  • +
  • -

#3 mycraz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 10-June 09

Re: display data from database

Posted 02 July 2009 - 08:28 PM

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: 02 July 2009 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • Posts: 30,868
  • Joined: 10-May 07

Re: display data from database

Posted 02 July 2009 - 08:29 PM

Can you supply your code, so we can see what is on line 15?
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: display data from database

Posted 06 July 2009 - 07:37 PM

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


$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";


Was This Post Helpful? 0
  • +
  • -

#6 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: display data from database

Posted 06 July 2009 - 09:41 PM

try this code:
$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'];

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1