1

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.

4
  • Any mysql errors ?try var_dump() on the row variable, also don't use mysql use mysqli instead. Commented Jul 28, 2017 at 4:59
  • 2
    remove the single quotes from ORDER BY 'id, displayorder' so that its ORDER BY id, displayorder Commented Jul 28, 2017 at 5:01
  • Nothing with the var_dump() it is not even outputting, <th>Name</th> and etc, almost like it is not going into the while statement Commented Jul 28, 2017 at 5:03
  • If you got it working make sure to accept the answer that helped you the most :) Commented Jul 28, 2017 at 5:28

4 Answers 4

1

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>';
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

2 Comments

When I echo $sql; It just prints out SELECT ID, topic_id, name, surveyid, questionid, longdesc, text, first_name , last_name , email FROM vw_polling_results WHERE 'topic_id' = '147796' ORDER BY id, displayorder
you try to change the query to topic_id='147796' and id, displayorder.. remove the quotes and try..
0

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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.