Return to Snippet

Revision: 35967
at November 15, 2010 18:29 by lorenz82


Initial Code
//Connection
$con = mysql_connect("localhost","root","password");
mysql_select_db("my_db", $con);

//Creat DB
mysql_query("CREATE DATABASE my_db",$con)

//Create table
$sql = "CREATE TABLE Persons
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
FirstName varchar(15) NOT NULL,
LastName varchar(15) NOT NULL,
Age int,
PRIMARY KEY (id)
)";
mysql_query($sql,$con);

//Insert row in table
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
$insertedId=mysql_insert_id();

//Update row
mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");

//Delete row
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");

//Select one row
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['LastName'];

//Select more rows
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
while( $row = mysql_fetch_array($result) ){
	echo $row['LastName'];
}

//Select more rows order by Age discending from the rows from 0 to 10
$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter' ORDER BY Age DESC LIMIT 0,10") or die(mysql_error());
while( $row = mysql_fetch_array($result) ){
	echo $row['LastName'];
}

Initial URL

                                

Initial Description

                                

Initial Title
MySQL Important query

Initial Tags
mysql, php

Initial Language
PHP