In php print is used to display or output a string
print "Hello World"; // string are written inside double quotes
Php connect to mysql database
Create a new page and save as conn.php and paste the below stated code
<?php
function conectDB() {
$host = "localhost";
$username = "root";
$password = "";
$db = "Write your database name"; // Write your database name here
$connect = mysql_connect($host,$username,$password);
if(!$connect){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Write your database name") or die('Could not connect: ');
}
?>
Then we can include the conn.php on every pages that connects to mysql database
<?php
include "conn.php";
$dbHandle = conectDB();
?>
<?php
function conectDB() {
$host = "localhost";
$username = "root";
$password = "";
$db = "Write your database name"; // Write your database name here
$connect = mysql_connect($host,$username,$password);
if(!$connect){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Write your database name") or die('Could not connect: ');
}
?>
Then we can include the conn.php on every pages that connects to mysql database
<?php
include "conn.php";
$dbHandle = conectDB();
?>
Create database with php mysql
<?php
mysql_query('
CREATE TABLE IF NOT EXISTS `table_sample` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 75 ) NOT NULL,
`email` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;');
?>
mysql_query('
CREATE TABLE IF NOT EXISTS `table_sample` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 75 ) NOT NULL,
`email` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;');
?>
Insert Data Into a Database - php mysql tutorials
Simple coding for insert data into adatabase in php and mysql.
include "conn.php";
$fieldname1 = $_REQUEST['fieldname1"];
$fieldname2 = $_REQUEST['fieldname2'];
$fieldname3 = $_REQUEST['fieldname3'];
$query = sprintf("INSERT INTO databasename (fieldname1, fieldname2, fieldname3)
VALUES('%s', '%s', '%s')",
mysql_real_escape_string($fieldname1),
mysql_real_escape_string($fieldname2),
mysql_real_escape_string($fieldname3));
$result = mysql_query ($query, $connect);
include "conn.php";
$fieldname1 = $_REQUEST['fieldname1"];
$fieldname2 = $_REQUEST['fieldname2'];
$fieldname3 = $_REQUEST['fieldname3'];
$query = sprintf("INSERT INTO databasename (fieldname1, fieldname2, fieldname3)
VALUES('%s', '%s', '%s')",
mysql_real_escape_string($fieldname1),
mysql_real_escape_string($fieldname2),
mysql_real_escape_string($fieldname3));
$result = mysql_query ($query, $connect);
Update Data In a Database - php mysql tutorials
The simple examples of how to update Data in a Database is
<?php
$Host = "localhost";
$User = "root";
$Pass = "";
$Name = "your databaseName";
$con = mysql_connect($Host, $User, $Pass);
$db = mysql_select_db($database_name, $con);
$sql = "UPDATE `table_name` SET `name` = 'Youname', `email` = 'emailid' WHERE `id` = '2';
$result = mysql_query($sql);
?>
<?php
$Host = "localhost";
$User = "root";
$Pass = "";
$Name = "your databaseName";
$con = mysql_connect($Host, $User, $Pass);
$db = mysql_select_db($database_name, $con);
$sql = "UPDATE `table_name` SET `name` = 'Youname', `email` = 'emailid' WHERE `id` = '2';
$result = mysql_query($sql);
?>
Delete Data from Database - php mysql tutorials
The simple examples of how to delete data from Database is
<?php
$Host = "localhost";
$User = "root";
$Pass = "";
$Name = "your databaseName";
$con = mysql_connect($Host, $User, $Pass);
$db = mysql_select_db($Name, $con);
$sql = "DELETE FROM `table_name` WHERE `id` = '2' ";
//you can use this
//$sql = "DELETE FROM `table_name` WHERE `name` = 'Yourname'";
$result = mysql_query($sql);
?>
<?php
$Host = "localhost";
$User = "root";
$Pass = "";
$Name = "your databaseName";
$con = mysql_connect($Host, $User, $Pass);
$db = mysql_select_db($Name, $con);
$sql = "DELETE FROM `table_name` WHERE `id` = '2' ";
//you can use this
//$sql = "DELETE FROM `table_name` WHERE `name` = 'Yourname'";
$result = mysql_query($sql);
?>
Php mysql order by clause
The ORDER BY clause is used to sort the data in ascending order(by default) or in descending order.
To sort the records in a ascending order, you can use the ASC keyword.
To sort the records in a descending order, you can use the DESC keyword.
Syntax
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC
To sort the records in a ascending order, you can use the ASC keyword.
To sort the records in a descending order, you can use the DESC keyword.
Syntax
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC
Subscribe to:
Posts (Atom)