Solved

Mysql query php syntax

Posted on 2013-01-17
8
361 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 100 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
Industry Leaders: 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!

 
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 200 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

Independent Software Vendors: 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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

685 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