Jump to content

Archived

This topic is now archived and is closed to further replies.

sjonni

simple mysql query help

Recommended Posts

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?

Share this post


Link to post
Share on other sites
The result from
[code]
$result = mysql_query($sql);
[/code]

is a result [i]resource[/i], 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:

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

Share this post


Link to post
Share on other sites
or
[code]echo mysql_result($result,0);[/code]

either way, you need one more step.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
that means your query is probably not correct. add this to your code:
[code] $result = mysql_query($sql) or die(mysql_error());[/code]

and tell us what error you got.
also post your query.

Share this post


Link to post
Share on other sites
Thanx :)

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.