Database Connectivity in Wordpress,Wordpress database tutorial

Wordpress is complete dynamic CMS of PHP,Wordpress provide database connectity when we install wordpress CMS then we can not say wordpress has database connectivity manually but it is mandatory in installation time.

Wordpress CMS provide table also WP_POST is the most impotent table where we can store records .

Wordpress page and post both store record using this.


Code or database operation.

Wordpress provide global $wpdb for database operation

Step  for database

Database operation in wordpress:-

1)  create database  

 2) create table  as per requirement

 3) create page template to create form

 4)  global $wpdb

Syntax for data selection:-

$wpdb->get_results( "SELECT id, name FROM mytable" );

Syntax for data insertion:-

$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
);

Syntax for data updation:-


$wpdb->update( 
    'table', 
    array( 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    ), 
    array( 'ID' => 1 )
);

Syntax for data deletion:-

$wpdb->delete( 'table', array( 'ID' => 1 ) );

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Complete code of database 
create table in wordpress exiting database

create table wp_student(rno varchar(50),sname varchar(50),branch varchar(50),sem varchar(50))

Code page template:-
<?php
/*
Template name:   Student Template
*/
get_header();
?>
<section>
<div style="margin-left: 50px;margin-top: 50px;float:left;">
<form action="" method="post">
  
     <input type="text" name="txtrno" placeholder="Enter rno" />
     <br>
     <br>
     <input type="text" name="txtsname" placeholder="Enter name" />
      <br>
      <br>
       <input type="text" name="txtbranch" placeholder="Enter branch" />
<br>
<br>
 <input type="text" name="txtsem" placeholder="Enter sem" />
 <br>
 <br>
      <input type="submit" name="btnsubmit" value="Submit" />

 </form>

 <?php
     global $wpdb;
     if(isset($_REQUEST['btnsubmit']))
     {
        $rno=$_REQUEST['txtrno'];
        $sname=$_REQUEST['txtsname'];
        $branch=$_REQUEST['txtbranch'];
        $sem=$_REQUEST['txtsem'];
        $res= $wpdb->insert('wp_student', 
        array( 'rno' => $rno, 
        'sname' =>$sname,
        'branch'=>$branch,
        'sem' =>$sem));
      //  $conn= mysqli_connect('localhost','root','','scsdb');
        // mysqli_query($conn,"INSERT INTO `wp_student` (`rno`, `sname`, `branch`, `sem`) VALUES ('g', 's', 's', 's')");


     }


 ?>

</div>
<div style="margin-left: 40px;margin-top: 50px;float:left;">
<p>View Student Record Here</p>
<table border="1">
<tr><th>RNO</th><th>Sname</th><th>Branch</th><th>Sem</th></tr>
  <?php

  $res = $wpdb->get_results( "SELECT rno,sname,branch,sem FROM wp_student");

   foreach ($res as $x) {
    echo "<tr><td>".$x->rno,"</td><td>".$x->sname."</td><td>".$x->branch."</td><td>".$x->sem."</td></tr>";
   }
    
  ?>
</table>
</div>
</section>
<?php
get_footer();
?> 











Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post