(إعداد السيرفر المحلي WAMP و MySQL) ربط تطبيق أندرويد مع MySQL و PHP و JSON

السلام عليكم ورحمة الله وبركاته

فهرس سلسلة دروس ربط تطبيق أندرويد مع MySQL و PHP و JSON :

  1. مقدمة وشرح توضيحي عن Web Services وعن المشروع.
  2. إعداد السيرفر المحلي WAMP و بناء قاعدة بيانات MySQL.
  3. التعامل مع PHP لبناء Web Service  تقوم بعمل Login و Register (مع شرح مبسط عن JSON) .
  4. التعامل مع PHP لبناء Web Service  بجلب بيانات المستخدمين الموجودة بقاعدة البيانات.
  5. بناء مشروع أندرويد  جديد و إعداد واجهة تطبيق الأندرويد.
  6. تطوير كلاسات Register و Login وJSONParser في أندرويد.
  7. تطوير UsersActivity و ListViewAdapter لعمل قائمة المستخدمين بالتنسيق الذي نحتاجه.

 

في هذا الدرس سنقوم بتنصيب و إعداد السيرفر المحلي وبناء قاعدة البيانات.

  • الخطوة الأولى: تنصيب السيرفر المحلي WAMP Server والتأكد من عمله على LocalHost

يوجد الكثير من البرامج التي تسمح لك بإعداد سيرفر محلي خاص ولكن في هذه السلسلة سوف نستخدم WAMP بإمكانك تحميل البرنامج من خلال هذا الرابط :

http://www.wampserver.com/en

بعد تثبيت WAMP قم بتشغيل البرنامج حتى تصبح أيقونة البرنامج خضراء كما في الصورة

wamp-green

للتأكد من عمل السيرفر قم بفتح المتصفح و اكتب في شريط العنوان http://localhost

من المفترض أن تحصل على صفحة مثل هذه

localhost

لعمل مشروع جديد اذهب إلى مجلد www الموجود في C:\wamp\www أو مكان ما قمت بتنصيب السيرفر

قم بعمل مجلد جديد وقم بتسميته “android-webservices” أو أي اسم تود إختياره حيث سنقوم بوضع جميع ملفات PHP الخاصة بالمشروع في هذا المجلد.

بداخل المجلد الذي أنشأته قم بإضافة ملف نص جديد قم بتسميته “index.php

قم بفتح هذا الملف بإستخدام أي محرر نصوص تفضله أنا أستخدم NotePad++  بداخل ملف “index.php” قم بكتابة الكود التالي

<?php

echo "This is my first PHP code";

?>

 

الآن في المتصفح قم بكتابة العنوان التالي (قم بتغيير android-webservice إلى اسم المجلد الذي قمت بإنشائه)

http://localhost/android-webservice

ستظهر لك صفحة مكتوب فيها “This is my first PHP code” كما هو موجود في الصورة التالية:

php-first

وهكذا تأكدنا من عمل PHP ، في هذه السلسلة لن أتطرق إلى شرح تفاصيل حول لغات البرمجة PHP أو Java في الأندرويد على إفتراض أن لديك خلفية ولو بسيطة حولها ولكن سأحاول قدر الإمكان شرح المفاهيم المعقدة.

  • الخطوة الثانية: بناء قاعدة البيانات MySQL

قم بالتوجه إلى PHPMyAdmin من خلال هذا الرابط:

http://localhost/phpmyadmin

ثم انقر على Database

phpmyadmin

في حقل 

db-name

 

الآن قم بالنقر على Databases ثم انقر على اسم قاعدة البيانات التي أنشأتها. سيتم نقلك إلى صفحة إنشاء الجداول.

في هذا المشروع سأقوم بعمل جدول واحد فقط بإسم users  يحتوي على 4 أعمدة user_id , user_username, user_password, user_displayname

قم بوضع البيانات كما في الصورة التالية:

tabel

 

table-structure

كود SQL  لقاعدة البيانات و جدول users

