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
https://forums.phpfreaks.com/topic/82657-simple-query-cant-get-it-to-work/
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";

 

}

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";
 }

?>

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

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.