Page 1 of 1

in php using mysql query with joins not returing any values

Posted: Mon Mar 06, 2017 2:20 am
by newbaba
the code mentioned here is that i am running a query which works fine in mysql without any user input i am trying to execute it with user input
the form part where the user input is taken
<form action="result.php" method="post">
<input type="text"name="input"size=40>
<input type="submit"value="SUBMIT">
and the result part

Code: Select all

 <?php 
 
$dbuser = 'root'; 
$dbpass = ''; 
$db='owaiskhan';
$dbhost='localhost';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
		
$input =  mysqli_real_escape_string($conn, $_POST['input']);

$result="SELECT answers_1.*, answers_2.*, answers_3.*, answers_4.*, biodata.* FROM answers_1 INNER JOIN answers_2 ON
 answers_1.anseng_id = answers_2.anseng_id AND answers_1.anseng_id INNER JOIN biodata ON biodata.anseng_id = answers_1.anseng_id INNER JOIN
 answers_3 ON biodata.anseng_id = answers_3.anseng_id INNER JOIN answers_4 ON biodata.anseng_id=answers_4.anseng_id WHERE biodata.anseng_id='".$input."' " ;
		//echo "$query";
		echo"$result";
 ?>
the issue here is that the query gets the output instead of the mysql rows values please help me out thanks in advance

Re: in php using mysql query with joins not returing any val

Posted: Mon Mar 06, 2017 3:54 am
by requinix

Re: in php using mysql query with joins not returing any val

Posted: Mon Mar 06, 2017 11:11 pm
by newbaba
Thanks for your prompt reply
basically my question was how to execute mysql query on user input

Code: Select all


<?php
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db='basicdb';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db); 
if(! $conn ) 
		{ 
			die('Could not connect: ' . error); 
		} 
		$search_query = mysqli_real_escape_string($conn,$_POST['input']);
$sql = 'SELECT answers_1.option31, answers_2.option16, answers_3.option1, biodata.name FROM answers_1
INNER JOIN answers_2 ON answers_1.anseng_id = answers_2.anseng_id AND answers_1.anseng_id  INNER JOIN biodata ON biodata.anseng_id = answers_1.anseng_id 
INNER JOIN answers_3 ON biodata.anseng_id = answers_3.anseng_id WHERE biodata.anseng_id='".search_query."'';
$result=mysqli_query($conn,$sql)
?>
<table width=10% cellspacing="1">
<?php while ($rows = mysqli_fetch_array($result)): ?>
 
             <?php echo $rows['option31'];?>
			<id= value="<?php echo $rows['option16'];?>">
			<br>
			 <br>
			<?php   echo $rows['option1'];  ?> <br>
			<?php   echo $rows['name'];  ?> <br>
			<br>
		
			<?php endwhile; ?>
			<?php mysqli_close($conn); ?>

i am trying to execute mysql query on user input but getting this error mysqli (mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given) and the query is perfect if no user input is required

Re: in php using mysql query with joins not returing any val

Posted: Tue Mar 07, 2017 2:56 am
by requinix
Your code has a syntax error. Presumably there's (also?) one in your query, but I can't be sure because what you've posted will not execute.

Use mysqli_error to get an error message.