[PHP Example] Using MySQL w/PHP
NEW TUTORIAL UNDER CONSTRUCTION
Here i am going to show you (quickly) how to use MySQL in PHP.
If you are not familiar with MySQL, then take a look here
Connecting to MySQL Server
The following is generic code to connecting to a MySQL Server. Typically you will be using your local mysql server on your computer / web server
PHP Code:
<?php
mysql_connect("localhost", "USERNAME", "PASSWORD") or die("could not connect to MySQL server");
?>
Selecting a MySQL Database
Before you can make MySQL queries, you need to select a database that has the information you need. This typically comes after you connect to a database
PHP Code:
<?php
mysql_select_db("databasename") or die("could not select MySQL database");
?>
Preforming a Query (Update/Insert/Delete/Truncate/Num_Rows)
A query is a message sent to the sql server either asking for information, or telling it to input or change information. This example shows how to Update/Insert/Delete/Truncate from database
PHP Code:
<?php
$sql = "YOUR SQL HERE"; // REFER TO THE MYSQL HELP LINK IN THIS POST AND MY SIGNATURE
$query = mysql_query($sql);
// THAT WILL PREFORM YOUR QUERY. IF YOU DID A NUM_ROWS QUERY:
$numrows = mysql_num_rows($query);
// then you can use $numrows in an IF statement
?>
Preforming a Query (Select)
Select query will be the most used query (for most people). It allows you to select rows from a table with many option for selecting the correct row. I will show 2 examples. 1) Selecting just one row; 2) selecting an ARRAY of rows.
PHP Code:
<?php
$sql = "SELECT * FROM `members` WHERE memberID='1' LIMIT 1";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
//USAGE:
//echo $result['COLUMN'];
echo $result['memberID'];
echo $result['name'];
echo $result['email'];
// etc...
?>
More coming soon!
Re: [PHP Example] Using MySQL w/PHP
Re: [PHP Example] Using MySQL w/PHP
you're welcome! and thank you :)