Monday, 13 January 2014

MYSQL PHP DATABASE CONNECTION

Hi guyz,

The most useful code snippet in present day web application development is MYSQL PLUS PHP CONNECTION.

for this we require the basic three modules:

Mysql - database
PHP  - Backend scripting language
XAMPP - Server

for developing any web application we require to deal with database hence for maintaining database and for manipulating database we require a server and a specific db language.

here im specifying some MYSQL Commands used for DB CONNECTION

Connecting php with server

      mysql_connect();

Connecting php with database

   mysql_select_db();

For writing any php mysql query we use

  mysql_query();

ex:   mysql_query("SELECT * FROM table_name WHERE col='$data' ");

For fetching data from this query

ex: mysql_fetch_array($query);

For counting number of rows

ex:  mysql_num_rows($query);

Php mysql tester:

An example programm - for database connection:

Programm name : db_connection.php

<?php
$hostname = ''localhost';
$username = 'root';   //for local server
$password  = '';     // empty for local server
$database = 'example_db';
mysql_connect($hostname,$username,$password);
mysql_select_db($database);
mysql_close();
?>

Example programm - 2

Program name : check_sql.php
<html>
 <head> <title>   checking the database </title> </head>
<body>
<?php

include('db_connection.php');  // including the db connection

?>
<center>
<h2> printing all the values from the datase table </h2>

<br><br>

<?php

$k = mysql_query("SELECT * FROM users ORDER BY u_id DESC");
if($mysql_num_rows($k)>0) {        //    number of rows

while($kk = mysql_fetch_array($k))    // fetching array
{
echo $name = $kk['name'];        //  printing the names from name coloumn in table
echo  $id_no = $kk['id_num'];      //  printing id numbers from id_number  coloumn in table
}

}

?>

</br>

<h2> All the values are printed </h2>

















Share this article

0 comments:

Post a Comment

 
Copyright © 2014 krishcdbry • All Rights Reserved.
Distributed By Free Blogger Templates | Template Design by BTDesigner • Powered by Blogger
back to top