How to connect MySQL in Prado Framework

Hello!

This is my first article tutorial in this blog, today my friend have a problem connecting SQL databases with PRaDO framework. I have used this framework several years ago, and it’s quite easy to make a website with Prado. This time I will write how to connect MySQL with Prado, insert data, and display it in the web page.

First, I’m using XAMPPLITE as webserver, XAMPPLITE is lite version of XAMPP, but if you are using XAMPP it’s ok. Download XAMPP.

After you finish the XAMPP download, continue with download Prado framework. Download Prado.

Install the XAMPP, and extract the prado framework in htdocs folder.

The optional prado-cli.php PHP script file in the framework directory provides command line tools to perform various tedious takes in Prado. The prado-cli.php can be used to create Prado project skeletons, create initial test fixtures, and access to an interactive PHP shell. We will use command line tool to generate our prado project.

1. Make setPath

First I made setpath.bat to declare PHP path, so we can called any file in PHP path easier.

Make a file setpath.bat ( I put this file in C:\)

PATH=C:\xampplite\php\;

set the path to your php directory.

2. Generate the Project

Open the command prompt, and execute the setpath.bat by typing the filename setpath.bat

1 setpath

Go to htdocs

2 htdocs

Let’s generate project, execute this line

php C:\xampplite\htdocs\prado\framework\prado-cli.php -c pradocontact

When the generate success the command line will display line like this

3 generate project

3. Open in browser

Open your browser and navigate to http://localhost/pradocontact/ if you see the prado welcome page, congratulations you have been generate prado project successly.

4 welcome to prado

4. Create Database

Navigate to http://localhost/phpmyadmin/ and create new database. Type “pradocontact” in create new database field and hit create button.
5 create database
Then make a new table called “contact” with 2 field (id and name). Set the id field type into tinyint, auto_increment, and as primary key. Set the field name type as varchar and give the length 100.
6 create table
7 field table


CREATE TABLE `contact` (
`id` TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;

After the table created, insert 2 rows data into contact table.
INSERT INTO `contact` ( `id` , `name` )
VALUES (
'1', 'leo'
), (
'2', 'ganda'
);

5. Set connection in application.xml

Browse your project folder and open the protected folder C:\xampplite\htdocs\pradocontact\protected

You will see a file “application.xml”, open it and edit.



	
    
  	

  	

We make a ConnectionString to mysql database, dbname is our database that used for this project. Set the username and password with your database account.

6. Create Active Record

To use active record, I will make a new folder called database inside the protected folder to store the ActiveRecord file. After the database folder created, open the application.xml once again, and declare the database path.


  
 

Your application.xml should be look like this




   
  
  

  
 
	
    
  	
  	
  


  
  
    
  

  

After you finish editing the application.xml, let’s generate Active Record from our database in MySQL.
Open the command prompt again, go to C:\xampplite\htdocs\pradocontact\ and execute php C:\xampplite\htdocs\prado\framework\prado-cli.php shell .

execute generate contact Application.database.ContactRecord

8 generate AR

Open your database folder with explorer, and you will find a new file called ContactRecord.php, try to open it.


7. Test The Active Record

Ok, let test our Active Record using command line tools, type “ContactRecord::finder()->findAll()” and hit enter. See the image below.
9 test AR
The command line return an array with 2 record from the mySQL database and that means our activeRecord running well!

8. Edit Home.page

paste this code on your Home.page file

<%@ Title="Contact List" %>

<h1>Contact List</h1>

<a href="<%= $this->Service->constructUrl('insert')%>">Create New Contact</a>
<br/>

<com:TDataGrid ID="UserGrid"
DataKeyField="id"
AutoGenerateColumns="false"
>

<com:TBoundColumn
HeaderText="ID"
DataField="id" />

<com:TBoundColumn
HeaderText="Name"
DataField="name" />

</com:TDataGrid>

9. Make new file Home.php

Paste this code on your Home.php

UserGrid->DataSource=ContactRecord::finder()->findAll();
        // binds the data to interface components
        $this->UserGrid->dataBind();
    }
}
?>

