Echo issue with while / if / else

I am starting to learn php and mysql and got into Chapter 5 of Head First PHP and MySQL, and I am trying to create a self-sufficient project in which I created a database and an index.php page where I can see the results printed out. When I go to the index.php page, I see the HTML header, but the php code does not print my messages. I must assume that my code syntax is correct, or I get a blank page. Can someone please tell me what I encoded wrong to end without output? Thank.

<?php

//Establish connection with database

$dbc = mysqli_connect(localhost,root,root,itmyfamily);

//Order the data to be retrieved

$query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name `enter code here`DESC, date ASC";

//Execute the connect command and the query 
$data = mysqli_query($dbc,$query);
//Loop through the array of family submissions, formatting it as html
$i = 0;

while ($row = mysqli_fetch_array($data)) {

//Display family submissions

    if ($i == 0) {

        echo '<strong>First Name:</strong> ' .$row['first_name']. ' <br/>';
        echo '<strong>Last Name:</strong> ' .$row['last_name']. ' <br />';
        echo '<strong>Spouse Name:</strong> ' .$row['spouse_name']. ' <br/>';
        echo '<strong>Email:</strong> ' .$row['email']. ' <br />';

        }
        else {
            echo 'There is no info in the database';        
        }

         $i++;
    }


    mysqli_close($dbc);

   ?>
+4
source share
1 answer

Install this on top of the source code to display errors.

<?php error_reporting( E_ALL ); ?>

Or set display_erros in php.ini as follows:

display_errors = On

error_reporting = E_ALL | E_STRICT

,

<?php

  //Establish connection with database

  $dbc = mysqli_connect(localhost,root,root,itmyfamily);

  //Order the data to be retrieved

  $query = "SELECT * FROM itsmyfamily ORDER BY last_name ASC, first_name DESC, date ASC";

  //Execute the connect command and the query 

$data = mysqli_query($dbc,$query);


  while ($row = mysqli_fetch_array($data)) {


//Loop through the array of family submissions, formatting it as html

  $i = 0;

  //Display family submissions

    if ($i == 0) {

    echo '<strong>First Name:</strong> ' .$row['first_name']. ' <br />';
    echo '<strong>Last Name:</strong> ' .$row['last_name']. ' <br />';
    echo '<strong>Spouse Name:</strong> ' .$row['spouse_name']. ' <br />';
    echo '<strong>Email:</strong> ' .$row['email']. ' <br />';

}
    else{
        echo 'There is no info in the database';       
  }
  $i++;


  }


    mysqli_close($dbc);

  ?>

If you can write it this way, i think you dont even need the $i.

$result = mysql_query("SELECT id, first_name FROM mytable");

if($result){

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["first_name"]);
}

}
else
 echo 'There is no info in the database'; 

php mysql_fetch_array http://www.php.net/mysql_fetch_array

. http://webcheatsheet.com/php/loops.php

, , , php 5.5.0. mysqli PDO. PHP.

http://www.php.net/manual/en/mysqli.query.php

http://www.php.net/manual/en/pdo.query.php

+4

Source: https://habr.com/ru/post/1538342/


All Articles