0

I am trying to query data from a table using the following script:

//connect file
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//connect file included above - ECHO tested and it is connected as $conn

$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
    printf("Errormessage: %s\n", $conn->error);
    exit;
} else {
    echo $row['username'];
}

UPDATE --

It now no longer tries to throw an error and seems to go to the else section; however, no echo - and the spelling is correct this time and the column is filled.

jpgerb
  • 1,043
  • 1
  • 9
  • 36
  • 6
    Find out why your query is failing http://php.net/manual/en/mysqli.error.php instead of a custom error text-based message. You also don't have an `else` so do `else { echo "there's info in here, now I just need to loop over the results."; }` – Funk Forty Niner Jun 27 '15 at 15:35
  • 6
    Is the example right? Because you have: `$results = $conn->query($sql);` but then in the 'if' statement, you are checking `!$result` - not `$!results`. `$result` variable is not assigned/initialized. – jjczopek Jun 27 '15 at 15:36
  • @Fred-ii- No messsage comes up when I change that line to `printf("Errormessage: %s\n", $mysqli->error);` it shows "Errormessage: " – jpgerb Jun 27 '15 at 15:37
  • 4
    that's because it should read as `printf("Errormessage: %s\n", $conn->error);` ;-) – Funk Forty Niner Jun 27 '15 at 15:38
  • 2
    then tell us what error you get ^ – Funk Forty Niner Jun 27 '15 at 15:39
  • @Fred-ii- Updating the spelling now treats it as if it is functioning; however, when I try and echo $row['username'] it does not echo anything - or any errors – jpgerb Jun 27 '15 at 15:39
  • 3
    @jjczopek good catch!! error reporting would have spotted that unassigned variable/typo. To OP Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jun 27 '15 at 15:39
  • 3
    can you tell me... for the life of me, why this question was closed with that link? and what it has to do with `bind_param() error`?! Edit: I've reopened the question and it also had 2 reopen votes. @yourcommonsense, don't do that. Have some "common sense" will ya? – Funk Forty Niner Jun 27 '15 at 15:44
  • Do you really have a column named `username` in `userInfo` table? – Rahul Jun 27 '15 at 15:44
  • 3
    `echo $row['username'];` you need a `while` loop for that. – Funk Forty Niner Jun 27 '15 at 15:44
  • @Rahul yes I do. 'username' 'password' 'email' 'dob' are my columns – jpgerb Jun 27 '15 at 15:44

1 Answers1

5
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["username"];
    }
} else {
    echo "0 results";
}

This now returns results. Thank you @Fred -ii- especially for your help on this.

Also thanks @jjczopek for the error checking advice!

jpgerb
  • 1,043
  • 1
  • 9
  • 36
  • 1
    Thank for giving credit to me but I think much help have been offered by @Fred -ii- and the other guy you have already named in answer. Cheers! – Rahul Jun 27 '15 at 15:51
  • Thanks for catching that - thanks for your check on the column names too – jpgerb Jun 27 '15 at 15:52