Blog Details

PHP Database


                                
                                    <h2>What is MySQL ?</h2>
<ul>
	<li>MySQL is a<strong> database system</strong> used on the web</li>
	<li>MySQL is a database system that <strong>runs on a server</strong></li>
	<li>MySQL is ideal for both <strong>small and large</strong> applications</li>
	<li>MySQL is <strong>very fast, reliable,</strong> and easy to use</li>
	<li>MySQL supports standard SQL</li>
	<li>MySQL compiles on a <strong>number of platforms</strong></li>
	<li>MySQL is <strong>free</strong> to download and use</li>
</ul>
&nbsp;
<h2>Open a Connection to the MySQL Server :-</h2>
<h3>Syntax :-</h3>
<strong>mysqli_connect(host,username,password,dbname);</strong>
<h3>Example  :-</h3>
&lt;?php
// Create connection
$con=<strong>mysqli_connect("example.com","peter","abc123","my_db");</strong>

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?&gt;
<h2>Close a Connection :-</h2>
&lt;?php
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

<strong>mysqli_close($con);</strong>
?&gt;
<h2>* All operation on Database :-</h2>
<h2>1)  Create a Database :-</h2>
&lt;?php
$con=<strong>mysqli_connect("example.com","peter","abc123","my_db");</strong>
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Create table
$sql=<strong>"CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";</strong>

// Execute query
if (mysqli_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?&gt;
<h2>2) Insert Data Into a Database Table :-</h2>
<h3>Syntax :-</h3>
&nbsp;

<strong>INSERT INTO table_name (column1, column2, column3,...)</strong>
<strong> VALUES (value1, value2, value3,...)</strong>
<h3>Example :-</h3>
&nbsp;
<h2>1) HTML form:</h2>
&lt;html&gt;
&lt;body&gt;

&lt;form action="insert.php" method="post"&gt;
Firstname: &lt;input type="text" name="firstname"&gt;
Lastname: &lt;input type="text" name="lastname"&gt;
Age: &lt;input type="text" name="age"&gt;
&lt;input type="submit"&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
<h2>2) <strong>insert.php :</strong></h2>
&lt;?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql=<strong>"INSERT INTO Persons (FirstName, LastName, Age)</strong>
<strong> VALUES</strong>
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?&gt;
<h2>3) Select Data From a Database Table :-</h2>
<h3>Syntax :-</h3>
&nbsp;

<strong>SELECT column_name(s)</strong>
<strong> FROM table_name</strong>
<h3>Example :-</h3>
&nbsp;

&lt;?php
$con=<strong>mysqli_connect("example.com","peter","abc123","my_db");</strong>
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result =<strong> mysqli_query($con,"SELECT * FROM Persons");</strong>

echo "&lt;table border='1'&gt;
&lt;tr&gt;
&lt;th&gt;Firstname&lt;/th&gt;
&lt;th&gt;Lastname&lt;/th&gt;
&lt;/tr&gt;";

while($row =<strong> mysqli_fetch_array($result))</strong>
{
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $row['LastName'] . "&lt;/td&gt;";
echo "&lt;/tr&gt;";
}
echo "&lt;/table&gt;";

mysqli_close($con);
?&gt;
<h2>4) Update Data In a Database :-</h2>
<h3>Syntax :-</h3>
&nbsp;

<strong>UPDATE table_name</strong>
<strong> SET column1=value, column2=value2,...</strong>
<strong> WHERE some_column=some_value</strong>
<h3>Example :-</h3>
&nbsp;
<div>&lt;?php
$con=<strong>mysqli_connect("example.com","peter","abc123","my_db");</strong>
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}<strong>mysqli_query($con,"UPDATE Persons SET Age=36</strong>
<strong> WHERE FirstName='Peter' AND LastName='Griffin'");</strong></div>
<div>mysqli_close($con);
?&gt;</div>
<div></div>
<div></div>
<div></div>
<div>
<h2>5) Delete Data In a Database :-</h2>
<h3>Syntax :-</h3>
&nbsp;

<strong>DELETE FROM table_name</strong>
<strong> WHERE some_column = some_value</strong>
<h3>Example :-</h3>
&nbsp;

&lt;?php
$con=<strong>mysqli_connect("example.com","peter","abc123","my_db");</strong>
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

<strong>mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");</strong>

mysqli_close($con);
?&gt;

</div>
                                    


Categories

Core_PHP

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote