Jump to content

Problem with Query? (Beginner)


RobbieEls

Recommended Posts

Hi, I've been fiddling around with this code for a while now and I haven't had any luck getting it to work.

 

I'm trying to get all the information from a table by searching the list and getting all the information for the specific user.

"SELECT * FROM user WHERE username= $username"

 

but for some reason it doesn't work and I get a

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mounted-storage/home2/sub004/sc11440-ATOP/www/Game/Usr/index.php on line 30

Error

 

$Username is what ever the user logs in as so for example if I logged in as "Test" $username = Test and when I print $username it prints the word "Test" but when I try to use it as a WHERE (statement,command? what ever the correct wording is) I get the error I posted above if I make it search for  "WHERE username = $username2" and make "$username2 = 'Test'" it works.

 

If anyone could help I would really appreciate it, I'm pretty new to PHP so it's been a bit of a challenge.

also the code for the user system I have is from http://www.olate.co.uk/articles/185 and I haven't changed anything.

Or if anyone knows a simpler way to get the users data so I can show it on the page that would also help,

 

Thanks in Advanced,

RobbieEls

 

Full Code is here:

<?php
include 'init.php';

if (!is_authed())
{
     die ('You are not permitted to view this page, <a href="login.php">click here</a> to go login.');
}

include 'main.php';
echo "<BR><BR>";

//test
print($username);
echo "<BR><BR>";
$Usar = $username;
print("$Usar");
echo "<BR><BR>";
//end test

$query  = "SELECT * FROM user WHERE username= $username";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "<br>Name :{$row['username']}" .
    "<br>ID :{$row['userid']}" ;
} 

?>

Link to comment
https://forums.phpfreaks.com/topic/120347-problem-with-query-beginner/
Share on other sites

you problem is in here

$query  = "SELECT * FROM user WHERE username= $username";
$result = mysql_query($query);

 

try using

$query  = "SELECT * FROM user WHERE username= '$username'";         //variables should have single ticks on them
$result = mysql_query($query) or die ("error in query" . mysql_error());  //would tell you where the error is

Archived

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

×
×
  • Create New...

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.