The Hidden Person
The Hidden Person

Reputation: 57

Trying to display SQL data in html table

So I have a file that is supposed to run a sql query and return the data and then populate a html table but for some reason it is not returning the data, in the sql in database the query does return data but not on my website.

  <?php  
            //run the query
            $sql = "SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email
                    FROM polling_results WHERE 'topic_id' = '147796' 
                    ORDER BY 'id, displayorder'";
            $result = mysql_query($sql);
            //fetch the results
            while ($row = mysql_fetch_array($result))
            {
                //display the results
                echo '<br /><table class="table table-bordered table-condensed">';
                echo '<thead><tr>';
                echo '<th>Name</th>';
                echo '<th>Email</th>';
                echo '<th>Question Text</th>';
                echo '<th>Answer</th>';
                echo '</tr></thead>';
                echo '<tbody><tr>';
                echo "<td>".$row['first_name']."</td>";
                echo "<td>".$row['email']."</td>";
                echo "<td>".$row['longdesc']."</td>";
                echo "<td>".$row['text']."</td>";
                echo '</tr></tbody></table>';
            }
    ?>

Got it working, thank you for all the help guys/gals.

Upvotes: 1

Views: 1228

Answers (4)

Prabhu Nandan Kumar
Prabhu Nandan Kumar

Reputation: 1255

You need to change code like below : Because of mysql_* is deprecated from php 5.6 and onwards so you should use mysqli_* .

Create DB Connection as follows :

    <?php
    // Create connection
    $db_conn = mysqli_connect ( $servername, $username, $password, $dbname );
    // Check connection
    if (! $db_conn)
    {
        die ( "Connection failed: " . mysqli_connect_error () );
    }
?>

And now your code :

<?php  
            //run the query
            $sql = "SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email
                    FROM polling_results WHERE 'topic_id' = '147796' 
                    ORDER BY 'id, displayorder'";
            $result = mysqli_query($db_conn,$sql);

            // Creating table format

             echo '<br /><table class="table table-bordered table-condensed">';
                echo '<thead><tr>';
                echo '<th>Name</th>';
                echo '<th>Email</th>';
                echo '<th>Question Text</th>';
                echo '<th>Answer</th>';
                echo '</tr></thead>';
                echo '<tbody>';


            //fetch the results
            while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                //display the results
               <tr>';
                echo "<td>".$row['first_name']."</td>";
                echo "<td>".$row['email']."</td>";
                echo "<td>".$row['longdesc']."</td>";
                echo "<td>".$row['text']."</td>";
                echo '</tr>';
            }
             echo '</tbody></table>';
            // display data end.
    ?>

If any issue you face please let me know.

Upvotes: 0

IcedAnt
IcedAnt

Reputation: 454

Are you opening a connection to the DB? I suggest using mysqli instead of mysql.

// Create connection
$conn = mysqli_connect ( $servername, $username, $password, $dbname );
// Check connection
if (! $conn)
{
    die ( "Connection failed: " . mysqli_connect_error () );
}

Also, you should move the table creation outside of your while, as this way it will create a new table for every line.

echo '<br /><table class="table table-bordered table-condensed">';
echo '<thead><tr>';
echo '<th>Name</th>';
echo '<th>Email</th>';
echo '<th>Question Text</th>';
echo '<th>Answer</th>';
echo '</tr></thead>';
echo '<tbody>';

//display the results
while ($row = mysqli_fetch_array($result))
{
    echo '<tr>';
    echo "<td>".$row['first_name']."</td>";
    echo "<td>".$row['email']."</td>"
    echo "<td>".$row['longdesc']."</td>";
    echo "<td>".$row['text']."</td>";
    echo '</tr>';
}

echo '</tbody></table>';

Upvotes: 1

Pankaj Makwana
Pankaj Makwana

Reputation: 3050

Remove quotes from the query 'topic_id' and ORDER BY 'id, displayorder'

Your Query :

$sql = "SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email
                    FROM polling_results WHERE 'topic_id' = '147796' 
                    ORDER BY 'id, displayorder'"

Edited Query :

$sql = "SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email
                    FROM polling_results WHERE topic_id = '147796' 
                ORDER BY id, displayorder"

Upvotes: 0

Jok3r
Jok3r

Reputation: 496

you try to echo the $sql and try data is there or not.. if it works try echo the $result..

Upvotes: 0

Related Questions