PHP/MySQL "SELECT" syntax

I'm a newbe with a PHP/MySQL syntax issue.

I am getting an error on the line "$data = SELECT" in the code below, can someone help?

<?php

include("sprqVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");


$search1=$_POST['search1'];
$search2=$_POST['search2'];

$data = 'SELECT * FROM `SPRQ` WHERE `FN` = "'.$search1.'" AND 'LN' = "'.$search2.'"';
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $data2 = mysql_fetch_array($query);


   
?>
dibrandtAsked:
Who is Participating?
 
Greg AlexanderLead DeveloperCommented:
Try

$data = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"';
0
 
bklyn2001Commented:
try taking out the quotes:

$data = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"'; 

Open in new window

0
 
JRandelCommented:
Try:

$data = 'SELECT * FROM `SPRQ` WHERE `FN` = "'.$search1.'" AND `LN` = "'.$search2.'"';

Without more error information it's a little hard to say specifically if I've corrected it or not.

Try running it in MySQL workbench or PHP MyAdmin for a specific error
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
p_nutsCommented:
Quotes is the problem..

Take all the quotes out like suggested..
0
 
dibrandtAuthor Commented:
I removed athe quotes as suggested, no error and the form opened however;

no data was reterived from the table!

FN & LN are strings (text not numbers) could that be part of the problem or could it be the variables "search1" and "search2" not being passed to the query?
0
 
Greg AlexanderLead DeveloperCommented:
What does the output of the attached look like? This will let you see the query so you will know if you are getting your two variables
<?php 
include("sprqVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");

$search1 = $_POST['search1']; 
$search2 = $_POST['search2']; 

$data = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"';
echo $data;

$query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
$data2 = mysql_fetch_array($query);   
?>

Open in new window

0
 
Greg AlexanderLead DeveloperCommented:
Or even run the attached too
<?php 
include("sprqVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");

$search1 = $_POST['search1']; 
$search2 = $_POST['search2']; 

$query = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"';
echo $data;

$result = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
	$data[] = $row;
}

echo "<pre>";
print_r($data);
echo "</pre>";
?>

Open in new window

0
 
p_nutsCommented:
Try

'SELECT * FROM SPRQ WHERE FN like "%'.$search1.'%" or Ln = "%'.$search2.'%"';
0
 
dibrandtAuthor Commented:
thanks, sometimes you overlook the obvious test!!

Ther echo is:

SELECT * FROM `SPRQ` WHERE `FN` = "John" AND `LN` = "Duncon"  

which is correct.  The variables are being passed correctly, but the data in that row is not getting "fetched" to the array "$data2"

The row exist (I checked the table) with colum FN=John, and LN=Duncon.


0
 
p_nutsCommented:
Change $data2 = MySQL.....

To
While($data2 = mysql ..... )
{
Print_r($data2);
}
0
 
Greg AlexanderLead DeveloperCommented:
Did you try my second solution?
0
 
Shinesh PremrajanEngineering ManagerCommented:
Hope this helps
<?php 
include("sprqVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");

$search1 = $_POST['search1']; 
$search2 = $_POST['search2']; 

$query = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"';
echo $query;

$result = mysql_query($query) or die("Couldn't execute query. ". mysql_error()); 

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
	$data[] = $row;
}

echo "<pre>";
print_r($data);
echo "</pre>";
?>

Open in new window

0
 
dibrandtAuthor Commented:
galexander07,

I trier your second solution, and it produced the following:

Couldn't execute query. Query was empty
0
 
Greg AlexanderLead DeveloperCommented:
Opps try the attached
<?php 
include("sprqVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");

$search1 = $_POST['search1']; 
$search2 = $_POST['search2']; 

$query = 'SELECT * FROM SPRQ WHERE FN = "'.$search1.'" AND LN = "'.$search2.'"';
echo $query;

$result = mysql_query($query) or die("Couldn't execute query. ". mysql_error()); 

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        $data[] = $row;
}

echo "<pre>";
print_r($data);
echo "</pre>";
?>

Open in new window

0
 
dibrandtAuthor Commented:
This solution was first to address the question asked.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.