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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.