First Steps into the World of PHP

So a tweet goes out from one of my favourite websites on the Internet BoroGuide asking if anyone knows anything about PHP and MySQL. Knowing nothing about PHP I respond with I’ll help you with the MySQL part, but PHP will be straight over my head.

Turns out the PHP stuff is fairly straight-forward. The following is nothing ground breaking, but just more of a reference for myself in case I need to revisit any of this in the future. For what its worth, its also not tested as I don’t have a ready-made PHP environment to run in.

1. Connecting to a MySQL database and running a query in PHP

   // Make a MySQL Connection
   mysql_connect(<server>,<user>,<passwd>) or die(mysql_error());
   mysql_select_db(<db_to_use>) or die(mysql_error());
   // Retrieve all the data
   $likeclause = "matchclause";
   $result = mysql_query("SELECT * FROM table WHERE column like '%$likeclause%'") or die(mysql_error());

   // Retrieve number of rows retrieved
   $totalRows = mysql_num_rows($result);

2. Example if…else… clause in PHP

   if ($totalRows == 0)
      // No data for this date
      echo "No data retrieved";
      // got some data

3. To iterate over the ResultSet

while ($row = mysql_fetch_array( $result ))
   echo $row['<column_name'];

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s