<?php
echo "<span style='color:red;font-size:1.2em'>obs: the connection host is set by default to localhost,with the user \"root\",with no pass(the default mysql settings). you may need to change the constructors params in order to make this example work</span><br />";
require_once("db.php");
//(change the constructors param with your user/pass/host)
//ex:new EasyPhpMysqlDb(<your_user_name>,<your_password>,<db_host>,<database_name>);
//note that all of theese params are optional...in the way that they have default values
$db = new EasyPhpMysqlDb();
//some dummy config vars
$dbName = "ip";
$tableName = "lagi";
//sql queries to be executed
$createDataBase = "CREATE DATABASE `".$dbName."`";
$truncateTable = "TRUNCATE TABLE `".$tableName."`";
$insertDummyData = "INSERT INTO `".$tableName."` (
`id`,
`raw_input`,
`query`,
`google_response`,
`google_processed`,
`yahoo_response`,
`yahoo_processed`,
`preprocessed`,
`final_response`,
`finalized`
)
VALUES (
13,
'bla iasi paris boo la&3 pLoIe%31ty cHiYnA',
'bla iasi paris ploiesti china',
'bla<place>iasi</place><place>paris</place><place>ploiesti</place><place>china</place>',
1,
'',
0,
1,
'',
0
)";
$createTable = "CREATE TABLE IF NOT EXISTS `".$tableName."` (
`id` int(11) NOT NULL,
`raw_input` text NOT NULL,
`query` text NOT NULL,
`google_response` text NOT NULL,
`google_processed` tinyint(1) NOT NULL DEFAULT '0',
`yahoo_response` text NOT NULL,
`yahoo_processed` tinyint(1) NOT NULL DEFAULT '0',
`preprocessed` tinyint(1) NOT NULL DEFAULT '0',
`final_response` text NOT NULL,
`finalized` tinyint(1) NOT NULL DEFAULT '0',
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
//let's try to connect
if(!$db->connect())
die("connect error : ".$db->getError());
//create a db
if(!$db->executeQuery($createDataBase,'create_db'))
die("create db error :".$db->getError());
//setting the db name->so the DbObj would know on witch database it executes the queries
$db->setDb($dbName);
//create a table
if(!$db->executeQuery($createTable,'create_table'))
die("create table error : ".$db->getError());
//truncating the data in the table,in case the table is already defined and has data
if(!$db->executeQuery($truncateTable,'create_table'))
die("truncate table error : ".$db->getError());
//inserting a row of dummy data
if(!$db->executeQuery($insertDummyData,"insert"))
die("insert error : ".$db->getError());
//retrieving all the data from the newly created table
$result = $db->executeQuery("select * from ".$tableName,"select");
if(!$result)
die("select error : ".$db->getError());
//print the raw results
echo "raw (stdObj) results:<br/>";
print_r($result);
echo "<br /><br />";
echo "some more encoding options:<br/>";
//setting the result to be returned as an array
$db->setResultEncoding("array");
$result = $db->executeQuery("select * from ".$tableName,"select");
if(!$result)
die("select error : ".$db->getError());
//print the raw ARRAY results
echo "raw ARRAY results:<br/>";
print_r($result);
echo "<br /><br />";
//setting the result to be returned as JSON
$db->setResultEncoding("json");
$result = $db->executeQuery("select * from ".$tableName,"select");
if(!$result)
die("select error : ".$db->getError());
//print the raw ARRAY results
echo "JSON result:<br/>";
echo $result;
echo "<br /><br />";
//setting the result to be returned as xml
$db->setResultEncoding("xml");
$result = $db->executeQuery("select * from ".$tableName,"select");
if(!$result)
die("select error : ".$db->getError());
//print the raw ARRAY results
echo "XML result - you can modify the node-names of the xml through the \"setXmlParentNodeName\" and \"setXmlChildNodeName\" methods(you can also use the getters to verify the names);<br/>
!obs : if your browser doesn't show anything,don't pannic! it means that it interprets the code as valid xml and it doesn't print it.just view the source of this document(ctrl+u or go tp view->source) and you shall see the xml-formatted result:<br/>";
echo $result;
echo "<br /><br />";
//a customised print
$db->setResultEncoding('obj');
$result = $db->executeQuery("select * from ".$tableName,"select");
if(!$result)
die("select error : ".$db->getError());
echo "print each returned element ,looping through the matched rows:<br/>";
foreach($result as $row)
{
/*
foreach($row as $obj)
{
//or..acces the fields manualy...like $obj->id(if it has an id)
print_r($obj);
echo "<br />";
}
could use this just to "show" the objects,
but for a more accurate&detailed print,use smth like the following:
*/
echo "id:".$row->id."<br />";
echo "raw_input:".$row->raw_input."<br />";
echo "query:".$row->query."<br />";
echo "preprocessed:".$row->preprocessed."<br />";
echo "google_processed:".$row->google_processed."<br />";
echo "google_response:".$row->google_response."<br />";
echo "yahoo_processed:".$row->yahoo_processed."<br />";
echo "yahoo_response:".$row->yahoo_response."<br />";
echo "finalized:".$row->finalized."<br />";
echo "final_response:".$row->final_response."<br /><br />";
}
//finally....say bbui to the connection
if(!$db->disconnect())
die("disconnect error : ".$db->getError());
?>
|