2

Following is a typical OOP way to Connect and Retrive data from MySQL database using MySQLi and PHP. Now I am a little bit confused on using the pure PHP arrays at this example:

Can some one please let me know if the $result is array or not? I mean in

$result = $mysqli_coonect->query($sql);

if so how come we didn't use the array() function or [] to create it?!

same thing happening with $row at:

$row = $result->fetch_assoc()

I am asking this because in order to load $row; to $results[] at $results[] = $row; we declared the $results as an array explicitly before the while() how come we are not doing this for other or vice versa?!

<?php
$mysqli_coonect = new mysqli($host_name, $user_name, $pass_word, $database_name, $port);
$sql = "SELECT * FROM books WHERE id <= 10";    
$result = $mysqli_coonect->query($sql);
if (($result) && ($result->num_rows > 0))
{
    $results = array();
    while ($row = $result->fetch_assoc())
    {
        $results[] = $row;
    }
  $result->free();
}
$mysqli_coonect->close();
3
  • array() isn't technically a function -- it's one of those syntax oddities, like many other 'language constructs' which are specifically not-functions, but look like them (e.g. list()) Commented Mar 12, 2015 at 17:11
  • Hi Josh can you please take a look at this link w3schools.com/php/func_array.asp Commented Mar 12, 2015 at 17:15
  • that's just one of many reasons not to treat w3schools like an authority. Refer to php.net first for any PHP definitions you need, and MDN for javascript. That array() looks like, and is occasionally referred to as a function is more a historical quirk than anything else. If you try and use it like it's a function, you're going to have a bad time. php.net/manual/en/… Commented Mar 12, 2015 at 17:22

1 Answer 1

3

Always refer to the PHP documentation. It's great and you can easily find what you want.

http://php.net/manual/en/mysqli.query.php states that a query will return mixed in this case meaning either false if the query failed or mysqli_result object of the query was successful.


Getting down to business

  1. $result = $mysqli->query($query) returns a mysqli_result object. This means that $result is now an object.
  2. We then call the method fetch_assoc from our newly created $result (mysqli_result) object and store the results of the method in the variable $row (which if the method is successful will be an array). If you look at what fetch_assoc returns (http://php.net/manual/en/mysqli-result.fetch-assoc.php) you see that it returns an array.
  3. We loop through our newly created $row array.
Sign up to request clarification or add additional context in comments.

9 Comments

I started typing out a longer answer, but I agree in this case the best advice is to rtfm.
Thanks dojs but I dint get anything from the link and your comment!
Okay, well try to follow the steps now and see if it makes any sense.
so in step 2 is the $row still object or array?
$row becomes an array because the method fetch_assoc returns an array
|

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.