Home Resources Forum Advertise Contact

Go Back   Webmaster Forums > Code Talk > PHP Programming

PHP Programming Find help, general discussions about PHP updates, issues and coding techniques here.

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 20th December 2006, 08:31 AM
hassen1 hassen1 is offline
WD Addict Poster
 
Join Date: 29th October 2006
Posts: 2,603
Send a message via Yahoo to hassen1
Default Database Manipulation

One of the defining features of PHP is the ease with which you can connect to and manipulate databases. PHP implements functions for connecting to a wide range of databases systems: MySQL, Oracle, MSSQL, Interbase, dBase, and many more. While there are many commercial database systems which cost thousands of dollars and provide thousands of components, the Internet community provides solutions for low-budget users as well. MySQL, for example, doesn’t require you to purchase a license if you don’t use it for commercial activities.

Most of the manipulating of any database is done while using SQL, which stands for Structured Query Language. This provides standard syntaxes by which different types of database can be queried. While there are a lot of extensions to SQL implemented by database systems, and some queries could work on a system but fail on another one, all of them incorporate some standard manipulating statements.

The way PHP talks to the database is simple. First you need to connect to the database system, which runs as a daemon (service for Windows) in the background. In order to do this, you must have appropriate permissions to connect to the database server from its owner. While you can always install and configure a database system on your own computer for starters, when it comes to hosting a web-site you must talk to a hosting company. They will open an account for you, and give you the information on how to connect to the database server.

Many hosting companies use MySQL, so we’ll focus on using this database system. First, you must use the “mysql_connect()” function along with the server’s IP, the username and the password required to connect to the server. After you connect, you can start running SQL statements as you like. Don’t forget that to close the connection using “mysql_close()”. You either must do this everytime you want to manipulate the database, or you use a persistent connection. This kind of connection is implemented when using the Apache web-server, and tells Apache not to terminate the connection after your script stops executing even if you call “mysql_close()”. Instead, the connection is left active, waiting for another process to call mysql_pconnect(). So, using this function, you will skip the time required for your computer to connect to the database server.


if(!mysql_connect(“localhost”, “bob”, “secret”)) //tries to connect to the database server

die(“Unable to connect to the database server!”); //terminates the script, and outputs the error



Depending on the hosting services, you can have access to one or more databases. But before you start changing data, you must also connect to the database of your choice. Selecting a database to work with is also accomplished using a simple function:

mysql_select_db(“test_db”) or die(“Error when selecting the database!”);



While we can treat the errors using the “die()” function, and stop the script if something bad happens, sometimes we might want to output more detailed information on the error. Whenever an operation fails, MySQL sets an error number and an error string, so we just have to read it:



if(!mysql_connect($db_ip, $db_user, $db_pass))

die(“MySQL Error: “ . mysql_error()); //stops the script if unable to connect to the database, and outputs the MySQL error message



All of the database manipulation is done thru SQL statements (also known as queries), which range from basic statements used to insert, modify or delete data in a table to complex statements used to retrieve some particular data using specified filters. You can find a lot of documentation on SQL on the Internet. Here are some of the most important statements:


//creates a table using the specified fields

CREATE TABLE users

(id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

username VARCHAR(20),

password VARCHAR(12),

email VARCHAR(20)

)


//adds a new record into the database

INSERT INTO my_table(id, username, password, email) VALUES (NULL, “bob”, “secret”, “bob@softwareprojects.org”)

//modifies the password of the user “bob”

UPDATE my_table SET password = “very secret” WHERE username = “bobo”

//retrieves the email address of the user “bob”

SELECT email FROM users WHERE username = “bob”



To execute these queries, you must call the “mysql_query()” function. The result of this function is a resource that will be used with other functions, in order to access the information returned by the query.

$sql_result = mysql_query(“SELECT password FROM users WHERE username=’Bob’”)

or die(“Error or running query: ” .$mysql_error())

$row = mysql_fetch_array($sql_result, MYSQL_NUM);



While it may seem that “mysql_fetch_array()” retrieves all the results from the query, you should know that it only returns one row at a time. After a call to this function, the internal index of $sql_result will be incremented by one, so the next time the next row will be retrieved, if there is any.

There are a lot of other built-in functions for MySQL, for a detailed explication of each one you should consult the PHP documentation.
Reply With Quote
  #2 (permalink)  
Old 24th May 2008, 02:52 PM
gprunescaper gprunescaper is offline
WD Newbie
 
Join Date: 19th May 2008
Posts: 44
Default Make Money with Free Online Game

[align=left]This method is more for fun than for money. You will get chance to win fantastic cash prizes completely free by playing free game online, (maplestory,runescape etc.)and win the scores. You will get some entries to enter the lucky draw based on how many scores you have made. If you lucky enough, you can get some rewards, such as cash or gifts. Some web sites even hold the campaign and award the top play with the prize or gift. Since you have no need to spend a dime during it, it's totally safe for you. Below are some very good sites where can win for prizes. gprunescape have been a professional, loyal and reliable RS2 gold store online, and also powerleveling seller for many years. You will find it is the best way to buy RS2 gold or leveling; you will really get what you want. This is your chance for you to buy RS2 gold and leveling up. And also provide making money guide,We are ready now, how about you? [/align]
__________________
GpRunescape.com
Reply With Quote
  #3 (permalink)  
Old 21st June 2008, 09:49 AM
Springbirdoo Springbirdoo is offline
WD Newbie
 
Join Date: 16th June 2008
Posts: 5
Lightbulb Pride and Poverty

An Irishman walked into a bar in Dublin. He ordered three pints of Guinness, sat in the back of the room, and drank a sip out of each one in turn. When he finished them, he came back to the bar and ordered three more. The bartender asked him, "You know, a pint goes flat(跑气) after I draw it. It would taste better if you bought one at a time." The Irishman replied, "Well, you see, I have two brothers. One is in America, the other in Australia, and I'm here in Dublin. When we all left home, we promised that we'd drink this way to remember the days when we drank together." The bartender admitted that this was a nice custom, and left it there. The Irishman became a regular in the bar, and always drank the same way. ..World Of Warcraft gold is WoW gold.One day, he came in and ordered two pints. All the other regulars noticed and fell silent. When he came back to the bar for the second round, the bartender said, "I don't want to intrude on your grief, but I want to offer my condolences on your great loss." The Irishman looked confused for a moment, then a light dawned in his eye and he laughed. "Oh, no, everyone's fine. I've just quit drinking.".
__________________
World Of Warcraft gold
Reply With Quote
Reply



Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 09:15 PM.


Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC6
vB Ad Management by =RedTyger=