--
-- Database: `android_webservices`
--
CREATE DATABASE IF NOT EXISTS `android_webservices` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `android_webservices`;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_username` varchar(255) NOT NULL,
  `user_password` varchar(255) NOT NULL,
  `user_displayname` varchar(255) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

 

  • إعداد ملف config.php للإتصال بقاعدة البيانات:

بعد أن قمنا بإنشاء قاعدة البيانات بقي علينا كتابة أكواد PHP  لنقوم بإضافة و حذف و تعديل قاعدة البيانات ولكن بدل أن نقوم بالإتصال بقاعدة البيانات في كل ملف PHP  سنقوم بكتابة بيانات الاتصال بقاعدة البيانات في ملف واحد بإسم config.php حيث سنقوم فقط بإضافة اسم الملف فقط في بداية كل ملف PHP  نقوم بإنشائه لاحقا.

وجدت في هذا الموقع شرح تفصيلي لكيفية انشاء نظام تسجيل محمي مع كتابة تعليقات مفصلة للكود (تسجيل مستخدم جديد – تسجيل دخول – تسجيل خروج و تعديل بيانات المستخدم ) وقمت بأخذ نسخة من كود الاتصال بقاعدة البيانات>

هذا رابط الموقع :

http://forums.devshed.com/php-faqs-stickies-167/program-basic-secure-login-system-using-php-mysql-891201.html

الآن في مجلد android-webservice الذي أنشأناه في www قم بإنشاء ملف نص جديد بإسم config.php وضع به الكود التالي:

قم بتغيير البيانات التالية بما تتناسب معك

$username = "root";&nbsp;// غالبا اسم المستخدم هو root ان قمت بتغيير الاسم فقم بوضعه هنا
$password = "";&nbsp; // ان أضفت كلمة مرور لقاعدة البيانات فضعها هنا 
$host = "localhost";&nbsp;
$dbname = "android_webservices"; // قم بتغيير قاعدة البيانات باسم قاعدة البيانات لديك

 

كود ملف confog.php

<?php 
/***
* @Source: devshed
* @author E-Oreo
* http://forums.devshed.com/php-faqs-stickies-167/program-basic-secure-login-system-using-php-mysql-891201.html
*/
    // These variables define the connection information for your MySQL database 
	$username = "root"; 
    $password = ""; 
    $host = "localhost"; 
    $dbname = "android_webservices"; 

// UTF-8 is a character encoding scheme that allows you to conveniently store 
    // a wide varienty of special characters, like ¢ or €, in your database. 
    // By passing the following $options array to the database connection code we 
    // are telling the MySQL server that we want to communicate with it using UTF-8 
    // See Wikipedia for more information on UTF-8: 
    // http://en.wikipedia.org/wiki/UTF-8 
    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
     
    // A try/catch statement is a common method of error handling in object oriented code. 
    // First, PHP executes the code within the try block.  If at any time it encounters an 
    // error while executing that code, it stops immediately and jumps down to the 
    // catch block.  For more detailed information on exceptions and try/catch blocks: 
    // http://us2.php.net/manual/en/language.exceptions.php 
    try 
    { 
        // This statement opens a connection to your database using the PDO library 
        // PDO is designed to provide a flexible interface between PHP and many 
        // different types of database servers.  For more information on PDO: 
        // http://us2.php.net/manual/en/class.pdo.php 
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
    } 
    catch(PDOException $ex) 
    { 
        // If an error occurs while opening a connection to your database, it will 
        // be trapped here.  The script will output an error and stop executing. 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code 
        // (like your database username and password). 
        die("Failed to connect to the database: " . $ex->getMessage()); 
    } 
     
    // This statement configures PDO to throw an exception when it encounters 
    // an error.  This allows us to use try/catch blocks to trap database errors. 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     
    // This statement configures PDO to return database rows from your database using an associative 
    // array.  This means the array will have string indexes, where the string value 
    // represents the name of the column in your database. 
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
     
    // This block of code is used to undo magic quotes.  Magic quotes are a terrible 
    // feature that was removed from PHP as of PHP 5.4.  However, older installations 
    // of PHP may still have magic quotes enabled and this code is necessary to 
    // prevent them from causing problems.  For more information on magic quotes: 
    // http://php.net/manual/en/security.magicquotes.php 
    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
    { 
        function undo_magic_quotes_gpc(&$array) 
        { 
            foreach($array as &$value) 
            { 
                if(is_array($value)) 
                { 
                    undo_magic_quotes_gpc($value); 
                } 
                else 
                { 
                    $value = stripslashes($value); 
                } 
            } 
        } 
     
        undo_magic_quotes_gpc($_POST); 
        undo_magic_quotes_gpc($_GET); 
        undo_magic_quotes_gpc($_COOKIE); 
    } 
     
    // This tells the web browser that your content is encoded using UTF-8 
    // and that it should submit content back to you using UTF-8 
    header('Content-Type: text/html; charset=utf-8'); 
     
    // This initializes a session.  Sessions are used to store information about 
    // a visitor from one web page visit to the next.  Unlike a cookie, the information is 
    // stored on the server-side and cannot be modified by the visitor.  However, 
    // note that in most cases sessions do still use cookies and require the visitor 
    // to have cookies enabled.  For more information about sessions: 
    // http://us.php.net/manual/en/book.session.php 
    session_start(); 

    // Note that it is a good practice to NOT end your PHP files with a closing PHP tag. 
    // This prevents trailing newlines on the file from being included in your output, 
    // which can cause problems with redirecting users.

?>

 

قمنا الآن بإعداد قاعدة البيانات و ملف الإتصال بقاعدة البيانات. في الدروس القادمة سوف نبدأ بتطوير ال Web Services المطلوبة و سأشرح بالتفصيل الممل طريقة عملها بالإضافة الى شرح JSON.

 

بإمكانك تحميل المشروع من خلال GitHub :
https://github.com/ahmadssb/Android-PHP-MySQL-JSON-Tutorial.git

9 Comments

Add a Comment
  1. WordPress › Error

    There has been a critical error on this website.

    Learn more about troubleshooting WordPress.