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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg AlexanderLead DeveloperCommented:
Try

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Learn 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.