Free download web templates, books and more. Free php projects ,games and apps. GTA mods and more

Breaking

Showing posts with label php tutorial. Show all posts
Showing posts with label php tutorial. Show all posts
February 15, 2019

php mysql select record and display

PHP MySQL Select Row In Database

the select query is display the records in database table.
PHP mysql_query() function is used to execute select query. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.
  • mysqli_query()
There are two other MySQLi functions used in select query.
  • mysqli_num_rows(mysqli_result $result): returns number of rows.
  • mysqli_fetch_assoc(mysqli_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It return NULL if there are no more rows.

PHP MySQLi Select Query Example

Example
  1. <?php  
  2. $host = 'localhost';  
  3. $user = 'root';  
  4. $pass = '';  
  5. $dbname = 'test';  
  6. $conn = mysqli_connect($host$user$pass,$dbname);  
  7. if(!$conn){  
  8.   die('Could not connect: '.mysqli_connect_error());  
  9. }  
  10. echo 'Connected successfully<br/>';  
  11.   
  12. $sql = 'SELECT * FROM staff';  
  13. $retval=mysqli_query($conn$sql);  
  14.   
  15. if(mysqli_num_rows($retval) > 0){  
  16.  while($row = mysqli_fetch_assoc($retval)){  
  17.     echo "staff id :{$row['id']}  <br> ".  
  18.          "staff name : {$row['name']} <br> ".  
  19.          "staff salary : {$row['salary']} <br> ".  ;
  20.  } //end of while  
  21. }else{  
  22. echo "0 results";  
  23. }  
  24. mysqli_close($conn);  
  25. ?>  
Output:
Connected successfully
staff id :1 
staff name : ratan 
staff salary : 9000 
staff id :2 
staff name : karan 
staff salary : 40000 
staff id :3 
staff name : jai 
staff salary : 90000 
February 15, 2019

php mysqli delete data in database

PHP MySQL Delete Data (or) Record

the delete query is delete user defined recored delete.
in the database table.
PHP mysql_query() function is used to delete record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.
  • mysqli_query()
  • PDO::__query()

PHP MySQLi Delete Record Example

Example
  1. <?php  
  2. $host = 'localhost';  
  3. $user = 'root';  
  4. $pass = '';  
  5. $dbname = 'test';  
  6.   
  7. $conn = mysqli_connect($host$user$pass,$dbname);  
  8. if(!$conn){  
  9.   die('Could not connect: '.mysqli_connect_error());  
  10. }  
  11. echo 'Connected successfully<br/>';  
  12.   
  13. $id=2;  
  14. $sql = "delete from staff where id=$id";  
  15. if(mysqli_query($conn$sql)){  
  16.  echo "Record deleted successfully";  
  17. }else{  
  18. echo "Could not deleted record: ". mysqli_error($conn);  
  19. }  
  20.   
  21. mysqli_close($conn);  
  22. ?>  
Output:
Connected successfully
Record deleted successfully
February 15, 2019

php mysqli update data in database

PHP MySQL Update Data (or) Record

PHP mysql_query() function is used to update record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.
  • mysqli_query()
  • PDO::__query()

PHP MySQLi Update Record Example

Example
  1. <?php  
  2. $host = 'localhost';  
  3. $user = 'root';  
  4. $pass = '';  
  5. $dbname = 'test';  
  6.   
  7. $conn = mysqli_connect($host$user$pass,$dbname);  
  8. if(!$conn){  
  9.   die('Could not connect: '.mysqli_connect_error());  
  10. }  
  11. echo 'Connected successfully<br/>';  
  12.   
  13. $id=4;  
  14. $name="sam";  
  15. $salary=80000;  
  16. $sql = "update staff set name=\"$name\", salary=$salary where id=$id";  
  17. if(mysqli_query($conn$sql)){  
  18.  echo "Record updated successfully";  
  19. }else{  
  20. echo "Could not update record: ". mysqli_error($conn);  
  21. }  
  22.   
  23. mysqli_close($conn);  
  24. ?>  
Output:
Connected successfully
Record updated successfully
February 15, 2019

php mysqli contact form example || php mysql contact form source code

php mysqli contact form example:

this contact form example by putting this code into add_employee.php, this will take input using HTML Form and then it will create records into database.
<html>
   
   <head>
      <title>Add New Record in MySQL Database</title>
   </head>
   



   <body>
      <?php
         if(isset($_POST['add'])) {
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = '';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            
            if(! $conn ) {
               die('Could not connect: ' . mysql_error());
            }
            
            if(! get_magic_quotes_gpc() ) {
               $name = addslashes ($_POST['name']);
               $address = addslashes ($_POST['address']);
            }else {
               $name = $_POST['name'];
               $address = $_POST['address'];
            }
            
            $phone_number = $_POST['phone_number'];
            
            $sql = "INSERT INTO contact ". "(name,address, phone_number, 
               join_date) ". "VALUES('$name','$address',$phone_number, NOW())";
               
            mysql_select_db('test_db');
            $retval = mysql_query( $sql, $conn );
            
            if(! $retval ) {
               die('Could not enter data: ' . mysql_error());
            }
            
            echo "Entered data successfully\n";
            
            mysql_close($conn);
         }else {
            ?>
            
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border = "0" cellspacing = "1" 
                     cellpadding = "2">
                  
                     <tr>
                        <td width = "100"> User Name</td>
                        <td><input name = "name" type = "text" 
                           id = "emp_name"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Address</td>
                        <td><input name = "address" type = "text" 
                           id = "address"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Mobile Number</td>
                        <td><input name = "phone_number" type = "text" 
                           id = "phone_number"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "add" type = "submit" id = "add" 
                              value = "Submit">
                        </td>
                     </tr>
                  
                  </table>
               </form>
            
            <?php
         }
      ?>
   
   </body>
