1

I'm pretty new to php, and I'm teaching myself. I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table.

 <?php

$connect = mysqli_connect("localhost","*","*","*");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$comments = "SELECT * FROM commentstable";

$rs = mysqli_query($connect,$comments);

$fetch = mysqli_fetch_array($rs);

while($fetch = mysqli_fetch_array($rs)) {
    echo $fetch['comments'];

    }
echo $fetch;

mysqli_close($connect);

echo "hello";

?>
2
  • 1
    Look at using mysqli_error() Commented Nov 6, 2013 at 21:42
  • change $rs = mysqli_query($connect,$comments); to $rs = mysqli_query($connect,$comments) or die(mysqli_error($connect)); Commented Nov 6, 2013 at 21:43

2 Answers 2

1

you have double entry:

$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop

while($fetch = mysqli_fetch_array($rs)) {    
    echo $fetch['comments'];

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

Comments

0

Check this

$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    $comments = "SELECT * FROM commentstable";

    $rs = mysqli_query($connect,$comments);
    if($rs)
    {
        while($fetch = mysqli_fetch_array($rs)) {
            echo $fetch['comments'];
        }
    }
    else
    {
        // no results from query
    }

    mysqli_close($connect);

}

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.