mardi 23 août 2016

how to use if .. else .. while

The script here fetches data from the database. The goal is that the input $subj might possibly not find a string but it is also possible to find it. Both actions are significant!

The idea is that I use if ... else ... while.

But here I'm completely stuck and I really hope someone can help me solving it.

code:

    <?php 

$subj = 'books';
$servername = "localhost";
$username = "your_name";
$password = "your_password";
$dbname = "test_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}

  if ( $stmt = $conn->prepare('SELECT testcol1 FROM test_table WHERE testcol1 = ? LIMIT 1' )) {
  $stmt->bind_param('s', $subj) ; // not sure if this is necessary
  $stmt->execute();

  $stmt->bind_result($subject);

  echo '<br>Does NOT exist <br>';

 } 
 else {


while ($stmt->fetch()) {
     echo $subject;
        echo ' : this subject is found in the database <br>';

} }  


$stmt->close();

mysqli_close($conn);



?>

Aucun commentaire:

Enregistrer un commentaire