Jump to content


Photo

simple mysql query help


  • Please log in to reply
5 replies to this topic

#1 sjonni

sjonni
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 July 2006 - 04:33 PM

Hi

I am totally new to php and MySQL.
Let my try and explain my problem. I know this is REALLY simple, i´m just not there yet ;)

I am working on a site with multiple pages. I want the same content to be on the left and right side of the pages, so I figured the easiest way was to create a MySQL table to handle the contents.
So it would look something like this

|                |                                |                |
| same for all |        main content      | same for all |
|    pages    |                                |    pages    |

The table consists of 2 fields, ID and content.
the problem is, how do I make a command to show a content from a desired ID.

This is the code in which I thought would do it, but no :(

<?php
$sql = "SELECT content from sidebar WHERE id = 1";
$result = mysql_query($sql);
echo $result;?>

can anyone help me with this?

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 26 July 2006 - 05:49 PM

The result from
$result = mysql_query($sql);

is a result resource, not something you can echo. To get the content you want, you need a further step - you need to fetch an array from that resource and then the content is an element in the array.  So:

$result = mysql_query($sql);
if($row=mysql_fetch_assoc($result))
{
$content = $row["content"];
echo $content;
}

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 July 2006 - 06:00 PM

or
echo mysql_result($result,0);

either way, you need one more step.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 sjonni

sjonni
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 July 2006 - 06:08 PM

thank you for your replys, but neither of this seems to work :(

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/netmynd.is/left.php on line 35

maybe there is a better solution to do this than this one?

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 July 2006 - 06:16 PM

that means your query is probably not correct. add this to your code:
$result = mysql_query($sql) or die(mysql_error());

and tell us what error you got.
also post your query.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 sjonni

sjonni
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 July 2006 - 08:49 PM

Thanx :)

I got it to work after adding the error code .. newbie mistake, misspelled the table name! ::)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users