This code correctly displays the data from the remote server:

<?php $host="localhost"; $user="***"; $password="***"; $db_name="***"; $conn=mysqli_connect($host,$user,$password,$db_name); $sql="select * from goods"; $result=mysqli_query($conn,$sql); while($row = mysqli_fetch_array($result)) { echo $row["goods_id"]." ".$row["goods_name"]; echo "<br>"; } mysqli_close($conn); // header(string "Location:index.php"); ?> 

and this code displays an empty web page:

 <?php $host="localhost"; $user="***"; $password="***"; $db_name="***"; $conn=mysqli_connect($host,$user,$password,$db_name); $sql="select * from goods"; $result=mysqli_query($conn,$sql); echo <table border='1'> <tr> <th>Goods_id</th> <th>Goods_name</th> </tr>; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$row["goods_id"]."</td>"; echo "<td>".$row["goods_name"]."</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($conn); // header(string "Location:index.php"); ?> 

I can not find the reason. Please help.

  • 2
    The first echo without quotes you need to enclose in quotes "<table border = '1' ...."; - Pavel
  • 2
    Also, the echo construction supports the and so on operator, and so on : echo 'str1', 'str2', 'str3'; - passing arguments separated by commas. Works faster than string concatenation. - And
  • @And there is a proof on this topic? Maybe in PHP there is an optimization in runtime for this account and they take the same number of cycles? - korytoff

1 answer 1

Due to the lack of quotes at the line after echo, you have an error that is not displayed on the screen. You need to at least fix the error:

 <?php error_reporting(E_ALL); // настраиваем уровень ошибок ini_set('display_errors', true); // включаем вывод ошибок на экран $host = "localhost"; $user = "***"; $password = "***"; $db_name = "***"; $conn = mysqli_connect($host, $user, $password, $db_name); $sql = "select * from goods"; $result = mysqli_query($conn, $sql); echo "<table border='1'> <tr> <th>Goods_id</th> <th>Goods_name</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row["goods_id"] . "</td>"; echo "<td>" . $row["goods_name"] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($conn); 

I also added two lines at the beginning of the code to regulate the level of errors and display them on the screen, it is desirable to change the config for this, but you can do it when you execute the code, though you will have to do it in all the files that are launched.

PS I also recommend immediately to teach yourself to work with the base to use PDO

PPS I noticed a line at the end of the code with a header header(string "Location:index.php"); . Always send headers before displaying any information on the screen , for example via echo .