How to use a @while loop@ mine is not working?

[php]<?php
require ‘database.php’;

//if (!empty($_POST[‘search’])):

$sql1 = “SELECT * FROM cookies2 WHERE cookie LIKE '%”.$_POST[‘search’]."%’";

$request1 = $conn->prepare($sql1);
$request1->bindparam(’:search’, $_POST[‘search’]);

$request1->execute();
$result1 = $request1->fetch(PDO::FETCH_ASSOC);
var_dump($request1);
$ff = $result1[‘cookie’];
//endif;
?>

<?php
if(!empty($request1)):
while($search_result = $request1->fetch(PDO::FETCH_ASSOC)){ 
var_dump($search_result);?>

<h1>baaaaaa</h1>
<p><?= $search_result['cookie']?></p>
<h3><?= $search_result['comment']?></h3><?php }

endif;
?>

[/php]

Hello Im trying w simple script to loop through my cookie database
but search_result" is returning false or empty half the time
and i tried different things indent code etz
and different if statements
please help see if i missed something>?

Your prepared statement query has the wrong syntax.
You define a placeholder, then don’t use it.

You should not be using PDO->fetch
Use fetchAll, with the columns you actually want, not everything

This isn’t Python, your code indention is meaningless.

First you are use wrong syntax for php by use if {} and while have counter you dont initialized that counter ++

[member=88290]Pardeep[/member], there is nothing wrong with the syntax for the if statement. I prefer that syntax when it surrounds large blocks actually.

You also don’t need a counter in the while. It is only returning a single result regardless.

Sponsor our Newsletter | Privacy Policy | Terms of Service