1

I wrote a website using Google App Engine intended for the 'Datastore' database but now the website is already complete and because of Google's resources limit the website goes down too frequently (and I don't have any money to spend on it, even if I did I wouldn't :/)

Question:

When ever I fetch a table, I want to simply return it like this:

  • Each row is in it's own array to make it easy to iterate through
  • Instead of the standard number, each key would be the name of column is from

Problem:

No matter if I use fetch_all, fetch_assoc, or fetch_objectI never get anything that looks like what I am trying to get.

I am pretty bad with iterations and for loops so I couldn't figure how to do it manually.

2
  • 2
    mysqli_fetch_assoc() performs exactly that, however using a mysqli resource, only 1 row is returned at a time, which is why you see a lot of result loops. An easy line to do this is: $result = array(); while($result[] = $mysqli_con->mysqli_fetch_assoc()); Commented Nov 23, 2015 at 16:52
  • Thanks man, this does exactly what I want. Didn't realize it was that easy :P Commented Nov 23, 2015 at 17:14

1 Answer 1

2

The simplest way to build an associative array of database rows with mysqli_* is to do this:

$mysqli_query = mysqli_query($db_link, "SELECT * FROM tablename");
$result = array();
while($result[] = $mysqli_query->mysqli_fetch_assoc());

and $result will hold the array you want

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

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.