0

I'm trying to use php to run a sql select statement to pull data from a database and then only output one column(username) to an array that I can then use to filter using a JS script. Similar to how facebook/twitter does their real time searching of contacts when you start typing someone's name in. I keep getting an error though in php when I try to pull the username column and set it to an array and output that:

<?php
session_start();
include_once 'dbconnect.php';
$contact = mysql_real_escape_string($con, $_POST['contact']);
$sql = "select * from users";
$result = $con->query($sql);
if ($result->num_rows > 0) {
   ($row = $result->fetch_assoc()) {
      printf($row["username"]);
    }
} else {
    echo "0 results";
}
?>
2
  • @bitfiddler nothing get's returned, it's completely blank...if i comment out the ($row ...){...} section and just put in a simple echo'result'; it get's printed but other wise nothing gets returned... Commented Apr 15, 2016 at 0:47
  • apparently i was missing while before the ($row)...thanks! Commented Apr 15, 2016 at 0:51

1 Answer 1

2

It would help to know what the exact error PHP is giving you is. But from a cursory look on your code I believe you are missing a while keyword.

   while ($row = $result->fetch_assoc()) {
      printf($row["username"]);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@GregBorbonus, this isn't my question though so it's up to DRE .

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.