0
<html>
<head>
    <meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
    <title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
   require_once("settings.php");
   $dbconnect = @mysqli_connect($host, $user, $pswd, $dbnm);
    if($dbconnect->connect_errno >0)
    {
        die('Unable to connecto to database [' . $db->connect_error . ']');
    }

    $queryResult = "SELECT car_id, make, model, price FROM cars";

    echo "<table width='100%' border='1'>";
    echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";

    //initiate array 
    $displayrow= mysqli_fetch_array($queryResult);

    //initiate while loop to iterate through table
    while($displayrow)
    {
        echo "<tr>";
        echo "<td>" . $row['ID'] . "</td>";
        echo "<td>" . $row['Make'] . "</td>";
        echo "<td>" . $row['Model'] . "</td>";
        echo "<td>" . $row['Price'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($dbconnect);
?>
</body>
</html>

This is doing my head in, I cannot figure out why it will not display the actual data apart from the Table header. No matter what I used. I have tried mysqli_fetch_array, mysqli_fetch_row, mysqli_fetch_assoc but nothing works.

Help and explanation why it was not displaying the data would be much appreciated :)

1
  • 1
    You can access data with coresponding column name. Change $row['ID'] to $row['car_id']. and others. Commented Apr 9, 2014 at 15:59

5 Answers 5

3

First: You aren't running a query, you are only putting the query text in a variable. You need to use mysqli_query.

Second: You should add mysqli_fetch_array to the loop.

For example:

while($displayrow = mysqli_fetch_array($queryResult))
{

}

Otherwise you are only getting the first row.

Third: Array keys are case sensitive. There is no $row['ID'], as Jeribo pointed out, it is $row['car_id'] as referenced in your query. $row['Make'] is not the same as $row['make'].

Sign up to request clarification or add additional context in comments.

Comments

0

Please Precision to names of field in Query ( car_id,make,...)

while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
    echo "<td>" . $displayrow['car_id'] . "</td>";
    echo "<td>" . $displayrow['make'] . "</td>";
    echo "<td>" . $displayrow['model'] . "</td>";
    echo "<td>" . $displayrow['price'] . "</td>";
    echo "</tr>";
}

Comments

0

If you want to query outside you still have to set it in the loop:

$result = $db->query($queryResult)

while($row = $result ->fetch_assoc()){
...
}

a Good Tutorial is shown here: http://codular.com/php-mysqli

Comments

0

$row needs to be initialized so why don't you try:

while($row = mysqli_fetch_array($queryResult))
{
    ....
}

Comments

0

You have to get the result set first and then try fetching array from result set

<?php
   require_once("settings.php");
   $dbconnect = @mysqli_connect($host, $user, $pswd, $dbnm);
    if($dbconnect->connect_errno >0)
    {
        die('Unable to connecto to database [' . $db->connect_error . ']');
    }

    $query = "SELECT car_id, make, model, price FROM cars";
    $resultSet=mysqli_query($dbconnect,$query)
    echo "<table width='100%' border='1'>";
    echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";

    //initiate array 
    $displayrow= mysqli_fetch_array( $resultSet);

    //initiate while loop to iterate through table
    while($displayrow)
    {
        echo "<tr>";
        echo "<td>" . $row['ID'] . "</td>";
        echo "<td>" . $row['Make'] . "</td>";
        echo "<td>" . $row['Model'] . "</td>";
        echo "<td>" . $row['Price'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($dbconnect);
?>

http://www.w3schools.com/php/func_mysqli_fetch_array.asp

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.