Monthly Archives: March 2013

PHP Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?

Hey Folks,

Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?

Got the above warning? No Problems. You can get rid of this warning by just enabling php_openssl.dll extension in your php.ini [For eg. xammp/php/php.ini] configuration file. [If you’re not seeing php_openssl.dll itself in your php.ini, just directly add the extension line to the php.ini configuration file]

Your php.ini file :

;extension=php_oci8_11g.dll
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll

You just need to uncomment the line extension=php_openssl.dll.

It should look like below :

;extension=php_oci8_11g.dll
extension=php_openssl.dll
;extension=php_pdo_firebird.dll

If the extension itself does not exist, You have to add extension=php_openssl.dll to your php.ini file. (Provided you’ve the proper dll file bundled with your windows PHP environment (I’m using XAMPP & the dll extension is php_openssl.dll.)

Note : I’m using XAMPP for Windows

Cheers,
JENSon.

PHP : Deleting Duplicates From An Array

Hey Folks,

PHP’s array_unique function allows you to remove duplicate values from an array.

Syntax:

<?php
$arr = array("a" => "Ford", "Mercedes", "b" => "BMW", "Lotus", "Ford", "Renault");
$result = array_unique($arr);
echo "<pre>";
print_r($result);
echo "</pre>";

/* Output
Array
(
    [a] => Ford
    [0] => Mercedes
    [b] => BMW
    [1] => Lotus
    [3] => Renault
)
*/
?>

Note : The keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

Cheers,
JENSon.

 

 

Convert a string to an array using str_split PHP function.

Hey Folks,

In PHP, You can convert a string to an array using str_split function.

Syntax:

array str_split ( string $string [, int $split_length = 1 ] )

/* $string=The input string.
$split_length=Maximum length of the pieces. */

Example:

<?php

$str = "Awesome World";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

echo "<pre>";
print_r($arr1); 
print_r($arr2);
echo "</pre>";

/* This will output..

Array
(
    [0] => A
    [1] => w
    [2] => e
    [3] => s
    [4] => o
    [5] => m
    [6] => e
    [7] =>  
    [8] => w
    [9] => o
    [10] => r
    [11] => l
    [12] => d
)
Array
(
    [0] => Awe
    [1] => som
    [2] => e w
    [3] => orl
    [4] => d
)

*/
?>

Return Values:

If the optional split_length parameter is specified, the returned array will be broken down into pieces with each being split_length in length, otherwise each piece will be one character in length.

FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (& only) array element.

Cheers,
JENSon.

 

 

What is PDO (PHP Data Objects)? Basic Explanation.

Hey Folks,

The PDO (PHP Data Objects) is an extension which defines a lightweight, consistent interface for accessing databases in PHP. PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn’t rewrite SQL or emulate missing features. PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new Object Oriented features in the core of PHP 5, and so will not run with earlier versions of PHP.

Note : I assume you already have a PDO configured PHP version. If not, Please click here to see the installation process.

PDO Connections

Connecting to Database (Eg. MySQL) :

<?php $dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password); ?>

//Note : If there are any connection errors, a PDOException object will be thrown.

Handling Errors In Connection :

<?php
try {
    $dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password);
    foreach($dbconn ->query('SELECT * from tablename') as $row) {
        print_r($row);
    }
    $dbconn = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

Upon successful connection to the database, an instance of the PDO class is returned in your script. The connection remains active for the lifetime of that PDO object.

Closing a connection :


<?php
$dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password);
// use the connection here


// and now we're done; close it
$dbconn= null;
?>

Persistent connections :


<?php
$dbconn = new PDO('mysql:host=localhost;dbname=testdb', $username, $password, array(
    PDO::ATTR_PERSISTENT => true //If you wish to use persistent connections, you must set PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor.
));
?>

Multiple database connections :

try {
  $dbconn1 = new PDO('mysql:host=localhost;dbname=testdb1', $username, $password);
  $dbconn2 = new PDO('mysql:host=localhost;dbname=testdb2', $username, $password);
} catch (PDOException $ex) {
  echo 'Connection failed: ' . $ex->getMessage();
}

You can execute the query like this :

<?php
$result = $dbconn->query("select * from tablename");
foreach ($result as $row) {
  echo $row['fieldname'] . "\n";
}
?>

Fetching data using prepared statements 1 :


<?php
$query= $dbconn->prepare("SELECT * FROM tablename where fieldname = ?"); // The user input is automatically quoted, so there is no risk of a SQL injection attack. 
if ($query->execute(array($_POST['name']))) {
  while ($row = $query->fetch()) {
    print_r($row);
  }
}
?>

Fetching data using prepared statements 2 :

$query= $dbconn->prepare("select * from tablename where id = :id");
$query->execute(array(':id' => 555));
$row = $query->fetch();

Calling a stored procedure with an output parameter :


<?php
$query = $dbconn->prepare("CALL sp_returns_string(?)");
$query->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 

// call the stored procedure
$query->execute();

print "procedure returned $return_value\n";
?>

I have covered only some basics here. You’ll get detailed explanation of various other functions of PDO from the php.net Website.

Cheers,
JENSon.

 

 

PHP Single Line if else Syntax

Here is the syntax for single line if else statement in PHP.

<?php
$x=15;
$y=10;
$val=($x<$y)?"Correct":"Incorrect";
echo $val;//Outputs Incorrect
?>

Cheers,
JENSon.