I have the SQL query that is fetching data from multiple columns and fields, with the primary and foreign keys. i am trying to run the code on the search script but on var_dump of the query, i get the query as follows:
SELECT `vendor`.`v_id`
,`vendor`.`v_name`
,`vendor`.`v_img`
,`contacts`.`cont_id`
,`contacts`.`cont_addr`
,`contacts`.`cont_phn`
,`fooditem`.`fi_id`
,`fooditem`.`item_name`
,`fooditem`.`fi_price`
,`fooditem`.`fc_desc`
,`vendor`.`v_id`
,`vendor`.`v_name`
,`vendor`.`v_img`
,`contacts`.`cont_id`
,`contacts`.`cont_addr`
,`contacts`.`cont_phn`
,`fooditem`.`fi_id`
,`fooditem`.`item_name`
,`fooditem`.`fi_price`
,`fooditem`.`fc_desc`
FROM vendor
LEFT JOIN `stfood`.`contacts`
ON `vendor`.`cont_id` = `contacts`.`cont_id`
LEFT JOIN `stfood`.`fooditem`
ON `vendor`.`v_id` = `fooditem`.`v_id`
WHERE `vendor`.`v_name` LIKE '%ven%'
OR `fooditem`.`item_name` LIKE '%ven%'
and when i proceed forward with the var_dump of the above query i get a bool(false) but this query runs flawlessly on PHP MyAdmin Can anyone tell me why is this so?
The code as requested by "Hirdesh Vishwdewa"
<?php
include_once "init.php";
include_once "functions.php";
//grab the contents to local variable.
$keysearch = $_POST['search_field'];
//sanitize the input contents.
$keysearch = sanitize($keysearch);
//check is the field is empty.
if ($keysearch == "") {
$errors[] = 'Please enter a search keyword to help you order your favorite food.';
} else {
//if the field is not empty, run the sql query.
$find_query = "SELECT `vendor`.`v_id`,`vendor`.`v_name`,`vendor`.`v_img`,`contacts`.`cont_id`,`contacts`.`cont_addr`,`contacts`.`cont_phn`,`fooditem`.`fi_id`,`fooditem`.`item_name`,`fooditem`.`fi_price`,`fooditem`.`fc_desc`,`vendor`.`v_id`,`vendor`.`v_name`,`vendor`.`v_img`,`contacts`.`cont_id`,`contacts`.`cont_addr`,`contacts`.`cont_phn`,`fooditem`.`fi_id`,`fooditem`.`item_name`,`fooditem`.`fi_price`,`fooditem`.`fc_desc` FROM vendor LEFT JOIN ".DBNAME.".`contacts` ON `vendor`.`cont_id` = `contacts`.`cont_id` LEFT JOIN ".DBNAME.".`fooditem` ON `vendor`.`v_id` = `fooditem`.`v_id` WHERE `vendor`.`v_name` LIKE '%".$keysearch."%' OR `fooditem`.`item_name` LIKE '%".$keysearch."%'";
$result = mysqli_query($con, $find_query);
$rowcount = mysqli_num_rows($result);
?>
//init.php
<?php
if(!isset($_SESSION))
{
session_start();
}
error_reporting(E_ALL);
define("DBNAME","stfood");
global $con;
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$con = mysqli_connect($servername, $username, $password);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>
//function sanitize
function sanitize($data) {
return preg_replace('#[^a-z 0-9]#i', '', $data);
}
also to note a point here that if i put the following query for $find_query the code runs as intended. THE FOLLOWING SQL QUERY RUNS:
SELECT `vendor`.`v_id`,`vendor`.`v_name`,`vendor`.`v_img`,`contacts`.`cont_addr`,`contacts`.`cont_phn`,`fooditem`.`item_name`,`fooditem`.`fi_price`,`fooditem`.`fc_desc` FROM ".DBNAME.".`vendor` LEFT JOIN ".DBNAME.".`contacts` ON `vendor`.`cont_id` = `contacts`.`cont_id` LEFT JOIN ".DBNAME.".`fooditem` ON `vendor`.`v_id` = `fooditem`.`v_id` WHERE `vendor`.`v_name` LIKE '%".$keysearch."%' OR `fooditem`.`item_name` LIKE '%".$keysearch."%'
the difference between these 2 queries is that in the query above (that gives me a bool(false)) i have selected the tables primary keys i.e fi_id & cont_id so that i can use them later. When i run this query, it gives me a mysqli_result object. and from there i am able to fetch the results and echo them in the applicable fields. So i want to know that why the query that is under question not working??
Thanks.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire