Solved

MySQL query select

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Extracting content from meta tag PHP MYHTML DOM 3 34
Get value in session when using dynamic information 24 38
PHP function parameters defined 14 39
$_GET call between URL 3 37
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

713 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