Solved

MySQL query select

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

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
Star! Your solution worked perfectly.
Thanks,
Neil
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
Comment Utility
Perfection. Excellent quick responce.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Thanks, I get them right sometimes!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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.

743 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

17 Experts available now in Live!

Get 1:1 Help Now