Page 1 of 1

code help

Posted: Tue Jun 22, 2010 9:53 pm
by Taneya
hi all,

I'm a newbie to php, could someone help? where and how do i append this code to include the piece i need that relates to id fields from two databases together? the piece i need to add is "WHERE names.nameid = news.id"

$a = false;
$sql = "SELECT * FROM names,news WHERE ";

if ( $_REQUEST['fullname'] ) {
$sql .= "name LIKE '%" . $_REQUEST['fullname'] . "%' ";
$a = true;
}
if ( $_REQUEST['nametype'] ) {
if ($a) $sql .= "AND ";
$sql .= "nametype LIKE '%" . $_REQUEST['nametype'] . "%' ";
$a = true;
}
if ( $_REQUEST['county'] ) {
if ($a) $sql .= "AND ";
$sql .= "county LIKE '%" . $_REQUEST['county'] . "%' ";
$a = true;
}
if ( $_REQUEST['articletype'] ) {
if ($a) $sql .= "AND ";
$sql .= "type = '" . $_REQUEST['type'] . "' ";
$a = true;
}

if ( $_REQUEST['state'] ) {
if ($a) $sql .= "AND ";
$sql .= "state = '" . $_REQUEST['state'] . "' ";
$a = true;
}
if ( $_REQUEST['issue'] ) {
if ($a) $sql .= "AND ";
$sql .= "issue = '" . $_REQUEST['issue'] . "' ";
$a = true;
}

$sql .= "ORDER BY name";

thanks in advance

Re: code help

Posted: Wed Jun 23, 2010 3:19 am
by rahulzatakia
Hi, you can try out the following code. And if you have any query let me know...

original code

Code: Select all

$sql = "SELECT * FROM names,news WHERE ";
Please replace the above code with below code

Code: Select all

$sql = "SELECT names.*, news.* FROM names inner join news on names.nameid = news.id where";