Main | School

Simple PHP Database Commands

PHP is very important in interacting with databases. Since most of our interaction with databases will be from a web page, PHP is needed to provide that conneciton. Here is a breif overveiw of PHP commands that can help your website read, write, and manage your MySQLi database.

real_connect:

Real Connect is probably the single most important command in the MySQLi library, because it actually allows your web pages to connect to the dataabase. To use this command, you will need a lot opf information:
host: The host name or IP address of the database.
username: The username for your MySQL database account.
password: The password to access the data base.
name: The database name.
There are also 2 optional parameters;
port: port number
socket: The specific socket or pipe to be used.

After connecting to the database, your website can begin sending and reading information.

close:

Close is the second most important command. It is very easy to implement, the only parameter it requires is the exact database to be closed. Closing the connection to a database will ensure that data does not accidentally get written, whcih can be very bad depending on how important the data is. Sometimes not closing a database can cause trouble with other sites attampting to open a connection, so itt is always a good idea to close the connection once transactions are complete.
example: mysqli_close (mysqli $link); where $link is the link variable for the database.

autocommit:

This command controls wheither the databe will autocommit new data or not. To commit data is to save it in the database, as well as any changed made to the database itself. Having autocommit is useful when you need to store data submitted by users, but you may wish to turn it off in some circumstances.
If the database address is assigned to the variable $mysqli, the syntax to activate autocommit would be $mysqli->autocommit(TRUE);

Alternativley, any statement that equaits to a boolean can be used instead of TRUE. To turn it off, simply use $mysqli->autocommit(FALSE);. An alternate form exists, which lets you decide which database you want to maipulate: mysqli_autocommit($link, TRUE); where $link is the connection variable for the database.

commit:

Commit is another important command. In most database query languages, 'commit' will save information added to database tables, or changes made to the table structure. Use the commit comand when you need to periodacly save changes made to the database. This is an alternative to autocommit, is case the user may need to quickly edit changes in entry data.
example: $mysqli->commit(); where $mysqli is the link variable.

rollback:

Rollback is a very handy command for those that make mistakes. A rollback command will erase all changes since the most recent commit. This is useful for users that may want to undo edits to the database table without risking altering other bits of data. Be careful when using rollback, as the erased changes can't be recovered.
example: $mysqli->commit(); where $mysqli is the link variable.

Contact Me | Policies | Colophon

© 2009, Jonathan Dunstan, All Rights Reserved.