473,473 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Selecting individual records in mySQL

40 New Member
I'm trying to check a user name in a database and get it into a variable so I can perform later checks on it. Only problem is that for some reason when I use a variable in the SQL command, I get back nothing.

Expand|Select|Wrap|Line Numbers
  1. $query = " SELECT `user_name`
  2. FROM `users`
  3. WHERE `user_name` LIKE 'John' ";
  4.  
  5. // Perform the query
  6. $result = mysql_query($query) ;
  7.  
  8. $name = mysql_result($result,0,"user_name");
  9.  
  10. echo $name;
The code above gives me back the user name entered in the SQL select query, but when I try to use a $_POST variable in place of 'John' in there I get nothing. I've also tried putting the $_POST var into a new variable, but to no avail.

Can someone kindly show me where I'm going astray?
May 20 '08 #1
10 2158
hsriat
1,654 Recognized Expert Top Contributor
Can you tell how did you try that with $_POST['variable']?
May 20 '08 #2
TheServant
1,168 Recognized Expert Top Contributor
Yeah, that won't work because you are probably opening and closing MySQL statements unintentionally when you replace John with $_POST['name']. You should declare it first and avoid any confusion:

Expand|Select|Wrap|Line Numbers
  1. $input_name = $_POST['name'];
  2.  
  3. $query = " SELECT `user_name`
  4. FROM `users`
  5. WHERE `user_name` LIKE '$input_name' ";
  6.  
  7. // Perform the query
  8. $result = mysql_query($query) ;
  9.  
  10. $name = mysql_result($result,0,"user_name");
  11.  
  12. echo $name;
I'm not sure if you were saying you have tried this, but just check you have it all like I have it.
May 20 '08 #3
BOMEz
40 New Member
Yeah, that won't work because you are probably opening and closing MySQL statements unintentionally when you replace John with $_POST['name']. You should declare it first and avoid any confusion:

Expand|Select|Wrap|Line Numbers
  1. $input_name = $_POST['name'];
  2.  
  3. $query = " SELECT `user_name`
  4. FROM `users`
  5. WHERE `user_name` LIKE '$input_name' ";
  6.  
  7. // Perform the query
  8. $result = mysql_query($query) ;
  9.  
  10. $name = mysql_result($result,0,"user_name");
  11.  
  12. echo $name;
I'm not sure if you were saying you have tried this, but just check you have it all like I have it.
I do have a variable set to the $_POST value higher up just as above.

@ hsriat: to use the post value I just replace John with $_POST['user_name']
May 20 '08 #4
dlite922
1,584 Recognized Expert Top Contributor
I'm trying to check a user name in a database and get it into a variable so I can perform later checks on it. Only problem is that for some reason when I use a variable in the SQL command, I get back nothing.

Expand|Select|Wrap|Line Numbers
  1. $query = " SELECT `user_name`
  2. FROM `users`
  3. WHERE `user_name` LIKE 'John' ";
  4.  
  5. // Perform the query
  6. $result = mysql_query($query) ;
  7.  
  8. $name = mysql_result($result,0,"user_name");
  9.  
  10. echo $name;
The code above gives me back the user name entered in the SQL select query, but when I try to use a $_POST variable in place of 'John' in there I get nothing. I've also tried putting the $_POST var into a new variable, but to no avail.

Can someone kindly show me where I'm going astray?





Add a mysql Error Number with it and print the SQL like so then give us the result:


Expand|Select|Wrap|Line Numbers
  1.  
  2. $query = " SELECT `user_name`
  3. FROM `users`
  4. WHERE `user_name` LIKE 'John' ";
  5.  
  6. // Perform the query
  7. $result = mysql_query($query)  or die(mysql_error() . "\nThe SQL is: " . $query);
  8.  
  9. $name = mysql_result($result,0,"user_name");
  10.  
  11. echo $name;
that way when the query fails, it will halt and print the error and what the SQL was.
May 20 '08 #5
TheServant
1,168 Recognized Expert Top Contributor
I do have a variable set to the $_POST value higher up just as above.
Are you sure in the MySQL statement you have inverted commas around teh variable like I have? If you echo your $_POST['input_name'] variable does it exist?
May 21 '08 #6
ronverdonk
4,258 Recognized Expert Specialist
@ hsriat: to use the post value I just replace John with $_POST['user_name']
When this is true the statement will never work, because the WHERE clause will become
Expand|Select|Wrap|Line Numbers
  1. WHERE `user_name` LIKE '$_POST['user_name']' ";
  2.  
