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

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...

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

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

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...