|
|
MySQL Queries - Select
Select Queries in MySQL
Now that we have some data in our table, we can learn how to retrieve it. Data in MySQL is retrieved using the SELECT query. We also need to know how to get this data into PHP using mysql_fetch_array(). An example SELECT query:
SELECT * FROM `customers`
This query indicates that we want to select everything from the "customers" table (using the *). We can also specify that we only want particular columns:
SELECT `customerid`, `name` FROM `customers`
In PHP, we use the mysql_fetch_array() function in a loop, which provides us with an associative array for each row which is retrieved for the database:
mysql_connect("localhost", "admin", "password") or die("Couldn't connect: " . mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die("Unable to switch to database: " . mysql_error());
echo "Connected to Database<br />";
$results = mysql_query("SELECT * FROM `customers`");
while($row = mysql_fetch_array($results)){
echo "Name: ' . $row['name'] . " - Address: " .$row['address'] . " - Tax ID: " . $row['taxid'] . " - Customer ID: " .$row['customerid']
}
Name: Steve Smith - Address: 123 Fake Street - Tax ID: 123456 - Customer ID: 1
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
|