June 7th, 2003 12:56 AM
basic php with a database
This tutorial has the purpose of teaching basic php setup, but before learning the code you need to setup your machine to be able to operate php with a database. You will set up Red Hat 8.0 for using php with a database, I am sure the setup is very similar for most distros. The power of php lies in the possibility of accessing a database for using its content for some purpose. This tut will help you to configure your box so that you can start playing with php and a database, so that you can do the same as if you had a real web server and a domain. I did all the setup using Gnome and not command line.
After the setup is complete, I will follow with some basic php code for using a database.
-Setup: If you did not choose to install a web server and databse during installation, add them now. When this is done, click on the equivalent of the windows start menu button go to server setting -> services: make sure you click the boxes of httpd, postgresql, mysqld -> restart these services.
-Configuring http: go to /etc/httpd/conf/httpd.conf open httpd.conf with any text editor -> look
for the line that says servername write localhost after this line //this means your machine is
the server. Look for a line that says document root verify that after that it says /var/www/html //this is the directory in which files are stored for when you type in your browser http://localhost/index.php it will look for file index.php in the dir you specified above.
Look for a line saying directory index after it you should write the name of the default file
that will be opened when you type just http://localhost. examnple: directory index test.php this means test.php will be accessed by default.
Save httpd.conf and restart the service.
-Configuring php and creating a database.
go to /etc/php.ini open it in a text editor and edit the following: short_open_tag=on this is
for when you write php code it indicates the start of a code by: <? instead of: <php><?
Also change: register_globals=on
The cool thing about linux is that you don't need to download a php interpreter.
Creating a database: many people use mysql, for this example I am using postgresql:
go to a terminal at the prompt type:
$createuser usr1 //then tell it yes when it asks if the user can create tables and databases
$psql - d testdb
Now you have created a database named testdb.
go to /var/lib/pgsql/data open postgresql.conf in a text editor delete the * before the
"tcpip_socket=true" line In the same dir open pg_hba.conf delete the * before the "host all"
line. Restart httpd and postgresql services.
Php code: php is embedded within html, you can combine php with html tags, I am not gonna
explain any html. <? indicates the start of php ?> indicates end. People familiar whith C will
find php easier since many things are in common: you end lines with ; for and while loops have the same syntax, if's, etc.
Variables in php: are always preceded with $ you don't need to declare data types or initialize
variables, you just write them on the fly, example:
printf("the answer is $result
Now the important stuff, accesing your database with php code:
$conect= pg_connect("host=localhost dbname=testdb user=anyuser password=anypwd");
//this has opened a connection to your db, user is not the same one that created the db, it can be any user you have created.
$result1= pg_exec("create table table1(id int, name varchar(20), address varchar(100))");
//this shows how to use sql queries with php to create, update, delete or insert into different
//tables within your db
$result2= pg_exec("insert into table1 values(1, 'john smith', 'street 100')");
//next is another important thing, selecting and printing your table:
$result3= pg_exec("select * from table1");
$n= pg_num_rows($result3); //returns the number of rows in your table
//loop for printing your table:
//this is like a matrix of coordinates:
$id= pg_result($result3,$i,0); //in the first pass of the loop: row 0 column 0 id
$name= pg_result($result3,$i,1); //in the first pass of the loop: row 0 column 1 name
$address= pg_result($result3,$i,2); //in the first pass of the loop: row 0 column 2 address
//every increment in the loop means a different row
printf("$id $name $address
"); //outputs every entry
If you are using a different dbms the syntax changes a bit for example using mysql instead of
postgresql: pg_connect = mysql_connect pg_exec = mysql_query pg_result = mysql_result.
Well that is the basics for manipulating a db in php, of course there are many other things you
can do like creating a form in html for adding, deleting and updating a table, which I can write
a tutorial for that if I get requests. You can find lots of info on millions of php functions
and syntax in http://www.php.net where anybody can add their input on new useful functions which is a great advantage of php.
This was my first attempt at writing a tutorial, hope it helps. Any input and comments will be
Copyright © Juan Mier 2003. Please give credits to me if you distribute this tut and don't
plagiarize it or claim it as yours... Thanks