</html>
February 15, 2019

php mysql insert data into database || how to insert data in php mysql

php mysql insert data in database

user insert data in mysql query way to insert data in database.
Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query. Below a simple example to insert a record into employee table.

Example

Try out following example to insert record into employee table.
<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'INSERT INTO student '.
      '(stu_name,stu_address, stu_mark, join_date) '.
      'VALUES ( "sam", "india", 99, NOW() )';
      
   mysql_select_db('test_db');
   $retval = mysql_query( $sql, $conn );
   
   if(! $retval ) {
      die('Could not enter data: ' . mysql_error());
   }
   
   echo "Entered data successfully\n";
   
   mysql_close($conn);
?>
In real application, all the values will be taken using HTML form and then those values will be captured using PHP script and finally they will be inserted into MySQL tables.
the Function insert data's in database.
February 15, 2019

php mysql create table || how to Create php mysql table

MySQL Create Table

PHP mysql_query() function is used to create table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.
  • mysqli_query()

PHP MySQLi Create Table Example

Example
  1. <?php  
  2. $host = 'localhost';  
  3. $user = '';  
  4. $pass = '';  
  5. $dbname = 'test';  
  6.   
  7. $conn = mysqli_connect($host$user$pass,$dbname);  
  8. if(!$conn){  
  9.   die('Could not connect: '.mysqli_connect_error());  
  10. }  
  11. echo 'Connected successfully<br/>';  
  12.   
  13. $sql = "create table student(id INT AUTO_INCREMENT,name VARCHAR(20) NOT NULL,  
  14. emp_salary INT NOT NULL,primary key (id))";  
  15. if(mysqli_query($conn$sql)){  
  16.  echo "Table student created successfully";  
  17. }else{  
  18. echo "Could not create table: ". mysqli_error($conn);  
  19. }  
  20.   
  21. mysqli_close($conn);  
  22. ?>  
Output:
Connected successfully
Table student created successfully