10. Make a new file insert.page


<%@ Title="Insert Contact" %>

<com:TForm>

<h1>Insert Contact</h1>

<span>Name:</span>
<br/>
<com:TTextBox ID="Name" />

<com:TButton Text="Create" OnClick="createButtonClicked" />

</com:TForm>

11. Make a new file insert.php

class insert extends TPage
{
    
    public function createButtonClicked($sender,$param)
    {
        if($this->IsValid)  // when all validations succeed
        {
            // populates a UserRecord object with user inputs
            $ContactRecord=new ContactRecord;
            $ContactRecord->name=$this->Name->Text;
            
 
            // saves to the database via Active Record mechanism
            $ContactRecord->save();
 
            // redirects the browser to the homepage
            $this->Response->redirect($this->Service->DefaultPageUrl);
        }
    }
}

?>

10 file structure

Navigate your browser to http://localhost/pradocontact/ you will see this page
11 final

Ok, thanks for reading. Have a nice day! 🙂

Download Source
Download SQL

22 thoughts on “How to connect MySQL in Prado Framework”

  1. Wah..thanx banget ya dah buatin tutorial ini:)
    berkat kamu mudah2an aja skripsi ku lancar:)
    thanx ya… ^-^

  2. wow bro, you have a new site, but you never abandon wordpress to manage your site…..
    I have the same problem, but I dont use Xampp as the support program..I am purely using PHP and MySQL separately and the system format is NTFS…
    I think I have been having this problem since I did my final project..
    recently, It’s still ensuing.. I hope I dont endure you a lot…my friend..hehehe

    I am looking forward to having your help bro…

    Kind regards

    Hery

    1. Hi Hery, 🙂 yeah I’m WP Holic

      I’m using NTFS file system but it’s running well so far. Check your php.ini, and make sure it’s suit with Prado

  3. Are you using Xampp for running the prado application? I wonder if there is nothing ensued if you use PHP and MySQL separately..I have been figuring it out..but Till now I haven’t found the trick to accomplish this one…

    You ever said that there was another configuration in Xampp that could help me to run the prado application..
    Maybe you can help me ..hehehehe

    oh Once more, I have checked the dynamic link libraries out that are used for running prado …but there is no change..

    help me please..help me please..hehehe thanks my friend..

    1. Yeap I’m using XAMPP.
      Hmmm, to run Prado at least you need PHP5 on your server. Make sure your PHP version is 5 above.

      Maybe you can try to install XAMPP on your computer.

  4. Thanks Thanks Thanks !!!
    I was looking for a good tuto about Prado & Active Record, and you put it on the network. Very useful, a great job!

  5. oe brother, I can do to edit and delete ..
    i am new in meadow, nose if you could add or do to edit and delete records already inserted.
    thanks

  6. dude this is awesome, i was relegated to using prado with a custom sql db wrapper coz i couuldnt set up the auto gen on win with xampp, thanks a mill.
    holla
    kesh

  7. When I write the the following command to the command line prompt i get the following error. Please help! 🙁

    Command:
    generate contact Application.database.ContactRecord

    Error:
    Parse error: syntax error, unexpected T_STRING in D:\xampplite\htdocs\prado\framework\3rdParty/phpShell\php=shell-cmd.php(51) : eval()’d code on line 1 false

    Regards

  8. when you fail to “execute generate contact Application.database.ContactRecord” because refused connection or cannot connect mysql socks.

    add this

    <database ConnectionString="mysql:host=localhost;dbname=website;unix_socket=/tmp/mysql.sock" …..

    which is /tmp/mysql.sock is the location of your mysql.sock

  9. thanks a lot,
    very useful for me

    i hope my final project about expert system will be success…

  10. Really great tutorial .. by reading i am able to get that how to connect with db .. nice work .. where i can find rest of the turotial that select , update , delete action ??

Comments are closed.