Skip to main content

Posts

Showing posts from 2011

Introduction to PHP PDO

Introduction to PHP PDO
(PHP Data Objects)


1. What is PDO
2. What Databases does PDO support
3. Where do I begin?
4. Connect to a Database
* Connect to PgSQL
* Connect to SQLite
* Connect to MySQL
* Connect to Firebird
* Connect to Informix
* Connect to Oracle
* Connect to ODBC
* Connect to DBLIB
* Connect to IBM
5. There is no five
6. Close a Database Connection
7. Query a Database
* INSERT
* SELECT
* UPDATE
8. Fetch Modes
* FETCH ASSOC
* FETCH NUM
* FETCH BOTH
* FETCH OBJECT
* FETCH LAZY
* FETCH CLASS
* FETCH INTO
9. Error Handling
10. Prepared Statements
11. Transactions
12. Get Last Insert ID
13. Global Instance
14. Conclusions

What is PDO.

PDO is a PHP extension to formalise PHP's database connections by creating a uniform interface. This allows developers to create code which is portable…

Search in all column in the table fields

<?php
$conn = @mysql_connect( 'localhost','root' ,'')
or die( mysql_errno().': '.mysql_error().NL );
$db=mysql_select_db('kiwi',$conn);
$result = mysql_query("SHOW COLUMNS FROM $table");
if (mysql_num_rows($result) > 0)
{
$i=0;
while ($row = mysql_fetch_array($result))
{
if($i==0)
{

echo $q="select * from $table where ".$row[0]."=".$condition;
}
else
{
$q.=" or ".$row[0]."=".$condition;
}
$i++;
}
}
echo 'a';

echo $q;

?>

Curl Example code php

You must enable CURL libraries on you system


CASE 1: Just retrieve the site content

<?php
$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL,"http://www.bing.com/search?q=test&FORM=MSNH");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1=curl_exec($ch) or die(curl_error());
echo "".$data1."";
echo curl_error($ch);
curl_close($ch);
?>

CASE 2: Retreive the site content by posting the value from FORM for eg. contact form and other
<?php
$a=$_POST["a"]; // Form posted value
$ch = curl_init() or die(curl_error());
$params="a=$a";
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_URL,"http://www.abc.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1=curl_exec($ch) or die(curl_error());
echo "".$data1."";
echo curl_error($ch);
curl_close($ch);
?>

CASE 3: Retrieve date from HTTPS site
<?php
$a=$_POST["a"];
$ch = curl_init()…

Easy way to pagination from smart mysql command

When one is using a LIMIT attribute in their query ,
it's often fallowed by an OFFSET. These two togeather
are often used in pagination ( paging of results ) as in.

select * from thetable limit 10 offset 0

To find out how many rows would be available if one had not
used the LIMIT / OFFSET, one would alter the statement
above to look like this

select SQL_CALC_FOUND_ROWS * from thetable limit 10 offset 0

The return from both commands looks the same, so to get the
row count you need to issue a fallowup query

select FOUND_ROWS()