Archive

Archive for the ‘php_mysql’ Category

PHP & JavaScript -> XML communication

January 26th, 2010 karlosp No comments


PHP:
  1. function unicode_urldecode($url) {
  2.     preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
  3.  
  4.     foreach ($a[1] as $uniord) {
  5.        $utf = '&#x' . $uniord . ';';
  6.        $url = str_replace('%u'.$uniord, $utf, $url);
  7.     }   
  8.     return urldecode($url);
  9. }

Usage:

PHP:
  1. $content = unicode_urldecode($_GET['content']);

Categories: php_mysql Tags:

UTF-8 and apache – mysql – php

December 16th, 2009 karlosp No comments

Settings a LAMP server to work in full unicode UTF-8 :

httpd.conf:

CODE:
  1. AddCharset UTF-8 .utf8
  2. AddDefaultCharset UTF-8

php.ini

CODE:
  1. default_charset = "utf-8"

my.cnf

CODE:
  1. character-set-server=utf8 default-collation=utf8_unicode_ci

In all your PHP scripts :

CODE:
  1. mysql_query("SET NAMES 'utf8';",$con);

Importing database :

CODE:
  1. mysql -h host -u username -p password --default_character_set utf8 database <file.sql

Dont forget to add UTF-8 in the head of all your HTML files :

CODE:
  1. <meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;/>

Categories: php_mysql Tags:

Tutorial: Create a zip file from folders on the fly | Web Development Blog

October 22nd, 2009 karlosp No comments
Categories: php_mysql Tags:

PHP post without cUrl

August 29th, 2009 karlosp No comments
PHP:
  1. <?php
  2.  
  3. $postdata = http_build_query(
  4.     array(
  5.         'var1' => 'some content',
  6.         'var2' => 'doh'
  7.     )
  8. );
  9. $opts = array('http' =>
  10.     array(
  11.         'method'  => 'POST',
  12.         'header'  => 'Content-type: application/x-www-form-urlencoded',
  13.         'content' => $postdata
  14.     )
  15. );
  16. $context  = stream_context_create($opts);
  17. $result = file_get_contents('http://example.com/submit.php', false, $context);
  18.  
  19. ?>

Categories: php_mysql Tags:

Asynchronous HTTP request in PHP

August 21st, 2009 karlosp No comments
PHP:
  1. function curl_post_async($url, $params)
  2. {
  3.     foreach ($params as $key => &$val) {
  4.       if (is_array($val)) $val = implode(',', $val);
  5.         $post_params[] = $key.'='.urlencode($val);
  6.     }
  7.     $post_string = implode('&', $post_params);
  8.  
  9.     $parts=parse_url($url);
  10.  
  11.     $fp = fsockopen($parts['host'],
  12.         isset($parts['port'])?$parts['port']:80,
  13.         $errno, $errstr, 30);
  14.  
  15.     pete_assert(($fp!=0), "Couldn't open a socket to ".$url." (".$errstr.")");
  16.  
  17.     $out = "POST ".$parts['path']." HTTP/1.1\r\n";
  18.     $out.= "Host: ".$parts['host']."\r\n";
  19.     $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  20.     $out.= "Content-Length: ".strlen($post_string)."\r\n";
  21.     $out.= "Connection: Close\r\n\r\n";
  22.     if (isset($post_string)) $out.= $post_string;
  23.  
  24.     fwrite($fp, $out);
  25.     fclose($fp);
  26. }

Categories: php_mysql Tags:

Joomla send email with PHP

August 16th, 2009 karlosp 1 comment
PHP:
  1. <?php
  2. define( '_JEXEC', 1 );
  3. define('JPATH_BASE', dirname(__FILE__) );
  4.  
  5. define( 'DS', DIRECTORY_SEPARATOR );
  6.  
  7. require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  8. require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
  9.  
  10.  $message =& JFactory::getMailer();
  11.   $message->addRecipient("example@gmail.com");
  12.   $message->setSubject('Your subject string');
  13.   $message->setBody("Your body string\nin double quotes if you want to parse the \nnewlines etc");
  14.   $sender = array( 'sender@email.address.org', 'Sender Name' );
  15.   $message->setSender($sender);
  16.   $message->addAttachment('htaccess.txt');
  17.   $sent = $message->send();
  18.   if ($sent != 1) echo 'Error sending email';
  19.   else
  20.   echo "OK";
  21.  
  22. ?>

Categories: php_mysql Tags:

PHP XML traversing

