Hello Experts !!!

I have two databases to select from my web page. In this script i can search one database(if ($choice=="staff publication") it works. I dont know how to write the script if I select the other option if($choice=="books"). Please can any one help me. I want to display search resuls of "books" the same way as staff publications. but books database has different fields.

if($choice=="books"){
$query="SELECT * FROM books WHERE match (authors, title, callnumber keywords, edition, year, notes) against ('$name' in boolean mode) ORDER BY year DESC";
}

I want to put the above somewhere in the script and display results.

<?php
$link = mysql_connect('localhost', 'root', 'vip');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test1');

@mysql_select_db(test1) or die("unable to connect");

$choice=$_POST['choice'];
$name=$_POST['name'];

if ($choice=="staff publications"){

$query="SELECT * FROM staffpub WHERE match (authors, title, source, keywords, mesh, year, notes) against ('$name' in boolean mode) ORDER BY year DESC";

}


$result=mysql_db_query("test1", $query);
$num_rows = mysql_num_rows($result);
if ($num_rows==0){$search_result="<p class='error'>No Results Found</p>";}
elseif ($num_rows > 0){$search_result="<p class='error'>".$num_rows. " Results Found</p>";}


mysql_close();

echo '<b><center><font size="4" color="#FF0000">Search Result</font></center></b><br><br>';

$i=0;
while ($i < $num_rows) {

$authors=mysql_result($result,$i,"authors");
$title=mysql_result($result,$i,"title");
$source=mysql_result($result,$i,"source");





echo "<b>Authors:</b> $authors<br><b>Title:</b> $title<br><b>Source:</b> $source<br><br><hr><br>";

$i++;

?>

Please use CODE tags next time. I have formatted your post.

Your code was missing some quotations as well, so I took the liberty to add them in. You probably just didn't copy and paste the text, right? :)

So how about using 'elseif'?

Such as :

if ($choice=="staff publications"){

$query="SELECT * FROM staffpub WHERE match (authors, title, source, keywords, mesh, year, notes) against ('$name' in boolean mode) ORDER BY year DESC";

} 
elseif($choice=="books")
{
$query="SELECT * FROM books WHERE match (authors, title, callnumber keywords, edition, year, notes) against ('$name' in boolean mode) ORDER BY year DESC";
}

Thank you very much Gary. I will try this today and let u know.I didnt aware of php tags. I am very new here. You are realy very helpful.

Hi Gary

Thanks its working. Actually that was not the only problem. When i select "books" it didnt pass to the PHP page because of case sensitivity of option. thanks a lot for your kind help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.