By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Simple SELECT query not working

P: 12
Am i losing my mind? The following mysql query seems to fail even though i am running it outside of the PHP code and its working fine. An echo statement shows that the select variables are being populated as expected. can anyone advise?
[PHP]
$userid = $_POST['userid'];
$password = $_POST['password'];

$getuser = "SELECT UserID,Password FROM User WHERE UserID = '$userid' AND Password = '$password'";

$userlogin=mysql_query($getuser);

while ($row = mysql_fetch_array($userlogin)) {
$loginid = $row['UserId'];
$loginpwd = $row['Password']; }

if (!$userlogin) {
exit('<p>There was a problem recalling your user details<p>');
}
[/PHP]
May 13 '07 #1
Share this Question
Share on Google+
8 Replies


Atli
Expert 5K+
P: 5,058
Have you check if the mysql_error() function gives you any error messages?
Can you give use the table structure?
If you have any error messages, please share them with us.

Besides everything, you do realize that statment is giving you the excact same data you are puting into it, right?

O, and I just realized, your putting quote marks arround your UserID. Are you sure you should be doing that?
May 13 '07 #2

P: 12
Thanks for replying Atli.

The table structure is:

UserID char(10)
Password char(10)
DisplayName char(30)

UserID is the primary key.

I am aware I am returning what I am putting in. I will change that. All i am doing is attempting to pull back a row given the 2 SELECT conditions. How do I get the error text out. I have used the following command and nothing is displayed:

$error=mysql_error($userlogin);

echo $error;
May 13 '07 #3

P: 4
TRY with
Expand|Select|Wrap|Line Numbers
  1. $getuser = "SELECT UserID,Password FROM User WHERE (UserID = '$userid' AND Password = '$password')";
  2.  
May 13 '07 #4

Atli
Expert 5K+
P: 5,058
If you are using UserID as a primary key, you dont want the quote marks arround it in your WHERE clause. The quote marks are ment for strings, ID's are integers.

You are using the mysql_error() function incorrectly, here is an example of how to use it.
[PHP]
// Connect to database
$DB = @mysql_connect("localhost", "user", "pass") or die("Connection failed: ". mysql_error());
@mysql_select_db("myDB", $DB) or die("Database selection failed: ". mysql_error($DB));

// Build query
$SQL = "SELECT * FROM myTable";

// Execute query
$RESULT = @mysql_query($SQL, $DB);

// Check results
if(!!$RESULT) {
// Query OK. Do stuff!
}
else {
// Query Failed. Print error
echo "SQL Query failed:
<blockquote>$SQL</blockquote>
<blockquote>". mysql_error($DB) ."</blockquote>";

}
[/PHP]
May 13 '07 #5

Atli
Expert 5K+
P: 5,058
TRY with
Expand|Select|Wrap|Line Numbers
  1. $getuser = "SELECT UserID,Password FROM User WHERE (UserID = '$userid' AND Password = '$password')";
  2.  
Putting (..) arround the conditions in this query does nothing. They are used to group conditions together in more complex queries.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM usertbl
  2. WHERE
  3.   (ID = 1 OR Name = 'ATLI') 
  4. AND 
  5.   (Password = sha('Password') OR isLoggedIN = true)
  6.  
This query is far from perfect, but you get my point.
May 13 '07 #6

ak1dnar
Expert 100+
P: 1,584
Am i losing my mind? The following mysql query seems to fail even though i am running it outside of the PHP code and its working fine. An echo statement shows that the select variables are being populated as expected. can anyone advise?
[PHP]
$userid = $_POST['userid'];
$password = $_POST['password'];

$getuser = "SELECT UserID,Password FROM User WHERE UserID = '$userid' AND Password = '$password'";

$userlogin=mysql_query($getuser);

while ($row = mysql_fetch_array($userlogin)) {
$loginid = $row['UserId'];
$loginpwd = $row['Password']; }

if (!$userlogin) {
exit('<p>There was a problem recalling your user details<p>');
}
[/PHP]
This coding is working perfectly with my Tables.
[PHP]<?php
require 'dbcon.php';
$userid = 1;
$password = 'Mike';
$getuser = "SELECT region,staffid FROM users WHERE id = '$userid' AND firstname = '$password'";
$userlogin=mysql_query($getuser) or die (mysql_error());

while ($row = mysql_fetch_array($userlogin))
{
echo $loginid = $row['region'];
echo $loginpwd = $row['staffid'];
}
if (!$userlogin)
{
exit('<p>There was a problem recalling your user details<p>');
}
?>[/PHP]

with some sample values to the POST Strings. if there you getting a problem check your HTML form also.
You can do it by simply echoing the POST strings and also the Sql String.

[PHP]echo $userid;
echo $password;
echo $getuser;
[/PHP]
May 14 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Simple question: have you connected to your database?
May 14 '07 #8

P: 12
Motoma - you are a genius. And I am a moron. I had forgotton to call the include file making the database connection. how embarassing. In any case thanks for your help everyone.
May 14 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.