in php using mysql query with joins not returing any values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

in php using mysql query with joins not returing any values

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

newbaba
Forum Newbie
Posts: 17
Joined: Thu Jan 26, 2017 10:36 pm
Location: Karachi

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply