0

I'm starting to learn php+mysql

after making a query like this:

$sql = "SELECT * FROM clients";
$query = $mysqli->query($sql);

I need to put in a array to access information right?

$result = mysqli_fetch_all($query);

Do I need to use an array or can I search within $query for info?

3
  • What do you want to do exactly? It seems to me that you just need to adapt your query to select the columns you want based on a criteria you want. Commented Sep 10, 2012 at 0:55
  • Right, you usually shouldn't be searching your query results for info, you should be writing a query that gets the results you want. That said retrieving all the data in the table and searching it in PHP is kludgy but harmless... as long as your database stays small. The more data you have, the slower that will get. Commented Sep 10, 2012 at 1:07
  • octern - yes I know what you mean. I'm just trying to understand the result of php queries to mysql DB, hence my question... cheers all Commented Sep 10, 2012 at 22:12

2 Answers 2

1

For all intents and purposes, yes. In most cases, you won't get much useful data out of it. See http://php.net/manual/en/mysqli.query.php for what values it will return.

That said, I typically use mysql_fetch_array() with a parameter to have the results put in an associative array - or just use mysql_fetch_assoc() to skip adding parameters. That function will put it into an array you can print out by looping through it.

For instance:

while($row = mysql_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
}

is part of an example from http://www.w3schools.com/php/php_ajax_database.asp - which is a rather good reference.

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

3 Comments

I should add that I used mysql extensions and not mysqli. In either case, the concepts are identical.
what happens when the query doesn't return any result? shouldn't I always test if query didn't return any results before trying to put results in an array? because if a query doesn't do anything why should I waste time as resources to put it in an array???
but here php.net/manual/en/mysqlinfo.api.choosing.php says: "It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%." that's why I chose to use mysqli....
1

mysqli_fetch_all will return you an associative or numeric array and then you can do whatever you want with that array .

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.