PDA

View Full Version : PHP and MySQL Help


Ady
02-17-2006, 01:52 PM
'pages' Table:
http://img106.imageshack.us/img106/2240/sig2409sm.jpg

<?
include("config.php");
$sql="SELECT * FROM pages WHERE id='$user'";
$result3=mysql_query($sql);
$rows2=mysql_fetch_array($result3);
$user2=$rows2['id'];
$key2=$rows2['key'];
$title2=$rows2['title'];
$body2=$rows2['body'];
if($user==$user2 && $page==$key2){ echo ('<b>' . $title2 . '</b><br>' . $body2); };
?>

I'm sure I've done this right, but it doesn't work. :( I'm trying to make it so if you visit the page with ?user=#&page=#, it searches the table for rows with whatever $user and $page are. If they are both in the same row, then it displays 'title' and 'body' from that row. I hope someone understands what I'm talking about... any help is appreciated. :)

PS. Sorry my code is probably really messy, I'm not too good with PHP / MySQL.

ronaldb66
02-17-2006, 02:21 PM
Define "it doesn't work"; what do (or don't) you get? Are $user and $page properly set (trying echoing them to see if they are)? What does the query return (again: echo...)?

By the way, if both user and page keys are available, why not add $page to your query? If it returns something => great; if no rows are returned => no dice.

Ady
02-17-2006, 02:46 PM
Define "it doesn't work"; what do (or don't) you get? Are $user and $page properly set (trying echoing them to see if they are)? What does the query return (again: echo...)?

By the way, if both user and page keys are available, why not add $page to your query? If it returns something => great; if no rows are returned => no dice.

Yup, $user and $page are both set, I echoed them and they both come out the way I want them to. Basically, I don't get what I have to do to get this to work lol (obviously), I have tried arranging everything differently and stuff, but still nothing works.

Also, if I try to do this:

$sql = "SELECT key FROM pages WHERE key='" . mysql_escape_string($_GET['page']) . "'";
mysql_fetch_array($sql);

It comes up saying:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/smashroc/public_html/webedit/index.php on line 68

Brandoe85
02-17-2006, 03:22 PM
What error are you getting? A mysql error? I would add on a or die statement to your query for debugging, maybe you're not selecting the db or something like that:

$result3 = mysql_query($sql) or die(mysql_error());



Also, if I try to do this:

$sql = "SELECT key FROM pages WHERE key='" . mysql_escape_string($_GET['page']) . "'";
mysql_fetch_array($sql);

It comes up saying:...
That's because you're not running the query, you're just passing the string to mysql_fetch_array(), you need to call mysql_query() and pass that resource.

Good luck;


You also have a semi colon after your ending curly brace:

if($user==$user2 && $page==$key2)
{
echo ('<b>' . $title2 . '</b><br>' . $body2);
};