Running MySQL Queries Using PHP

After connecting to the MySQL database you are ready to run some queries. The function used to perform queries is named - mysql_query(). The function returns a resource that contains the results of the query, called the result set. To examine the result we're going to use the mysql_fetch_array() function, which returns the results row by row. In the case of a query that doesn't return results, the resource that the function returns is simply a value true or false.
A convenient way to access all the rows is with a while loop. Let's add the code to our script:


//execute the SQL query and return records
$result = mysql_query("SELECT id, model, year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."".$row{'year'}."";

}



0 Responses to "Running MySQL Queries Using PHP"