August 3rd, 2009 karlosp No comments
PHP:
  1. function parse_account_list($xml)
  2. {
  3.     $doc = new DOMDocument();
  4.     $doc-&gt;loadXML($xml);
  5.     $entries = $doc-&gt;getElementsByTagName('entry');
  6.     $i = 0;
  7.     $profiles = array();
  8.     foreach($entries as $entry)
  9.     {
  10.         $profiles[$i] = array();
  11.  
  12.         $title = $entry-&gt;getElementsByTagName('title');
  13.         $profiles[$i]["title"] = $title-&gt;item(0)-&gt;nodeValue;
  14.  
  15.         $entryid = $entry-&gt;getElementsByTagName('id');
  16.         $profiles[$i]["entryid"] = $entryid-&gt;item(0)-&gt;nodeValue;
  17.  
  18.         $properties = $entry-&gt;getElementsByTagName('property');
  19.         foreach($properties as $property)
  20.         {
  21.             if (strcmp($property-&gt;getAttribute('name'), 'ga:accountId') == 0)
  22.                 $profiles[$i]["accountId"] = $property-&gt;getAttribute('value');
  23.  
  24.             if (strcmp($property-&gt;getAttribute('name'), 'ga:accountName') == 0)
  25.                 $profiles[$i]["accountName"] = $property-&gt;getAttribute('value');
  26.  
  27.             if (strcmp($property-&gt;getAttribute('name'), 'ga:profileId') == 0)
  28.                 $profiles[$i]["profileId"] = $property-&gt;getAttribute('value');
  29.  
  30.             if (strcmp($property-&gt;getAttribute('name'), 'ga:webPropertyId') == 0)
  31.                 $profiles[$i]["webPropertyId"] = $property-&gt;getAttribute('value');
  32.         }
  33.  
  34.         $tableId = $entry-&gt;getElementsByTagName('tableId');
  35.         $profiles[$i]["tableId"] = $tableId-&gt;item(0)-&gt;nodeValue;
  36.  
  37.         $i++;
  38.     }
  39.     return $profiles;
  40. }

Categories: php_mysql Tags:

MySQL insert date

June 7th, 2009 karlosp No comments
PHP:
  1. $add2h = time() + (7 * 60 * 60);
  2. $dt=date("Y-m-d H:i:s", $add2h);
  3. $time = $dt;
  4.  
  5. $query = "INSERT INTO slovarji (id, language, geslo, time)
  6. VALUES ('', '$language', '$geslo', '$time')";
  7.  
  8. $results = mysql_query($query) or die
  9. ("Could not execute query : $query." . mysql_error());

Categories: php_mysql Tags:

PHP mail() UTF-8

March 17th, 2009 karlosp No comments
PHP:
  1. function mail_utf8($from, $to, $subject = '(No subject)', $message = '', $header = '') {
  2.   $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
  3.   $header_ .= "From: $from\r\n" . "Reply-To: $from\r\n";
  4.   mail($to, "=?UTF-8?B?".base64_encode($subject).'?=', $message, $header_ . $header);
  5. }

Categories: php_mysql Tags: , ,

Mysql php generator

February 20th, 2009 karlosp No comments

http://www.turningturnip.co.uk/free-mysql-php-generator/

Categories: php_mysql Tags:

MySql date timezone

November 6th, 2008 karlosp No comments

Knowing the time offset, you can replace all your SQL statements of

SQL:
  1. SELECT NOW();

with

SQL:
  1. SELECT DATE_ADD(NOW(), INTERVAL 2 HOUR);

Categories: php_mysql Tags:

PHP date compare

October 26th, 2008 karlosp No comments
PHP:
  1. $exp_date = "2006-01-16";
  2. $todays_date = date("Y-m-d");
  3. $today = strtotime($todays_date);
  4. $expiration_date = strtotime($exp_date);
  5. if ($expiration_date> $today)
  6. { $valid = "yes";
  7. }
  8. else { $valid = "no"; }

Categories: php_mysql Tags:

php curl 2 & http build query

October 24th, 2008 karlosp No comments

http_build_query

PHP:
  1. //extract data from the post
  2. extract($_POST);
  3.  
  4. //set POST variables
  5. $url = 'http://domain.com/get-post.php';
  6. $fields = array(
  7.                         'lname'=>urlencode($last_name),
  8.                         'fname'=>urlencode($first_name),
  9.                         'title'=>urlencode($title),
  10.                         'company'=>urlencode($institution),
  11.                         'age'=>urlencode($age),
  12.                         'email'=>urlencode($email),
  13.                         'phone'=>urlencode($phone)
  14.                 );
  15.  
  16. //url-ify the data for the POST
  17. foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  18. rtrim($fields_string,'&');
  19.  
  20. //open connection
  21. $ch = curl_init();
  22.  
  23. //set the url, number of POST vars, POST data
  24. curl_setopt($ch,CURLOPT_URL,$url);
  25. curl_setopt($ch,CURLOPT_POST,count($fields));
  26. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  27.  
  28. //execute post
  29. $result = curl_exec($ch);
  30.  
  31. //close connection
  32. curl_close($ch);

Categories: php_mysql Tags:
71258 pages viewed, 274 today
38330 visits, 133 today
FireStats icon Powered by FireStats