Solved

Mysql query php syntax

Posted on 2013-01-17
8
357 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

919 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