All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

MySQL Querying Database

You can query a mysql table like insert data, delete data, update data, select data.



Insert Data Into MySQL Table

<?php

$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);

mysql_select_db( 'Demodata' );

$sql = "INSERT INTO student_info values( "Rahul" ,"BCA" )";

$insert = mysql_query( $sql );

mysql_close($conn);

?>

Code Explanation

  • It can insert the data in student_info table of Demodata database and it.
  • It insert Rahul in student_name column and BCA in course column.



Select Data From MySQL Table

We use PHP mysql_fetch_array() function to get the rows from database you can also use mysql_fetch_assoc() to get results. This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows.


<?php

$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);

mysql_select_db( 'Demodata' );

$sql = "SELECT student_name, course from student_info";

$select = mysql_query( $sql );

while($row=mysql_fetch_array($select))
{

echo "Name:".$row['student_name'];
echo "Course:".$row['course'];

}

mysql_close($conn);

?>

Code Explanation

  • It can select the data from student_info table of Demodata database and it.
  • In this query we mention the name of columns from which we want to fetch data you also use * in place of column names that means you want to fetch data from all columns.
  • This query returns only one row because only one row is present in that table.
  • It displays Rahul in student_name and BCA in course.



Update Data In MySQL Table


<?php

$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);

mysql_select_db( 'Demodata' );

$sql = "UPDATE student_info SET student_name = 'Rohit' ";

$select = mysql_query( $sql );

mysql_close($conn);

?>

Code Explanation

  • It can change all the names on student_name column present in student_info table to Rohit.



Delete Data From MySQL Table


<?php

$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);

mysql_select_db( 'Demodata' );

$sql = "DELETE from student_info";

$select = mysql_query( $sql );

mysql_close($conn);

?>

Code Explanation

  • It can delete all the data present on student_info table.
❮ PrevNext ❯