Solved

Mysql query php syntax

Posted on 2013-01-17
8
354 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
Comment Utility
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
Comment Utility
Thanks Roads, but I still get no results with that syntax.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Hmmm, there's nothing wrong with the query ... How do you test it ? Phpmyadmin, php page ?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I made up a test page using $email copied from your first post and it works fine.  There is something else going on.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dsg138
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 200 total points
Comment Utility
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
Comment Utility
Perfect!  That did it, thanks Dave.
Also. thanks to Roads too for initially solving the syntax issue.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
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 …

772 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

11 Experts available now in Live!

Get 1:1 Help Now