Main Menu

HOME

.Net
ASP
Assembly
C
C++
Delphi
HTML
Java
JavaScript
MySQL
PC interface
Powershell
Perl
PHP
VBScript
Visual Basic
XML

US Job listings




   Misc

   Amazon

   Links

Connect to a MySQL database with PHP and PEAR

 

This shows the basics of connecting to a database with the db package from Pear.

Here is the database table we will be connecting to

CREATE TABLE `lotto` (
`date` date NOT NULL default '0000-00-00',
`ball1` tinyint(4) NOT NULL default '0',
`ball2` tinyint(4) NOT NULL default '0',
`ball3` tinyint(4) NOT NULL default '0',
`ball4` tinyint(4) NOT NULL default '0',
`ball5` tinyint(4) NOT NULL default '0',
`ball6` tinyint(4) NOT NULL default '0',
`bonus` tinyint(4) NOT NULL default '0',
`ballset` tinyint(4) NOT NULL default '0',
`machine` varchar(20) NOT NULL default '',
KEY `date` (`date`)
) TYPE=MyISAM;


 

 

<?php
//this is the path to the dbpager file you downloaded, change this
ini_set("include_path", '/home/path_to_db_pear/dbpager/' . PATH_SEPARATOR . ini_get("include_path"));
require 'DB.php';
//connect to MySQl database using the db package
//the format for this is as follows
//mysql = database being used
//username = your username for access
//password = your password for access
//localhost = database server
//database = the name of the database you are using
$dsn = 'mysql://username:password@localhost/database';
//conect to database
$dbh = DB::connect($dsn);
//SQL query ,list all links
$sql = $dbh->query('SELECT * FROM lotto LIMIT 10');
//display links
//$rows[0] : date and $rows[1] to $rows[7] are the numbers
while($rows = $sql->fetchRow())
{
print"$rows[0] : $rows[1],$rows[2],$rows[3],$rows[4],$rows[5],$rows[6],$rows[7]";
print "<br>";
}
?>

view example here


 




   Sponsors
 

   Software
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals

   Source Code
radians to degrees conversion(C Sharp)
app does not appear in ctrl + alt + delete applications list(VB)
display icon in picturebox control(VB.net)
find a string in another(PHP)
Close window with button(Javascript)
Split a string(ASP)
Delete recent documents(C)
set text colour(CSS)




Copyright © 2004 by programmershelp.co.uk