0
votes

I am trying to get some data from MySQL and echo it using PHP. Following is the code which I used. Please check the code and tell me what's wrong in it.

<?php

    // Get a connection for the database
    require_once('mysqli_connect.php');

    // Create a query for the database
    $query = "SELECT first_name, last_name, email, street, city, state, zip, phone, birth_date FROM testable";

    // Get a response from the database by sending the connection and the query
    $response = @mysqli_query($dbc, $query);

    // If the query executed properly proceed
    if($response){

    echo '<table align="left"
    cellspacing="5" cellpadding="8">
    <tr><td align="left"><b>First Name</b></td>
    <td align="left"><b>Last Name</b></td>
    <td align="left"><b>Email</b></td>
    <td align="left"><b>Street</b></td>
    <td align="left"><b>City</b></td>
    <td align="left"><b>State</b></td>
    <td align="left"><b>Zip</b></td>
    <td align="left"><b>Phone</b></td>
    <td align="left"><b>Birth Day</b></td></tr>';

    // mysqli_fetch_array will return a row of data from the query until no further data is available
    while($row = mysqli_fetch_array($response)){

    echo '<tr><td align="left">' .
    $row['first_name'] . '</td><td align="left">' .
    $row['last_name'] . '</td><td align="left">' .
    $row['email'] . '</td><td align="left">' .
    $row['street'] . '</td><td align="left">' .
    $row['city'] . '</td><td align="left">' .
    $row['state'] . '</td><td align="left">' .
    $row['zip'] . '</td><td align="left">' .
    $row['phone'] . '</td><td align="left">' .
    $row['birth_date'] . '</td><td align="left">';
        echo '</tr>';
  }
    echo '</table>';
    } else {  
    echo "Couldn't issue database query<br />";
    echo mysqli_error($dbc);    
    }

    // Close connection to the database
    mysqli_close($dbc);
?>

The output I am getting for this code is :

First Name Last Name Email Street City State Zip Phone Birth Day'; // mysqli_fetch_array will return a row of data from the query // until no further data is available while($row = mysqli_fetch_array($response)){ echo '' . $row['first_name'] . '' . $row['last_name'] . '' . $row['email'] . '' . $row['street'] . '' . $row['city'] . '' . $row['state'] . '' . $row['zip'] . '' . $row['phone'] . '' . $row['birth_date'] . ''; echo ''; } echo ''; } else { echo "Couldn't issue database query "; echo mysqli_error($dbc); } // Close connection to the database mysqli_close($dbc); ?>

1
Why there is an @ before mysqli_queryjophab
the @ is a error suppression characterElzo Valugi
I am learning PHP and really don't know about this @. I written this code following some tutorial. I manage to insert data following this tutorial but can't get it from database . Should I remove this ?Malik
I removed @ but its still showing same output.Malik
The output suggests that the PHP part inside the while is not executed and just outputed as text. This is a little strange as I am not seeing any quotes that will make this code break. I will do some tests with your code and come backElzo Valugi

1 Answers

1
votes

I found my mistake. I was enter incorrect URL in browser.

Incorrect URL : file:///C:/xampp/htdocs/php/view.php

Correct URL : http://localhost/php/view.php

The code is working perfectly fine and showing me data from MySQL database now.