Hello I am currently having problems with if statements in php with prepared statements, no matter what I enter my when checking get_result my statements are always true. I honestly have no idea what Im doing I am very new to php and the concept of prepared statements.
$searchtext= $_POST['search-box'];
$sqlName= $connect->prepare("SELECT * FROM SearchPG WHERE PGName LIKE ? ");
$sqlName->bind_param("s", $searchtext);
$sqlName->execute();
$NameResult = $sqlName->get_result();
$sqltext= $connect->prepare("SELECT * FROM SearchPG WHERE PGKeywords LIKE ? ");
$sqltext->bind_param("s", $searchtext);
$sqltext->execute();
$TextResult = $sqltext->get_result();
if($NameResult->num_rows > 0 || $TextResult->num_rows > 0){
if($NameResult->num_rows > 0 ){
echo "made it into sqlname";
while($rs = $NameResult->fetch_assoc()){
echo "name result";
?>
<div class="row resultcontent">
<a href="<?php echo $rs['PGUrl'];?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$rs['PGName'];?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
if($count<10){
echo "exploded result";
foreach($searchbit as $value){
$temp = '%'.$value.'%';
$sqlvalue= $connect->prepare("SELECT * FROM SearchPG WHERE PGName LIKE ? OR PGKeyword LIKE ? ");
$sqlvalue->bind_param("ss", $temp, $temp);
$sqlvalue->execute();
$ValueResult = $sqlvalue->get_result();
if($ValueResult->num_rows > 0){
while($rs = $ValueResult->fetch_assoc()){
?>
<div class="row resultcontent">
<a href="<?php echo $rs['PGUrl'];?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$rs['PGName'];?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
}
while($rs = $TextResult->fetch_assoc()){
?>
<div class="row resultcontent">
<a href="<?php echo $rs['PGUrl'];?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$rs['PGName'];?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
I have tried if(!$result), if($result->num_rows>0), if($result->affected_rows) and others but none seem to work has anybody got any idea what I am doing wrong
There's a good chance you don't have the "native driver" (mysqlnd) installed, which is necessary for the get_result()
method.
cf. http://php.net/manual/en/mysqli-stmt.get-result.php
Try using PDO:
<?php
$dbh = new PDO(
'mysql:dbname=myDatabase;host=myHost',
'myUsername', 'myPassword');
$stmt = $dbh->prepare('SELECT * FROM SearchPG WHERE PGName LIKE ? ');
$stmt->bindParam(1, $searchtext, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
print_r($row);
}
For anybody that maybe new to php, prepared statements and having similar problems to me, I solved my problems by switching get_result to store_result after changing code to suit store_result things seemed to work flawlessly. Here is my new code it still needs some fixing and cleaning up but I hope this helps someone in need.
$sqlname= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGName LIKE ? ");
$sqlname->bind_param("s", $searchtext);
$sqlname->execute();
$sqlname->store_result();
$sqltext= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGKeywords LIKE ? ");
$sqltext->bind_param("s", $searchtext);
$sqltext->execute();
$sqltext->store_result();
if($sqlname->num_rows > 0 || $sqltext->num_rows > 0){
if($sqlname->num_rows > 0 ){
$sqlname->bind_result($id, $name, $url, $imgurl);
while($rs = $sqlname->fetch()){
?>
<div class="row resultcontent">
<a href="<?php echo $url;?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$name;?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
if($count<10){
if($words > 1){
foreach($searchbit as $value){
$temp = '%'.$value.'%';
$sqlvalue= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGName LIKE ? OR PGKeywords LIKE ? ");
if ( false===$sqlvalue ) {
die('connect failed: ' . htmlspecialchars($sqlvalue->error));
}
$sqlvalue->bind_param("ss", $temp, $temp);
if ( false===$sqlvalue ) {
die('bind failed: ' . htmlspecialchars($sqlvalue->error));
}
$sqlvalue->execute();
if ( false===$sqlvaue ) {
die('execute() failed: ' . htmlspecialchars($sqlvalue->error));
}
$sqlvalue->store_result();
//$ValueResult = $sqlvalue->get_result();
if($sqlvalue->num_rows > 0){
$sqlvalue->bind_result($id, $name, $url, $imgurl);
while($rs = $sqlvalue->fetch()){
?>
<div class="row resultcontent">
<a href="<?php echo $url;?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$name;?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
}
}
$sqltext->bind_result($id, $name, $url, $imgurl);
while($rs = $sqltext->fetch()){
echo "made it into the loop";
?>
<div class="row resultcontent">
<a href="<?php echo $url;?>">
<div class="col-sm-8 searchRsltCont">
<img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" />
<div class="contshading">
</div>
</div>
<div class="col-sm-4 searchRslttxt">
<h1><?php echo "name".$name;?></h1>
</div>
</a>
</div>
<?php
$count++;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.