Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple SELECT query not working

Newbie
 
Join Date: May 2007
Posts: 12
#1: May 13 '07
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]

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#2: May 13 '07

re: Simple SELECT query not working


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?
Newbie
 
Join Date: May 2007
Posts: 12
#3: May 13 '07

re: Simple SELECT query not working


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;
Newbie
 
Join Date: May 2007
Posts: 4
#4: May 13 '07

re: Simple SELECT query not working


TRY with
Expand|Select|Wrap|Line Numbers
  1. $getuser = "SELECT UserID,Password FROM User WHERE (UserID = '$userid' AND Password = '$password')";
  2.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#5: May 13 '07

re: Simple SELECT query not working


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]
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#6: May 13 '07

re: Simple SELECT query not working


Quote:

Originally Posted by hugonot

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.
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#7: May 14 '07

re: Simple SELECT query not working


Quote:

Originally Posted by captainmerton

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]
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#8: May 14 '07

re: Simple SELECT query not working


Simple question: have you connected to your database?
Newbie
 
Join Date: May 2007
Posts: 12
#9: May 14 '07

re: Simple SELECT query not working


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