Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql query php syntax

Posted on 2013-01-17
8
Medium Priority
?
368 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
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 84

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 84

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 84

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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

610 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