MySQL

create_table_example

<?php

//create table : table name: example

// Make a MySQL Connection
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

// Create a MySQL table in the selected database
mysql_query(“CREATE TABLE example(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
age INT)”)
or die(mysql_error());

echo “Table Created!”;

?>

 

insert data into example

<?php
//insert data into example table

// Make a MySQL Connection
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

// Insert a row of information into the table “example”
mysql_query(“INSERT INTO example
(name, age) VALUES(‘Timmy Mellowman’, ’23’ ) “)
or die(mysql_error());

mysql_query(“INSERT INTO example
(name, age) VALUES(‘Sandy Smith’, ’21’ ) “)
or die(mysql_error());

mysql_query(“INSERT INTO example
(name, age) VALUES(‘Bobby Wallace’, ’15’ ) “)
or die(mysql_error());

echo “Data Inserted!”;

?>

 

mysql1

<?php
// php and mysql code

// Make a MySQL Connection
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

// Get a specific result from the “example” table
$result = mysql_query(“SELECT * FROM example
WHERE name=’Sandy Smith'”) or die(mysql_error());

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array($result,MYSQL_NUM );
// Print out the contents of each row into a table
echo $row[1].” – “.$row[2];
?>

mysql2

<?php

//using wild card

// Connect to MySQL
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

// Insert a row of information into the table “example”
$result = mysql_query(“SELECT * FROM example WHERE age LIKE ‘2%’ “)
or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result,MYSQL_BOTH )) {
// Print out the contents of each row
echo $row[‘name’].” – “.$row[‘age’]. “<br />”;
echo $row[1].” – “.$row[2]. “<br />”;

}
?>

mysql3

<?php
// Make a MySQL Connection
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

// Get all the data from the “example” table
$result = mysql_query(“SELECT * FROM example order by age “)
or die(mysql_error());

echo “<table border=’1′ bordercolor=\”#00CCCC\”>”;
echo “<tr> <th>ID</th><th>Name</th> <th>Age</th> </tr>”;
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_object( $result )) {
// Print out the contents of each row into a table
echo “<tr><td>”;
echo $row->id;
echo “</td><td>”;
echo $row->name;
echo “</td><td>”;
echo $row->age;
echo “</td></tr>”;
}

echo “</table>”;
?>

update example

<?php

//update example table
// Connect to MySQL
mysql_connect(“localhost”, “root”, “root”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());
// Get Sandy’s record from the “example” table
$result = mysql_query(“UPDATE example SET age=’22’ WHERE age=’23′”)
or die(mysql_error());
$result = mysql_query(“SELECT * FROM example WHERE age=’22′”)
or die(mysql_error());

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row[‘name’].” – “.$row[‘age’]. “<br />”;
?>

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.