and that is incorrect. But why are you reluctant to show your code and keep us guessing? Just show the code you did using the $_POST variable in the WHERE clause and I mean the code, not a description.

Ronald
May 21 '08 #7
BOMEz
40 New Member
When this is true the statement will never work, because the WHERE clause will become
Expand|Select|Wrap|Line Numbers
  1. WHERE `user_name` LIKE '$_POST['user_name']' ";
  2.  
and that is incorrect. But why are you reluctant to show your code and keep us guessing? Just show the code you did using the $_POST variable in the WHERE clause and I mean the code, not a description.

Ronald
Below is all th code I have in the file:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.  
  4. $user_login = $_POST['user_name'];
  5. $pass_login = $_POST['password'];
  6.  
  7. echo $_POST['user_name']; // Post value echos just fine
  8. echo "<br>";
  9. echo $pass_login; // Post value stored in variable echos just fine
  10. echo "<br>";
  11.  
  12. $user = myuser_name_dot ;  //database user name
  13. $pass = my_pass; // database password
  14. $db = my_database; // name of database
  15.  
  16. mysql_connect(localhost,$user,$pass); // connects to database
  17. @mysql_select_db($db) or die ("Could not connect"); //selects database
  18.  
  19. $query = " SELECT `user_name`
  20. FROM `users`
  21. WHERE `user_name` LIKE '$user_login' ";
  22.  
  23. // Perform the query
  24. $result = mysql_query($query) or die(mysql_error() . "\nThe SQL is: " . $query); ;
  25.  
  26. $name = mysql_result($result,0,"user_name");
  27.  
  28. echo $name;
  29.  
  30. ?>
The error I am getting is :

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home/mysite/public_html/ravey/login_check.php on line 26
May 21 '08 #8
TheServant
1,168 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. mysql_connect(localhost,$user,$pass); // connects to database
  2. @mysql_select_db($db) or die ("Could not connect"); //selects database
  3.  
  4. // Perform the query
  5. $result = mysql_query(" SELECT `user_name`
  6. FROM `users`
  7. WHERE `user_name` LIKE '$user_login' ") or die(mysql_error() . "\nThe SQL is: " . $query);
  8. $name = mysql_fetch_array($result);
  9. echo $name['user_name'];
  10. ?>
Try that. I basically used msql_fetch_array rather than mysql_result so that rows are not important. The $name is an array, so while you're only calling one variable from the table you can just call it $name, but if you call more than one variable, you will need to specify which part of the array you want: $name['user_name'].
May 22 '08 #9
BOMEz
40 New Member
Expand|Select|Wrap|Line Numbers
  1. mysql_connect(localhost,$user,$pass); // connects to database
  2. @mysql_select_db($db) or die ("Could not connect"); //selects database
  3.  
  4. // Perform the query
  5. $result = mysql_query(" SELECT `user_name`
  6. FROM `users`
  7. WHERE `user_name` LIKE '$user_login' ") or die(mysql_error() . "\nThe SQL is: " . $query);
  8. $name = mysql_fetch_array($result);
  9. echo $name['user_name'];
  10. ?>
Try that. I basically used msql_fetch_array rather than mysql_result so that rows are not important. The $name is an array, so while you're only calling one variable from the table you can just call it $name, but if you call more than one variable, you will need to specify which part of the array you want: $name['user_name'].
Thanks alot. I got it to work, but I had to include CONVERT( _utf8 and COLLATE latin1_swedish_ci or else it wouldn't work for some reason...possibly my server configuration. I figured I would just copy the code phpMyAdmin gave me and it worked.
May 23 '08 #10
TheServant
1,168 Recognized Expert Top Contributor
Thanks alot. I got it to work, but I had to include CONVERT( _utf8 and COLLATE latin1_swedish_ci or else it wouldn't work for some reason...possibly my server configuration. I figured I would just copy the code phpMyAdmin gave me and it worked.
Glad you got it to work. See you next time.
May 25 '08 #11

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

Similar topics

4
by: webhigh | last post by:
I¹m not sure if this a PHP question or an MySQL question but here it goes. I have a repeat region of a table called userid What I¹m trying to accomplish is being able to edit the record and...
2
by: C.F. Scheidecker Antunes | last post by:
Hello all, I wonder if is there any way to select from a table all the records which has a Date Field that is at least five minutes old? In other words, I have a table with a date field and I...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
5
by: mikevde | last post by:
Hi, Can anyone confirm whether it is possible to: a) use subqueries in mySQL b) use DISTINCT TOP in mySQL? I am trying to find a way to return the top 5 results in my table. For simplicity...
5
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.