article

PHP And MySQL Tutorial

Email
Submitted on: 1/1/2015 9:09:00 PM
By: Marc ''InSp3KtaH'' Giroux (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 888
 
     This tutorial shows beginners how to use mysql with the basic commands. If you get stuck or want more go to www.mysql.com and read the documentation.

This article has accompanying files
 
				
MySQL Databases seem like a lot of work to use but it's not really that hard. Let's start.
To connect to a MySQL database you need to do the following:
$link = mysql_connect("host","user","pass");
details: $link is the value that will enable you to work with the database afterwards (eg: close it), the mysql_connect tells php to open a connection to mysql host "host" then use user and pass for username and password. The following line of code is recommended before you do anything else with the database:

if (! $link)
die("Error: Could not connect to MySQL");

details: this means if the link is empty then kill the script because mysql did not respond to the request and there is nothing to work with.
The to open a database to work with you need to do the following:
mysql_select_db("dbname");
details: this tells MySQL to open "dbname" to work within. The following is what is recommended to write for this line of code:
mysql_select_db("dbname") or die("Error: Could not open database");
details: this tells PHP if the database could not be open then kill the script with the text "Error: Could not open database"
To write informations not existing into the database you need to do the following:
mysql_query("INSERT INTO tablenamehere VALUES( 'val1', 'val2', ETC.)");
details: This inserts values in order to MySQL. You can add only same amount of values to the database has their are existing columns. to skip a column you just add blank wich is ''
To update a row in the database you need to do the following:
mysql_query("UPDATE tablenamehere SET valname = 'val1', valname2 = 'val2', etc.");
To read how many rows is there in your database that are matching with something you need to do the following:
$query = mysql_query("SELECT * FROM tablenamehere WHERE valname = 'something'");
$all_records = mysql_num_rows($query);
details: this gets all values that match 'something' in 'tablenamehere'. * means everything you can change that with a specific column
$all_records will now hold the number of rows matching with your query.
To get each column into a variable to be able to print it to the user you need to do the following:
$query = mysql_query("SELECT * FROM tablenamehere WHERE valname = 'something'");
$row = mysql_fetch_array($query);
echo $row["valname1"];
The following is required after all your pages that uses mysql:
mysql_close($link);
This should be enough to help you start out with mysql. Comment and Rate :)

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.