PHP/Mysql & SELECT
We have been looking at SELECT statement for sometime and I would like to apologise in advance but we are going to have to go through it again in this article. However , we are not just going over how to query rows and columns using SELECT , we are going to look at it from PHP language point of view and see how we can return those rows into a webpage. Of course , I am simplifying the whole process slightly because there will be topics we won’t be going into such as pagination. Perhaps in future. If you have forgotten all about SELECT statement , here is the link to my previous article explaining it in more details.
Lets take a brief look at the SELECT statement and this is how the syntax should look ,
SELECT column_name(s) FROM table_name
This is how the whole PHP/MySQL SELECT works ,
$con = mysql_connect("host-name","user-name","user-password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database-name", $con);
$result = mysql_query("SELECT * FROM Employee");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "";
}
mysql_close($con);
Of course we have been looking at “mysql_connect” function too many times. So if we are to remove all those items that are not necessary to our objective and view only those that are important items , this is what we been talking about all this time ,
$result = mysql_query("SELECT * FROM Employee");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "";
}
First we send a query , take note that unlike C# , all that PHP does is to send a command , a string , to the MySQL database. It has no control over how MySQL handles it or whether it is even a legal SQL statement. Then it passes the resultant array to the $row and using while loop , it goes over the entire array printing the result in each new line HTML BR. It is similar to “Console.WriteLine” except you need to specify the new line explicitly.
SELECT Statement Conclusion
I think this should do for now to the PHP/Mysql side of the tutorials and tomorrow onwards , we go back into C#. Mainly because this is what I will be doing in my part-time degree so need to concentrate more into C# rather than PHP. Perhaps , next time I can go into PHP again.