简体   繁体   中英

Output not displaying when attempting to connect to database

I recently set up a lamp server in an attempt to create a website. My code is as follows.

 <!DOCTYPE html> <html> <head> </head> <body> <h1>PHP connect to MySQL</h1> <?php $query = "SELECT * FROM nes"; $db = mysqli_connect('localhost','root','password','gameRankings'); $result = mysqli_query($db, $query); $row = mysqli_fetch_array($result); while ($row < mysqli_fetch_array($result)) { echo $row['ranking'] . ' ' . $row['name'] . ': ' . $row['review'] . ' ' . $row['releaseYear'] .'<br>'; } mysqli_close($db); ?> <h1>PHP connect to MySQL</h1> </body> </html> 

The program runs fine and outputs the data I want when I run it using php index.php however when I go to load the site into a browser nothing displays except the heading. If I were to move my the $db connector line to the top of the code then not even the heading displays. There are no errors being thrown in the apache logs either. I have no idea what I'm doing wrong here!

Replace

$row = mysqli_fetch_array($result);

while ($row < mysqli_fetch_array($result)) {

With

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

That will update $row each loop, with the value from database, and display the info you want.

Do these two steps and you will get your result .

  1. Remove this statement :

     $row = mysqli_fetch_array($result); 
  2. Re-write the while statement as :

     while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){ //Your Code } 
  3. If you still got no result , then check your database tables whether they have any records or not .

I wish I had a good answer as to what fixed this but I came in this morning to it functioning properly. I also made the other adjustments that were mentioned. This was my first post and I really appreciate the feedback everyone gave!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM