?
Solved

Mysql query php syntax

Posted on 2013-01-17
8
Medium Priority
?
367 Views
Last Modified: 2013-01-17
I have a simple php query to find users with the same email address.
This query results in 0 records
$email = "dan@email.com";
$query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE email = '$email'");
//This results in 0 records.

Open in new window


This query correctly returns 7 records.
$query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE email = 'dan@email.com'");
//This results in 7 records

Open in new window


Isn't this the exact same thing?  I'm unable to hardcode the email address.  Any ideas on this syntax?
0
Comment
Question by:dsg138
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 400 total points
ID: 38788466
Try this:
$email = "dan@email.com";
$query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE email = '".$email."'")

Open in new window

0
 

Author Comment

by:dsg138
ID: 38788662
Thanks Roads, but I still get no results with that syntax.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38788724
Hmmm, there's nothing wrong with the query ... How do you test it ? Phpmyadmin, php page ?
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38788860
I made up a test page using $email copied from your first post and it works fine.  There is something else going on.
0
 

Author Comment

by:dsg138
ID: 38788918
Yeah, I think something else is going on but I can't pinpoint it....

This is the page I'm using for testing...  Currently the email address is hardcoded.
http://www.officepickem.com/forgot-username.php

$email = "dan@email.com";

	function getSongs() {       
	//Get all Users associated with this email address
	$songs=array();	
      $query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE USERS.email = 'dan@email.com'");  // Works correctly when hardcoded

     // $query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE USERS.email = '".$email."'");// doesn't work when using $email

	  while($result_songs = mysql_fetch_object($query)){
            $songs[]  = array( 'id'=>$result_songs->userid, 'fname'=>$result_songs->FirstName,'lname'=>$result_songs->FirstName);                  
      }
      return $songs;
}
$allSongs = getSongs();
$yyy = "";
$xxx = "";
      
	  foreach( $allSongs as $Songs){
	  $xxx = "Name: ";
	  $xxx = $xxx . $Songs['fname'] . " " .$Songs['lname'] .  "  -  Username: ";
	  $xxx = $xxx . $Songs['id'];
	  $xxx = $xxx . "<BR>";
		$yyy = $yyy . $xxx;
		$xxx = "";		
		}
	echo "Here are the User Accounts associated with your email address:<BR>";
	echo $yyy;		

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38788948
Here is my demo page that works.  Enter the values needed for you database and email address.
<?php 
$dbhost = "Your database server";    // Your database server
$dbuser = "username";      // Your db username
$dbpass = "password";      // Your db password
$dbname = "database";      // Your database name
$dbtable = "USERS";  // the table we're using

$dbh = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>eecontact2.php</title>
</head>
<body>
<h1>eecontact2.php</h1>
<table border="0" cellpadding="0" cellspacing="0" summary="">
<?php 
$email = "dan@email.com";
$result = mysql_query("SELECT userid, FirstName, LastName FROM $dbtable WHERE email = '$email'");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<tr bgcolor="#ffffff">';
echo '<td>&nbsp;'.$row[0].'</td>';
echo '<td>&nbsp;'.$row[1].'</td>';
echo '<td>&nbsp;'.$row[2].'</td>';
echo '</tr>';
echo "\r\n";
}

?>
</table><br />

</body>
</html>

Open in new window

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 800 total points
ID: 38788973
I think I just realized what's going on.  You are not passing '$email' to the function.
$email = "dan@email.com";

function getSongs($email) {       
  //Get all Users associated with this email address
  $songs=array();	
  //$query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE USERS.email = 'dan@email.com'");  // Works correctly when hardcoded

  $query = mysql_query("SELECT userid, FirstName, LastName FROM USERS WHERE USERS.email = '".$email."'");// doesn't work when using $email

  while($result_songs = mysql_fetch_object($query)){
    $songs[]  = array( 'id'=>$result_songs->userid, 'fname'=>$result_songs->FirstName,'lname'=>$result_songs->FirstName);                  
    }
  return $songs;
}
$allSongs = getSongs($email);

Open in new window

0
 

Author Closing Comment

by:dsg138
ID: 38789099
Perfect!  That did it, thanks Dave.
Also. thanks to Roads too for initially solving the syntax issue.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 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 …
Suggested Courses

777 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