PHP mysql issue while retreving data using select * from table using where claus

Facing issue while retrieving data from table using select command with where clause. my code is as below

if(isset($_POST[‘dcode’]))
{
$dcode = $_POST[‘dcode’];
if(empty($dcode))
{
$dcode = ‘ALL’;
}
}else {
$dcode = ‘ALL’;
}

$db = mysqli_connect(‘localhost’, ‘root’,’’,‘mydb’);

if(!$db)
{
print "

Unable to Connect to MySql ";
}

print "

Connected to database “.$dcode.”";

$sql_statement = “SELECT NEIS, NAME, DESIGN, GRADE, DCODE, UNIT, DOA, DOPP1, DOPP2, DOPP3, DOPP4 FROM wani1”;

if($dcode !=‘ALL’)
{
$sql_statement .= “WHERE DCODE = '”.$dcode."’";
}

You should be using prepared statements, it will prevent easy attacks on your database.

print the $sql_statement, you should see something like this come out.

SELECT NEIS, NAME, DESIGN, GRADE, DCODE, UNIT, DOA, DOPP1, DOPP2, DOPP3, DOPP4 FROM wani1WHERE DCODE = '';

You are missing a space before the where clause.

Sponsor our Newsletter | Privacy Policy | Terms of Service