Carlos Gouveia
Carlos Gouveia

Reputation: 11

How to handle mysql queries in mysql

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?

Upvotes: 0

Views: 282

Answers (2)

MD. Sahib Bin Mahboob
MD. Sahib Bin Mahboob

Reputation: 20524

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

Upvotes: 1

Mlagma
Mlagma

Reputation: 1260

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.

Upvotes: 1

Related Questions