View Single Post
Old 08-29-2021, 11:46 AM  
k33n
Confirmed User
 
Join Date: Feb 2009
Posts: 201
Sarettah's approach is better but for learning purposes, check the red notes. Tested.

Code:
<?php
    // check incoming params exist

    if ( ! isset($_GET['id'] ) ) { //1.you were missing a )
        // missing param, go to an error page for example
        header('Location: index.php');
        exit;
    }

    // You can now use $_GET['id'] which will be the id number passed
    // any way you want.

    // For example using a PDO connection called $pdo
   // 2. Do the PDO connection

   $pdo = new PDO("mysql:host=localhost;dbname=yourdbname", "dbusername","dbpass");
    $table = "Recipes";
   //3. Your query was looking for a column called id, but your column is called RecipeID
   //use this instead
      $sql = "SELECT * FROM $table WHERE RecipeID = :id";
    try {
        $stmt = $pdo->prepare($sql);

        $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
        $stmt->execute();

        $rows = $stmt->FetchAll();  // $rows now contains all the results of the query
    }
    catch( PDOException $e) {
        echo $e-getMessage();
    }
    //Added the table as i've seen a </table> below
    echo "<table border='1'>
			<tr>
			<th>ID</th>
			<th>Title</th>
			<th>Method</th>
			<th>Ingredients</th>
			</tr>";
    foreach ( $rows as $row ) {
        // do things with the $row['column_name'] data
       echo "<tr>";
       echo "<td>" . $row['Title'] . "</td>";
       echo "<td>" . $row['Ingredients'] . "</td>";
       echo "<td>" . $row['Method'] . "</td>";
       echo "</tr>";
    }
  // 4. You are using PDO, mysqli_fetch_array($result) will throw error:  mysqli_fetch_array() expects parameter 1 to be mysqli_result.And $result is undefined.   

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['Title'] . "</td>";
    echo "<td>" . $row['Ingredients'] . "</td>";
    echo "<td>" . $row['Method'] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>
k33n is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote