Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL query select

Posted on 2011-03-20
4
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

715 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