Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to display items from a database, however the while loop keeps looping around.

Is there anyway i can stop it looping around each one?

See code below.

What I have tried:

<?php

$1 = ("cheese");
$2 = ("yoghurt");
$3 = ("egg");

   $dbQuery=$conn->prepare("select * from Results");
   $dbQuery=$conn->prepare("select * from Food where Keyword = '$1'");
   $dbQuery->execute();
   echo $dbQuery->rowCount()."\n";
   while ($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) {
if ($1 = '1') {
  echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n";
} else {
    echo "";
}
 $dbQuery=$conn->prepare("select * from Food where Keyword = '$2'");
   $dbQuery->execute();
if ($2 = '1') {
  echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n";
} else {
    echo "";
}
 $dbQuery=$conn->prepare("select * from Food where Keyword = '$3'");
   $dbQuery->execute();
if ($3	 = '1') {
  echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n ";
} else {
    echo "";
}
   }
?>
Posted
Updated 22-Mar-18 6:34am
Comments
Richard MacCutchan 22-Mar-18 12:03pm    
You need to check what result the fetch operation is returning.

1 solution

PHP
$dbQuery=$conn->prepare("select * from Food where Keyword = '$1'");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
-----
Advice: Learn to indent properly your code. It help reading by showing the structure.
PHP
<?php

$1 = ("cheese");
$2 = ("yoghurt");
$3 = ("egg");

$dbQuery=$conn->prepare("select * from Results");
$dbQuery=$conn->prepare("select * from Food where Keyword = '$1'");
$dbQuery->execute();
echo $dbQuery->rowCount()."\n";
while ($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) {
	if ($1 = '1') {
		echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n";
	} else {
		echo "";
	}
	$dbQuery=$conn->prepare("select * from Food where Keyword = '$2'");
	$dbQuery->execute();
	if ($2 = '1') {
		echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n";
	} else {
		echo "";
	}
	$dbQuery=$conn->prepare("select * from Food where Keyword = '$3'");
	$dbQuery->execute();
	if ($3	 = '1') {
		echo "<br><br>".$dbRow["Name"]."<br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' float='left'/>"."".$dbRow["Instructions"]."\n ";
	} else {
		echo "";
	}
}
?>

Professional editors have this feature along with others, like parenthesis matching, that are helpful to programmers.
Notepad++ Home[^]
-----
Nota: your code is really weird, you should explain what it is supposed to do.
 
Share this answer
 
Comments
Member 13637584 22-Mar-18 12:50pm    
I am trying to display food which corresponds with user preferences.
Patrice T 22-Mar-18 15:35pm    
Why do you have a loop in first place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900