iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 13, 2008
Rainbow Links
EarthWeb.com
 
May 13, 2008
MySpace Profile Page Resources
HTML Goodies
 
May 13, 2008
How to Upload Your Photos onto the Web
HTML Goodies
 
May 13, 2008
Email Marketing for MySpace Artists
HTML Goodies
 
May 13, 2008
Top Online Marketing Techniques
HTML Goodies
 
May 13, 2008
I want to create a site just like ____, is that a violation of...
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Database-Related

PHP & MySQL basics 

  Views:    15517
  Votes:    2
by John Doe 11/26/03 Rating: 

Synopsis:

This article will guide new PHP developers to create simple scripts interacting with a MySQL database.
Pages: firstback1 3 forwardlast
The Article

Lesson 1: Connecting to MySQL

Before you can make any actions in MySQL, you will have to connect into it. You Web Server may give you the necessary information for this.
Let"s see a simple example:

<?php
// MySQL info variables
$mysql["host"] = "localhost";
$mysql["user"] = "root";
$mysql["pass"] = "pass";
$mysql["database"] = "devpapers";

// Connect to MySQL
mysql_connect($mysql["host"], $mysql["user"], $mysql["pass"]) or die("MySQL connection failed. Some info is wrong");

// Select database
mysql_select_db($mysql["database"]) or die("Could not connect to DataBase \"$mysql["database"]\"");
?>



if you do everything right, nothing will appear, and now you can start using MySQL!


Lesson 2: Creating a MySQL table
For content storage, you must have a table in your DataBase. The code bellow will show how to create a simple table. 

 <?php
// Define table fields and types
$query = "CREATE TABLE `test1` ( `id` INT( 6 ) NOT NULL AUTO_INCREMENT ,";
$query .= " `name` VARCHAR( 40 ) NOT NULL ,";
$query .= " `email` VARCHAR( 80 ) NOT NULL ,";
$query .= " `info` TEXT NOT NULL ,";
$query .= " PRIMARY KEY ( `id` ) );";

// Create the table
mysql_query($query) or die("Could not create the table");
echo "Table created!";
?>


This must create our examples table. See more about the "CREATE TABLE" syntax at
http://www.mysql.com/doc/en/CREATE_TABLE.html

Lesson 3: Inserting content into the table
The main objective of a DataBase is storage content, so, lets insert it.

<?php
// Inserting query
$query = "INSERT INTO test1 (id, name, email, info) VALUES ("", "Andreas",
"dreazdesign@hotmail.com", "PHP and MySQL programmer")";

// Run the query
mysql_query($query) or die("Could not insert content into table");
echo "Content was sucessfully insert";
?>


Lesson 4: Showing a table content
Supposing that you are using the table test1 (lesson3), content showing will look like this:

<?php
// Get the MySQL connection file (lesson 1)
require "connection.php";

// Call the table test1
$query = "SELECT name, email, info FROM test1";

// Execute the query
$query = mysql_query($query) or die("error in the query \$query");

// Show what we got
while($info = mysql_fetch_array($query)) {
    echo "User Name: ".$info["name"]."<br>";
    echo "User e-mail: ".$info["email"]."<br>";
    echo "User Info: ".$info["info"]."<br>";
    echo "<br>";
}
?>


With this you may have every table content printed in the screen.

Now you have your personal content storage! In the next page you will learn some other queries syntax, which will prove to you that DataBase is very useful.

Pages: firstback1 3 forwardlast

Similar/related articles:


 
  Sponsors