Solved

MySQL query select

Posted on 2011-03-20
4
272 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

770 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