MySQL client API in C, Python and PHP

This article illustrates how to access to local or remote MySQL server from the client API in Python, C and PHP.

Refer to MySQL server installing and test to install a MySQL server on your Acme Board with the database used on this examples.

Connecting to MySQL server with Python

Install MySQLdb module from Debian repository by typing:

~# apt-get update
~# apt-get install python-mysqldb

The try this example:

addrlist.py

~# python addrlist.py
(1L, 'Acme Systems srl', '+39 (06) 99-12-187', 'www.acmesystems.it')
(2L, 'Atmel Corporate', '+1 (408) 441-0311', 'www.atmel.com')
(3L, 'Digikey', '+1 (800) 344-4539', 'www.digikey.com')
(4L, 'Mouser', '+39 02 575 065 71', 'www.mouser.com')

Connecting to MySQL server with C

Install MySQL C client library from Debian repository by typing:

~# apt-get update
~# apt-get install libmysqlclient-dev

The try this example:

addrlist.c

Compiling it in this way:

~# gcc addrlist.c -lmysqlclient -o addrlist
~# ./addrlist
1 Acme Systems srl +39 (06) 99-12-187 www.acmesystems.it 
2 Atmel Corporate +1 (408) 441-0311 www.atmel.com 
3 Digikey +1 (800) 344-4539 www.digikey.com 
4 Mouser +39 02 575 065 71 www.mouser.com

Connecting to MySQL server with PHP

Install the PDO driver:

~# apt-get update
~# apt-get install php5-mysql

Restart lighttpd web server:

~# /etc/init.d/lighttpd restart

Save in /var/www/ the following PHP script:

addrlist.php

Open a browser on the following url:

We suggest to enable the display_errors parameter on line 531 of /etc/php5/cgi/php.ini file during the development:

display_errors = On

and restart the lighttpd web server:

~# /etc/init.d/lighttpd restart

Related links