0

I'm attempting to set up an API for an app project.

I've got a mysql table called 'users', which I've added a row to.

using the code:

// Create connection
$mysqli = new mysqli("localhost","user", "pass", "db");

// Check connection
if($mysqli->connect_errno){

    $result = "Failed to connect to MySQL: " . mysqli_connect_error();
    print_r( json_encode($result) );    
    return false;

}

$row = $mysqli->query("SELECT * FROM users");
print_r( json_encode($row) );

I get an empty result, how come? (connection doesn't throw an error)

to be exact i get:

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

EDIT:

got the answer to ym original question, thanks!

so now using the code:

$row = $mysqli->query("SELECT * FROM users WHERE email = '".$email."'");
$result = $row->fetch_array();
print_r( json_encode($result) );

I get the result:

{"0":"test","username":"test","1":"[email protected]","email":"[email protected]","2":"test","password":"test","3":"2013-10-18 22:22:53","date_registered":"2013-10-18 22:22:53","4":"1","id":"1"}

where what i want is something like:

{"username":"test","password":"test","email":"[email protected]", ...etc }

how do i get that?

4
  • If you've used mysql_* previously.. Could you obtain results from running mysql_query()? I don't think so. fetch an array from the query Commented Oct 18, 2013 at 21:50
  • 2
    Don't you need to fetch the results from $row? Commented Oct 18, 2013 at 21:54
  • Edited my answer. You need to use mysqli_fetch_assoc() to avoid duplicating the results with their numeric indexes. Commented Oct 18, 2013 at 22:02
  • try my answer for associative...? Commented Oct 18, 2013 at 22:10

2 Answers 2

2

Try this:

$result = $mysqli->query("SELECT * FROM users");
$row = $result->fetch_array(MYSQLI_ASSOC);
print json_encode($row);   // json_encode returns a string...

Try this for your associative array:

while($row = $result->fetch_array(MYSQLI_ASSOC))
{
   $rows[] = $row;
}
print json_encode($rows);

or you can try... $rows = $result->fetch_all(MYSQLI_ASSOC);

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

Comments

1

mysqli_query() will return a mysqli_result, you need to fetch (as an array in this case) your rows before doing anything with them. Added a line:

// Create connection
$mysqli = new mysqli("localhost","user", "pass", "db");

// Check connection
if($mysqli->connect_errno){

    $result = "Failed to connect to MySQL: " . mysqli_connect_error();
    print_r( json_encode($result) );    
    return false;

}

// Get a mysql_result
$row = $mysqli->query("SELECT * FROM users");

// Get it into an array without numeric indexes
$result = $row->fetch_assoc();

// Display the row
print_r( json_encode($result) );

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.