Solved

MySQL query select

Posted on 2011-03-20
4
271 Views
Last Modified: 2013-12-13
An unusual problem has just cropped up that I am having some problems figuring out.

A simple query I have used may times is having trouble returing values when the username is anything other than a number. When The username is a text value my query returns "helloUnknown column 'myUsername' in 'where clause'".

The username MySQL value is varchar. Any ideas?
Cheers,
N
<?php  
  // connect to db
  $conn = db_connect();
  // find information dependant on username
  $result = $conn->query("SELECT * FROM members
                         WHERE username=".$_SESSION['valid_user']."");
						 if (!$result) {
     echo'<p>There was a problem retrieving data from the database. Please try again later</p>';
  } else {
     $row = $result->fetch_assoc();
  }
 ?>

Open in new window

0
Comment
Question by:Neil_Bradley
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 35177031
I think you're missing single quotes around the username value.  Try this.
<?php  
  // connect to db
  $conn = db_connect();
  // find information dependant on username
  $result = $conn->query("SELECT * FROM members
                         WHERE username='".$_SESSION['valid_user']."'");
                                                 if (!$result) {
     echo'<p>There was a problem retrieving data from the database. Please try again later</p>';
  } else {
     $row = $result->fetch_assoc();
  }
 ?>

Open in new window

0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 35177063
Star! Your solution worked perfectly.
Thanks,
Neil
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 35177065
Perfection. Excellent quick responce.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35177070
Thanks, I get them right sometimes!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now