Saturday, 31 August 2013

How do I use wildcards with php and mysql prepared statements?

How do I use wildcards with php and mysql prepared statements?

I have followed examples from other people on here who also had the same
problem but I can't get it to work. I just changed my code to use prepared
statements for safety and before the change I would get results from the
query using a certain string for $prof and now that same string won't
return any results. the query is empty every time. I don't receive any
errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$tempProf = $_POST["professor"];
$tempProfArray = explode("=",$tempProf);
$prof = $tempProfArray[1];
$postString = '';
$con=mysqli_connect("localhost","root",".......","......");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(empty($prof)) {echo "notFound";}
else
{
$stmt = $con->prepare("SELECT * FROM professors WHERE name LIKE ?");
$prof = '%' . $prof . '%';
$stmt->bind_param('i', $prof);
$stmt->execute();
$profTemp = $stmt->get_result();
while ($profResult = $profTemp->fetch_assoc())
{
$postString .= $profResult['id'];
$postString .= ".";
$postString .= $profResult['name'];
$postString .= "!";
}
if(!empty($postString))
{
echo $postString;
}
else
{
echo "notFound";
}
}
?>

No comments:

Post a Comment