0

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); ?>

8
  • Why there is an @ before mysqli_query Commented Aug 9, 2016 at 9:22
  • the @ is a error suppression character Commented Aug 9, 2016 at 9:23
  • 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 ? Commented Aug 9, 2016 at 9:25
  • I removed @ but its still showing same output. Commented Aug 9, 2016 at 9:26
  • 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 back Commented Aug 9, 2016 at 9:34

1 Answer 1

1

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.

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

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.