PHP ADODB Tutorial with HTML
From w3cyberlearnings
Contents |
Create Table and Insert Sample Record
We create a people table and insert sample records!
mysql> CREATE TABLE People -> (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, -> name VARCHAR(100) NOT NULL, -> age INT); Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO People VALUES(null,'Jingk',29), -> (null,'Dara',21), -> (null,'John',21), -> (null,'Yin',21), -> (null,'Sopaul',29), -> (null,'CHris',28), -> (null,'Moore',28), -> (null,'Jake',28), -> (null,'Kim',31), -> (null,'King',24); Query OK, 10 rows affected (0.00 sec) Records: 10 Duplicates: 0 Warnings: 0 mysql>
Generate Pagination using ADODB_Pager
- Require to include the adodb-pager.inc.php
- Require to include the adodb.inc.php
<?php require_once ('adodb.inc.php'); require_once ('adodb-pager.inc.php'); $host = 'localhost'; $user = 'root'; $pass = 'caojiang'; $dbname = 'mytestdatabase'; $conn1 = &ADONewConnection('mysql'); $conn1->PConnect($host, $user, $pass, $dbname); $sql = "SELECT id,name,age FROM People"; $pager = new ADODB_Pager($conn1,$sql); $pager->Render($rows_per_page=2); $pager->close(); $conn1->close(); ?>
Generate Menu using GetMenu
require_once ('adodb.inc.php'); $host = 'localhost'; $user = 'root'; $pass = 'caojiang'; $dbname = 'mytestdatabase'; $conn1 = &ADONewConnection('mysql'); $conn1->PConnect($host, $user, $pass, $dbname); $sql = "SELECT name,id FROM People"; $rs = $conn1->Execute($sql); echo $rs->GetMenu('sel_people','Yin'); $rs->close(); $conn1->close(); ?>
Return Result
<select name="sel_people"> <option></option> <option value="1">Jingk</option> <option value="2">Dara</option> <option value="3">John</option> <option selected="selected" value="4">Yin</option> <option value="5">Sopaul</option> <option value="6">CHris</option> <option value="7">Moore</option> <option value="8">Jake</option> <option value="9">Kim</option> <option value="10">King</option> </select>
Generate HTML Table using tohtml.inc.php
require_once ('adodb5/adodb.inc.php'); require_once ('adodb5/tohtml.inc.php'); $host = 'localhost'; $user = 'root'; $pass = 'caojiang'; $dbname = 'mytestdatabase'; $conn1 = &ADONewConnection('mysql'); $conn1->PConnect($host, $user, $pass, $dbname); $sql = "SELECT id,name,age FROM People"; $rs = $conn1->Execute($sql); rs2html($rs,'border=2 cellpadding=3',array('id','name','age')); $rs->close(); $conn1->close(); ?>
Return Result
<table cols="3" border="2" cellpadding="3"><tr> <th>id</th><th>name</th><th>age</th> </tr> <tr valign="top"> <td align="right">1</td> <td>Jingk</td> <td align="right">29</td> </tr> <tr valign="top"> <td align="right">2</td> <td>Dara</td> <td align="right">21</td> </tr> <tr valign="top"> <td align="right">3</td> <td>John</td> <td align="right">21</td> </tr> <tr valign="top"> <td align="right">4</td> <td>Yin</td> <td align="right">21</td> </tr> <tr valign="top"> <td align="right">5</td> <td>Sopaul</td> <td align="right">29</td> </tr> <tr valign="top"> <td align="right">6</td> <td>CHris</td> <td align="right">28</td> </tr> <tr valign="top"> <td align="right">7</td> <td>Moore</td> <td align="right">28</td> </tr> <tr valign="top"> <td align="right">8</td> <td>Jake</td> <td align="right">28</td> </tr> <tr valign="top"> <td align="right">9</td> <td>Kim</td> <td align="right">31</td> </tr> <tr valign="top"> <td align="right">10</td> <td>King</td> <td align="right">24</td> </tr> </table>
Related Links
- PHP ADODB Export in CSV
- PHP ADODB Delete Record
- PHP ADODB Tutorial with HTML