1

I've compiled the following PHP and HTML, what I want to do is connect my WAMP database to my webpage, it's a simple task but the output i receive is displayed in the picture below, can somebody show me where i went wrong?

<?php
    //Step 1
    $db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');
?>

<html>
    <head>
    </head>
    <body>
        <h1>
            PHP connect to MySQL
        </h1>
        <?php
            //Step 2
            $query = "SELECT * FROM patients";
            mysqli_query($db, $query) or die('Error querying database.');

            $result = mysqli_query($db, $query);
            $row = mysqli_fetch_array($result);

            while($row = mysqli_fetch_array($result)){
                echo $row['id'] . ' ' . $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
            }
        ?>
    </body>
</html>

PHP and HTML error

7
  • 2
    I think you have to close the bracket in the while($row = mysqli_fetch_array($result)){ line Commented Mar 14, 2017 at 15:28
  • missing a closing curly bracket in the while loop Commented Mar 14, 2017 at 15:30
  • I added the closing curly bracket and it didn't fix it. My output now simply reads " PHP connect to MySQL '; } ?> " Commented Mar 14, 2017 at 15:38
  • add latest code in ur question Commented Mar 14, 2017 at 15:38
  • I added the latest code to the question now, it's still not working Commented Mar 14, 2017 at 15:42

2 Answers 2

1

I have found some errors in that code

$query = "SELECT * FROM patients";

You need to add the semicolon a the end of the query code here, so:

$query = "SELECT * FROM patients;";

Then we have this

$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);

while($row = mysqli_fetch_array($result)){
   echo $row['id'] . ' ' . $row['patient_name']. ' ' . $row['check_in_date'] . ' ' . $row['room_number'] . ' ' . $row['bed_number'] . ' ' . $row['notes'] . '<br />';
 }

This should work

$result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result)){
   $id = $row['id'];
   $patientname = $row['patient_name']; //do this with every variable you have
   echo "$id $patientname";
}

EDIT: Also, change this

$db = mysqli_connect('localhost', 'root', '', 'hospital') or die('Error connecting to MySQL server.');

with this

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "hospital";
$db = mysqli_connect($dbhost,$dbuser,$dbpass) or die ("dbconn");
mysql_select_db($dbname,$db) or die ("msq");
Sign up to request clarification or add additional context in comments.

4 Comments

Adding a semicolon at the end of single query is not mandatory
Hi, I used your code and it doesn't give any errors, however it also does not print the data I want it to
I added something, try that and let me know
@Stever I added the code you told me too but unfortunately is still not working
0

You dont need to assign $row = mysqli_fetch_array($result); as you are assigning it inside the while loop

EDITED ANSWER

Build a sperate array from the rows and then loop over that, this way you will have more flexibility to add to or edit the results before displaying them

//Step 2
$query = "SELECT * FROM patients";
mysqli_query($db, $query) or die('Error querying database.');

$result = mysqli_query($db, $query);

$rows = array();
while($row = mysqli_fetch_array($result)){
    $rows[] = $row;
}

foreach($rows as $r) {
    echo $r['id'] . ' ' . $r['patient_name']. ': ' . $r['check_in_date'] . ' ' . $r['room_number'] . ' ' . $r['bed_number'] . ' ' . $r['notes'] . '<br />';
}

12 Comments

I did that but unfortunately the code still seems to not work
I used the edited answer but unfortunately had the same result
can u delete all your code @JLally and echo "working" using php
@MasivuyeCokile I did that and nothing now appears on the screen
I removed the HTML file frm my folder and now it works. Thank you so much @LukeBradley
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.