Jump to content

Recommended Posts

Hey guys,

I'm making a real basic login that looks up the user name entered in a database and if it exists says Welcome. But the query isn't actually running, I've printed the output of it to see what's going out and its just returning what I've set the variable to i.e. SELECT ...

 

 

 

<? 

mysql_connect( "localhost",  );

$dbcnx = @mysql_connect( "localhost",  );
if (!$dbcnx) {
  echo( "<P>Unable to connect to the " .
        "database server at this time.</P>" );
  exit();
}

$user = "SELECT username FROM users WHERE username = '$_POST[username]'";

echo "$user";

if ($user == '1') {

echo "Logged in Welcome";

}

else
{
echo "Invalid Sorry";
}



?>

 

Returns

 

SELECT username FROM users WHERE username = 'test'Invalid Sorry

 

Any ideas, thanks.

 

PS. Ive removed my username and password from the top but it does connect.

Link to comment
https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/
Share on other sites

Hey guys,

I'm making a real basic login that looks up the user name entered in a database and if it exists says Welcome. But the query isn't actually running, I've printed the output of it to see what's going out and its just returning what I've set the variable to i.e. SELECT ...

 

 

 

<? 

mysql_connect( "localhost",  );

$dbcnx = @mysql_connect( "localhost",  );
if (!$dbcnx) {
  echo( "<P>Unable to connect to the " .
        "database server at this time.</P>" );
  exit();
}

$user = "SELECT username FROM users WHERE username = '$_POST[username]'";

echo "$user";

if ($user == '1') {

echo "Logged in Welcome";

}

else
{
echo "Invalid Sorry";
}



?>

 

Returns

 

SELECT username FROM users WHERE username = 'test'Invalid Sorry

 

Any ideas, thanks.

 

PS. Ive removed my username and password from the top but it does connect.

 

You need to actually execute the query and fetch the results:

$query = "SELECT username FROM users WHERE username = '{$_POST['username']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$user = $row['username'];

 

Keep in mind, unless the username in the DB is actually 1, your test will fail every time.

<? 

mysql_connect( "localhost", );

$dbcnx = @mysql_connect( "localhost",  );
if (!$dbcnx) {
  echo( "<P>Unable to connect to the " .
        "database server at this time.</P>" );
  exit();
}

$query = "SELECT username FROM users WHERE username = '{$_POST['username']}'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

$user = $row['username'];


if ($user == 'true') {

echo "Logged in Welcome";

}

else
{
echo "Invalid Sorry";
}



?>

 

Returns

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/thegamer/public_html/PHP/login2.php on line 14

Invalid Sorry

 

Does this mean I've spelt a database name wrong or something?

no it means there is a problem with the result resource returned from mysql_query. This normally means you have an error in your query. Whenever you come across such an error always add 'or die(mysql_error())' after your call to mysql_query eg

$result = mysql_query($query) or die(mysql_error());

 

If there is a problem with your query an error message from MySQL will be displayed.

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.