Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: php-mysql

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: php-mysql
Showing posts with label php-mysql. Show all posts
Showing posts with label php-mysql. Show all posts

PHP Mysql Tutorial



PHP MySql Tutorial





In this tutorial i will share how to use mysql and php.

Lets get start.

For Cpanel
Open Mysql Database And Create A New Database And Create a New Username and Password then set the username to Database And Give all privilages.
Now Open Your Phpmyadmin from cpanel then select database and create a new table which we use to store data.

In this tutorial i will create a table name login
And Columns are
id - int - 255 - Auto increment - primary key
name - text
password - text
email - text

For xampp:

Open http://localhost/phpmyadmin
And Create a new database and create the table same as which we created in for server.

Now Lets start How to insert,select,update,delete data from database.

Create Database Connection in PHP

Filename: connect.php

<?php
$server=”localhost”;
$username=”root”;
$password=”root”;
$database=”demo”;
$con=mysql_connect($server,$username,$password);

//this function is user to connect our server mysql it takes three parameters first is your server ,second is username and third parameter is password.

$con2=mysql_select_db($database,$con);

//this function is used to select our database. it takes two parameter first parameter is database name and second is the connection which we create to connect our server.
?>



Insert Operation

<?php
include ‘connect.php’;
//inclufe function is used to to include one file into other in php

$name=”sswebtricks”;
$email=”sswebtricks@gmail.com”;
$password=”pass@123”;

$qr=mysql_query(“INSERT INTO login (name,email,password) VALUES (‘$name’,’$email’,’$password’));

//mysql_query is function to write our query it takes one parameter which is our query it returns boolean type value true or false

If($qr)
{
echo “insert sucess”;
}
else
{
echo “failed”;
}
?>


Select Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“select * from login”);
$row=mysql_fetch_array($qr);

//mysql fetch array function is used to fetch fetch all the data from.database it takes one parameter which is our query and return array value .Here we only fetch single row .

echo $row[‘email’].”<br>”;
//print the email of name

//Now we fetch multiple rows
while($row2=mysql_fetch_array($qr))
{
  echo $row2[‘email’].”<br>”;
}

?>


Update Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;
$newemail=”sksanjeev362@gmail.com”;

$qr=mysql_query(“UPDATE login SET email='$newemail' WHERE name=’$name’");

// Above is updAte query

if(mysql_affected_rows>0)
{
echo “updated”;
}
else
{
echo “failed”;
}


//mysql affected rows check how many rows will affected with our query
?>

Delete Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“delete from login where name=’$name’”);

if($qr)
{
echo “ deleted”;
}
else
{
echo “failed”;
}
?>


8