Skip to main content

PHP Tutorial–Lesson 5: Introduction to PHP- Part II

PHP and MySQL

MySQL query through PHP script:

•Remember rule of single/double quotes when dealing with non-php script inside php script? We’ll apply it now with MySQL queries.

•Each MySQL query will be stored in a variable called for example $sql.

•We’ll use a built-in function that executes the query, the function is mysql_query(), and takes as parameter $sql value i.e. MySQL query.

•The returned value of this function is true or false, we’ll store it in a variable called for example $result.

•Finally, we’ll check whether your query executed successfully or not.

•These steps will be followed at each time we want to run MySQL query using PHP.

•We’ll suppose that you saved your config.php file that includes your connection configuration in an external directory called include. for more information on how to configure connection to MySQL server revise Previous Lesson on the Link: PHP Tutorial–Lesson 5: Introduction to PHP- Part I

Example:

<?php

// Employees.php

// Created in 13/10/2009

require(“../include/config.php”);

$sql = “SELECT * FROM employee”;

$result = mysql_query($sql);

If(!$result) die(“MySql Error: <br />“.mysql_error());

$tableHeader = ‘<table border=“1” width=“100%”>

<tr>

<th width=“10%”>ID</th>

<th width=“30%”>Emplyee Name</th>

<th width=“30%”>Department</th>

<th width=“20%”>Job Title</th>

<th width=“10%”>Salary</th>

<th width=“10%”>Telephone</th>

</tr>’;

echo $tableHeader;

while($row = mysql_fetch_array($result))

{

echo “<tr>”;

echo “<td>”.$row[‘id’].”</td>”;

echo “<td>”.$row[‘name’].”</td>”;

echo “<td>”.$row[‘department’].”</td>”;

echo “<td>”.$row[‘jobTitle’].”</td>”;

echo “<td>”.$row[‘salary’].”</td>”;

echo “<td>”.$row[‘telephone’].”</td>”;

echo “</tr>”;

}

echo “</table>”;

?>

Notes:

•Require: in order to make connection with MySQL server, We used require to insure that this script depends completely on connecting to db. If the file is not found then the script will not work, and prevent it from errors. remember difference between require and include in PHP Tutorial - Lesson 2: Introduction to PHP - Part III at Server Side Include SSI part.  

•../include/config.php: if the included file in a parent directory, and your script in a child directory, then the way to reach the included file is putting ../ At beginning of the address. If both the included file and the script are in same directory then we directly write it like this: require(“config.php”);

•Purpose of this script is displaying all employees information in the company.

•The MySQL query for this case is SELECT.

•$sql is a variable that stores a non-php code, in our case it stores MySQL query.

•Note until this line, your query has not yet been executed.

•$result is a variable that stores MySQL response for your query. Its value depends on the returned value of mysql_query().

•mysql_query($sql) is the function that makes your query become executed. This function takes one parameter which is MySQL query. And returns the response of MySQL. If the query succeeded then response will be stored in $result. Else false value will be stored in $result.

•After that we checked whether $result has been executed successfully or not. If not then we force the program to stop execution by calling die function and printing the error message from MySQL using mysql_error function.

•These couple of lines will be used at every MySQL query executed from php script, regardless what the query is.

•$tableHeader is a variable that holds html format for table.

•We stored this in a variable in order to be able to use it as needed.

•When mysql_query returns MySQL response about a query, it returns all records regardless how much they are in the same variable $result.

•PHP offers a built-in function called mysql_fetch_array() that separate each record in the returned query and save it in an array called $row.

•$row is a multi-dimensional associated array, which means we refer to each cell in it by key value, not index number.The key value of $row is your table columns.

•Finally, we loop through all records using while loop. Until reaching null value to $row which means this is end of records.

To download this lesson as PDF file click here: PHP Tutorial–Lesson 5: Introduction to PHP- Part II

Best Wishes,

Anas Jaghoub

Comments

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and I ntegrate with external services called web services and APIs. As well the application M anages its own objects and components from views to models to controllers to plugins and so on. A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should ha ve intensive focus on how each component work and how can be used, what can be done, and what cannot. On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a hi