Sun
21
Jan
Small Business Blog

Most mutual websites today visit accumulation to be presented dynamically and interactively supported on signaling from the user. For example, a client haw requirement to finger into a retail website to analyse his purchase history. In this instance, the website would hit stored digit types of accumulation in visit for the client to action the analyse - the client s individualized login info and the client s purchased items. This accumulation crapper be stored in digit types of hardware - insipid files or databases.

Flat files are exclusive viable in rattling baritone to baritone intensity websites as insipid files hit 3 inexplicit weaknesses:

  1. The quality to finger the data. This makes it needed to potentially feature ALL the accumulation sequentially. This is a field difficulty if there are a aggregation of records in the insipid enter because the instance required to feature the insipid enter is relative to the sort of records in the insipid file.

  2. The quality to expeditiously curb admittance by users to the data

  3. The wasteful hardware of the data. In most cases, the accumulation would not be encrypted or shut as this would anger the difficulty no. 1 above

The deciding which is, in my opinion, the exclusive viable method, is to accumulation the accumulation in a database. One of the most current databases in ingest is MySQL. Data that is stored in a database crapper easily be indexed, managed and stored efficiently. Besides that, most databases also wage a flat of concomitant utilities that earmark the database chief to reassert the database - for example, patronage and restore, etc.

Websites cursive using PHP are rattling substantially suited for the MySQL database as PHP has a bespoken and desegrated MySQL power that communicates rattling expeditiously with MySQL. PHP crapper also transmit with MySQL finished the accepted ODBC as MySQL is ODBC-compliant, However, this power not be as economical as using the bespoken MySQL power for PHP.

The rest of this article is a tutorial on how to ingest PHP to:

  1. Connect to a MySQL database

  2. Execute accepted SQL statements against the MySQL database

Starting a Session with MySQL

Before the PHP playscript crapper transmit with the database to query, append or update the database, the PHP playscript power prototypal requirement to enter to the MySQL computer and take which database in the MySQL computer to control on.

The mysql_connect() and mysql_select_db() functions are provided for this purpose. In visit to enter to the MySQL server, the computer name/address a username and a legal countersign is required. Once a unification is successful, the database needs to be specified.

The mass 2 cipher excerpts elaborate how to action the computer unification and database selection:

@mysql_connect(”[servername]”, “[username]”, “[password]”) or die(”Cannot enter to DB!”)

@mysql_select_db(”[databasename]”) or die(”Cannot superior DB!”)

The @ cause is utilised to bury some nonachievement messages that mysql_connect() and mysql_select_db() functions haw pass if an nonachievement occurred. The die() duty is utilised to modify the playscript enforcement and pass a bespoken nonachievement message.

Executing SQL Statements against a MySQL database

Once the unification and database activity is successfully performed, the PHP playscript crapper today travel to control on the database using accepted SQL statements. The mysql_query() duty is utilised for executing accepted SQL statements against the database. In the mass example, the PHP playscript queries a plateau titled tbl_login in the previously designated database to watch if a username/password unify provided by the individual is valid.

Assumption:

The tbl_login plateau has 3 columns titled login, password, last_logged_in. The last_logged_in article stores the instance that the individual terminal logged into the system.


// The $username and $passwd uncertain should justifiedly be ordered by the login form

// finished the POST method. For the watch of this example, we re manually writing it.

DECODING WEB HOSTING REVIEWS, PART II: FROM JAVA TO WINDOWS
...

MAKE A WEBSITE IN FEW HOURS
...

AUTOMATING YOUR ONLINE BUSINESS
...

FREE HOSTING MYTHS AND FACTS
...

INTERNET MARKETING STRATEGIES - PART NINE: BUILDING A LIST OF POTENTIAL CUSTOMERS
...

$sql=”SELECT * FROM tbl_login WHERE login = “.$username.” AND countersign = “.$passwd.” “ // Execute the SQL evidence against the currently designated database. // The results power be stored in the $r variable. $r = mysql_query($sql) // After the mysql_query() bidding executes, the $r uncertain is examined to // watch of the mysql_query() was successfully executed. if(!$r) { $err=mysql_error() print $err exit() } // If everything went well, analyse if the ask returned a termination - i.e. if the username/password // unify was institute in the database. The mysql_affected_rows() duty is utilised for this purpose. // mysql_affected_rows() power convey the sort of rows in the database plateau that was affected // by the terminal query if(mysql_affected_rows()==0){ print “Username/password unify is invalid. Please essay again.” } else { // If successful, feature discover the terminal logged in instance into a $last uncertain for pass to the user $row=mysql_fetch_array($r) $last=$row[”last_logged_in”] print “Login successful. You terminal logged in at “.$last.”.” }

The above warning demonstrated how a SELECT SQL evidence is executed against the designated database. The aforementioned method is utilised to fulfil another SQL statements (e.g. UPDATE, INSERT, DELETE, etc.) against the database using the mysql_query() and mysql_affected_rows() functions.

About The Author

This PHP scripting article is cursive by Evangelist L. Evangelist L is the Webmaster of The Ultimate BMW Blog! (http://www.bimmercenter.com).

The Ultimate BMW Blog!

daboss@bimmercenter.com

.

Author:
Small Business Blog
Time:
Sunday, January 21st, 2007 at 6:30 am
Category:
Web Design Tips
Comments:
You can leave a response, or trackback from your own site.
RSS:
You can follow any responses to this entry through the RSS 2.0 feed.
Navigation:

Leave a Reply

You must be logged in to post a comment.