# Part 6.2Using MySQL queries in PHP

Queries that have been generated would be useless if they could not be output or worked on with PHP.

## Sample queries

Below are some sample queries that will be referenced in this article ($query1 and $query2):

PHP
<?php
$myConnection = mysqli_connect($dbName, $dbUsername,$dbPassword);

$query1 = "SELECT * FROM table1 WHERE id > 4";$query2 = "SELECT * FROM table1 WHERE forename = 'John'";

$result1 = mysqli_query($myConnection, $query1); if(!$result1)
echo "Failure!";

$result2 = mysqli_query($myConnection, $query2); if(!$result2)
echo "Failure!";

mysqli_close($myConnection); ?>  Assume that the following table ("table1") has been used: id forename surname age 0 James Adams 44 1 Frank Adams 45 2 John Roosevelt 45 3 Mark French 46 4 Frank Skeldon 43 5 Justin McGregor 51 6 Helga Adams 45 7 John Smith 46 ## Working with the result The important note is that the information that has been retrieved from the server is now in the $result1 and $result2 variables. This means that any kind of references to the database data will only refer to the variables. If the database changes after the query, these changes will not be reflected in the variables. ## Count the number of results MySQL can count the number of rows using the COUNT aggregate function. This may be useful if all that is required is the count of the results but in cases where the information is required multiple times this would be slower than using PHP to count them. PHP provides the mysqli_num_rows function to get the number of rows found in a query: PHP <?php //Should output 3 echo mysqli_num_rows($result1);
//Should output 2
}
?>


## Getting the results from the query

Of course, none of these functions have worked on the actual result, giving out the values returned. This is achieved, mostly, using the mysqli_fetch_array function.

This function transforms the result into a PHP array. Each row itself is an associative array. This means that the result can be iterated with a foreach loop:

PHP
<?php
$rows = mysqli_fetch_array($result1);

foreach($rows as$row) {
//Will output the id column
echo $row; //Will output the forename column echo$row;
}
?>


Alternatively, the column name can be used instead of using the index of the column:

PHP
<?php
$rows = mysqli_fetch_array($result1);

foreach($rows as$row) {
//Will output the id column
echo $row['id']; //Will output the forename column echo$row['forename'];
}
?>

Feedback 👍
Comments are sent via email to me.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7465709,"math_prob":0.88771707,"size":2919,"snap":"2023-40-2023-50","text_gpt3_token_len":754,"char_repetition_ratio":0.13207547,"word_repetition_ratio":0.061810154,"special_character_ratio":0.26447412,"punctuation_ratio":0.12256809,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98413515,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-30T19:09:44Z\",\"WARC-Record-ID\":\"<urn:uuid:63adf743-72eb-4ef6-9171-cd3c915f6213>\",\"Content-Length\":\"50213\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:17a0ac2a-7272-41c2-b09e-c5e7d4554489>\",\"WARC-Concurrent-To\":\"<urn:uuid:ad5a441f-d020-4b0f-92fb-fd3d7a5993f8>\",\"WARC-IP-Address\":\"138.68.162.147\",\"WARC-Target-URI\":\"https://www.jamiebalfour.scot/courses/web/php/using-mysql-queries-in-php/\",\"WARC-Payload-Digest\":\"sha1:Y6ZFSPJUWMHQ734IULUDAY4KPQXC45WB\",\"WARC-Block-Digest\":\"sha1:RNZEBGOF4I2B4SJUKGR3WQB5JSS5VUZS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510707.90_warc_CC-MAIN-20230930181852-20230930211852-00807.warc.gz\"}"}