473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple SELECT query not working

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
8 1988
Atli
5,058 Expert 4TB
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
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
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
5,058 Expert 4TB
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
5,058 Expert 4TB
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
1,584 Expert 1GB
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
3,237 Expert 2GB
Simple question: have you connected to your database?
May 14 '07 #8
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

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

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
20
by: max | last post by:
Hi! I'm having a problem with a very simple drop-down list since the page comes out with no elements in the drop-down but giving no errors. This is the code: <form name="year_search_form"...
6
by: firewire888 | last post by:
I tried a bunch of sql queries, but can't get this sql correct. Any help would be appreciated. Thanks. I have a two field table, key1, key2 key1 field can have dupes key2 field is...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
6
by: dkintheuk | last post by:
Hi all, I have a form with some unbound combo's and a list box that are used to requery the dataset behind the form. All was working fine until i added the list box. I have the record set...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
5
by: kkddrpg | last post by:
the database looks like this the database is called username_tpp (not really just using username as a sub) the table is called home it has field 1 : varchar(50) | latin1_swedish_ci | no...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.