Jump to content

Simple query cant get it to work


payney

Recommended Posts

Hi all.

I cannot understand why this is not working, I will first show you the code:

 

$username = $_SESSION["authenticatedUser"];

echo $username;

 

  if (!empty($username))

  {

    require ('db.php');

 

    if (!($connection = @ mysql_pconnect($hostname, $username, $password)))

    die("Could not connect to database");

 

    if (!mysql_select_db($databaseName, $connection))

    showerror();

 

    $query = "SELECT * FROM users WHERE username = $username";

 

    if (!($result = @ mysql_query ($query, $connection)))

    echo "Query error";

 

    while($row = mysql_fetch_assoc($result)) { 

    echo  $row['email'];

    echo  $row['username'];

    echo $row['password']; 

 

  }

 

With this I get the following error:

 

payneyQuery error

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

Now, if I hardcode the username, which is 'payney', it works.

 

   

Link to comment
Share on other sites

Check by this :

 

if (!(mysql_query($query, $connection))){

   

  while($row = mysql_fetch_assoc($result)) {

 

    echo  $row['email'];

    echo  $row['username'];

    echo $row['password']; 

   

    } 

 

}else{

        echo "Query Fail";

 

}

Link to comment
Share on other sites

your code should be something like...

 

<?php
$username = $_SESSION["authenticatedUser"];      
echo $username;

  if (!empty($username))
  {
    require ('db.php');
   
    if (!($connection = @ mysql_pconnect($hostname, $username, $password)))
    die("Could not connect to database");

    if (!mysql_select_db($databaseName, $connection))
    showerror();
       
    $query = "SELECT * FROM users WHERE username = '$username'";
$result = mysql_query ($query, $connection);
$intNumRows = mysql_num_rows($result);


    if ($intNumRows > 0)
{
    while($row = mysql_fetch_assoc($result)) { 
     echo  $row['email'];
     echo  $row['username'];
     echo $row['password'];   
   
     }
}
else 
 {
		    echo "No records fetched";
 }

?>

Link to comment
Share on other sites

Uhmmm.

 

$usename is being used by the main code and by the database mysql_connect() function.

 

The value it had from the session is replaced when the 'db.php' file is required/included.

 

That is correct, just realised that when returning the query. Whoopsy daisy. Problem solved by changing the name of username to username2.

 

Cheers for all the help and effort guys  ::)

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.