Skip to main content

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

PHP and MySQL

•Our previous examples focused on how to write correct MySQL syntax.

•Now, we are going to learn how to take advantages of MySQL queries in our PHP applications.

•PHP, offers number of built-in functions that deal with MySQL db.

•These functions communicate with MySQL db. and allow us to run MySQL queries through PHP Script.

Connectivity to MySQL

•Before getting access to database, we had to start a connection with the server where it lies on.

•To start a connection between your script and db. PHP offers function mysql_connect().

•This function takes three parameters, first one is MySQL server address. Second one is username for the user of the db. And last one is password for that user.

Example

mysql_connect(“localhost”,”anas”,”1234”);

•A call to mysql_connect() will start a connection with the specified MySQL server and username and password.

•This function returns a value of true or false.

•If the connection succeeded, then it returns true, so you can complete.

•If the connection failed, then it returns false, which means your script will not work well as expected.

•It is important to know whether the connection succeeded or not. And if not, determine where is the error.

•PHP offers a function that tells you what is the MySQL error message for your query.

•It is important to make your script stop working if the connection failed with your db server, this will increase security of your application.

Example:

<?php

$host = “localhost”;

$username = “anas”;

$password = “123”;

$connected = mysql_connect($host,$username,$password);

if(!$connected)

{

die(“Unable to connect to db<br />”.mysql_error() );

}

mysql_close($connected);

?>

Notes:

•$host: is the MySQL server address you want to make connection on.

•$username: is the username of the user who uses the MySQL server.

•$password: is the password of the user who uses the MySQL server.

•$connected: stores the returned value of function mysql_connect. Which is true or false.

•We checked for value of $connected. If it is true, then we can complete our application. Else then we had to stop executing the script, in order to avoid unexpected errors.

•Function die like function exit in C/C++. Its job is stopping the script execution.

•It is important to know what the error that prevented your query succession is. To get this, PHP offers mysql_error() function that returns a string contains the error message that MySQL server responded to you about your query.

•At end of each php script that communicates with MySQL server, it is preferred to close the connection, although this connection will be closed automatically at end of script. It is important to close it manually by calling function mysql_close() with parameter of the connection to be closed.

•Once we had connected to the MySQL server, we have to select which db we want to deal with.

•PHP offers function mysql_select_db() that takes two parameters, first one is database_name, second parameter is the connection.

•This function call comes after starting succeeded connection with db.

<?php

// config.php

$host = “localhost”;

$username=“anas”;

$password=“1234”;

$connected = mysql_connect($host,$username,$password);

if(!$connected) die(“unable to connect to db<br />”.mysql_error());

mysql_select_db(“company”,$connected);

mysql_close($connected);

?>

•To organize your project, and make development acceleration improve smoothly, it is preferred to save previous code in a file called for example: config.php and put this file in a directory named for example include.

•These steps, will save your time, and avoid you write these lines each time you want to connect to your MySQL server. Then include this file in each script you need to communicate with db.

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

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 أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...