Archive

Archive for the ‘php_mysql’ Category

Selecting the most common value from relation

October 7th, 2011 No comments

MySQL:
  1. SELECT `keyword`, count(`keyword`)<br />
  2.     FROM data<br />
  3.     GROUP BY `keyword`<br />
  4.     ORDER BY count(`keyword`) desc LIMIT 100, 200

Categories: php_mysql Tags:

PHP & JavaScript -> XML communication

January 26th, 2010 No comments


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

Usage:

PHP:
  1. $content = unicode_urldecode($_GET[&#39;content&#39;]);

Categories: php_mysql Tags:

UTF-8 and apache – mysql – php

December 16th, 2009 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 = &quot;utf-8&quot;

my.cnf

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

In all your PHP scripts :

CODE:
  1. mysql_query(&quot;SET NAMES &amp;apos;utf8&amp;apos;;&quot;,$con);

Importing database :

CODE:
  1. mysql -h host -u username -p password --default_character_set utf8 database &lt;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 No comments
Categories: php_mysql Tags:

PHP post without cUrl

August 29th, 2009 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 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 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 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 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 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 No comments

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

Categories: php_mysql Tags:

MySql date timezone

November 6th, 2008 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 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:
17039 pages viewed, 0 today
11071 visits, 0 today
FireStats icon Powered by FireStats