Showing entries 1 to 10 of 30
10 Older Entries »
Displaying posts with tag: PHP/MySQL (reset)
How To – Configure MySQL to Use UTF-8

Background Knowledge

Using the character set UTF-8 allows for the use of any language, can represent every character in the Unicode character set and is backward compatibility with ASCII. Not to mention is can handle any platform and be sent through many different systems without corruption. With such advantages this is why so many are making the switch.

The following instructions were done on Debian Squeeze v6.04 AMD64 operating system using MySQL v14.14 Distrib 5.1.61.

Solution – Server Configuration

At present MySQL is configured by default to use “latin1″ character set. Here’s how to change MySQL configuration to use UTF-8 character set and collation.

  1. Check MySQL’s current configuration, run the following two SQL statements.

    1
    2
    
    SHOW VARIABLES LIKE '%collation%'; …
[Read more]
How To – Fix MySQL Option Without Preceding Group

Background Knowledge

You try starting, stopping or connecting to MySQL and receive the following error, “Error: Found option without preceding group in config file: /etc/mysql/conf.d/char_collation_set.cnf at line: 1″. The error message my vary but comes to the same issue. MySQL may not start or might experience connectivity issues.

This issue was experienced on Debian Squeeze v6.04 AMD64 system with MySQL v14.14 Distrib 5.1.61.

Solution

This issue is caused by a improperly formatted MySQL configuration file(s) and refers to one missing the group heading (e.g. [mysqld], [mysqld_safe], etc.).

Source: MySQL 5.1 Reference Manual :: 4.2.3.3. Using Option Files
Source: …

[Read more]
Example of a Basic MSSQL Query using PHP

An example of a basic MSSQL (Microsoft SQL Server/SQL Server Express) query using PHP.

1
2
3
4
5
6
7
8
9
$szQry = "SELECT column1, column2 FROM foo";
$szDBConn = mssql_connect("host","username","password");
mssql_select_db("database_name", $szDBConn);
$saResults = mssql_query($szQry, $szDBConn);
while($obResults = mssql_fetch_row($saResults))
{
   echo $obResults[0]." ".$obResults[1];
}
mssql_close($szDBConn);

Comments/description of Example

Line #1
SQL statement that will be sent to the MySQL database server.
Line #2
MSSQL database login credentilas; host (127.0.0.1), username and password.
The “host” is the server name or IP address of your database server. If your host has multiple instances the “host” value would be formatted like so …
[Read more]
PHP – Upgrading v5.2.5 to v5.2.8

Background Knowledge

The following is the process I took to upgrade a web server with PHP v5.2.5 to PHP v5.2.8 running on OpenBSD. PEAR is already installed on this system and up to date. I wasn’t sure if I should exclude PEAR at install or not so therefore did not tell the configurator to exclude PEAR at install.

Installation Process

  1. Download the latest stable PHP release from command prompt # wget http://ca.php.net/get/php-5.2.8.tar.gz/from/a/mirror
  2. # tar -zxvf php-5.2.8.tar.gz
  3. ./configure –with-mysql=/usr/local –with-mssql=/usr/local –with-apxs –with-zlib-dir=/usr/lib –with-config-file-path=/var/www/conf –with-iconv=/usr/local/bin/iconv –enable-exif –enable-mbstring …
[Read more]
Pear::Date Returned Timezone is Wrong

Background Knowledge

I’m trying to determine the difference in minutes between two timestamps. I’m using Pear::Date to do this. The issue comes into play when I noticed that the wrong timezone was being used by Pear::Date, UTC. If I do not use Pear::Date the timezone is set correctly.

I have tried using date_default_timezone_set() and it does set the timezone back, however I feel this shouldn’t be necessary as the default timezone should be used. I have been using date_default_timezone_get() to determine what timezone is being used.

It’s my understanding that Pear::Date uses UTC when it is unable to determine the default timezone. As far as I know I have the default timezone set correctly and with a …

[Read more]
Using short if statement in programming

In many programing languages it is possible to shorten if statements using what’s called the ternary operator. It is sometimes referred as the “one line if statement” or the “short if statement”. This can help at times to produce cleaner code, however use this operator wisely as it is not always best to be used for more complicated statements.

PHP Example of an if statement

1
2
3
4
5
6
7
8
if($nFoo > 0)
{
   echo "I'm at the work.";
}
else
{
   echo "I'm at home.";
}

PHP Example using the ternary operator

1
      echo $nFoo > 0 ? "I'm at the work." : "I'm at home.";

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to …

[Read more]
How To - Remove Leading Zeros

Solutions

Using PHP function intval().

Code example: echo intval(”05″);
Returned value: 5

Using PHP function ltrim().

Code example: echo ltrim(”005″,”0″);
Returned value: 5

ShareThis

How To - Convert MySQL Timestamp/Datetime to Unix Timestamp

Background Knowledge

Since MySQL v4.1 timestamp and datetime data types are formatted “YYYY-MM-DD HH:MM:SS”. Prior to MySQL v4.1 the timestamp was formatted as YYYYMMDDHHMMSS” and datetime formatted as “YYYY-MM-DD HH:MM:SS”. Refer to MySQL Reference Manual for further details.

The Unix timestamp differs from MySQL. Unix’s timestamp is a integer value of seconds since January 1, 1970. For further explanation of Unix timestamps refer to Wikiepedia or UnixTimestamp.com.

Solutions

In MySQL you can use Unix_Timestamp() function.

Query Example: SELECT …

[Read more]
French Characters Not Rendering Correctly

Background Knowledge

The MySQL database v4.0.23 is using the default character set of “Latin1″. When the database was created I had no knowledge of character sets other wise it would have been “UTF-8″.
The web pages are using a character set of “UTF-8″.

Problem

Data being queried from a MySQL database that contains French accent characters will not render correctly in the browser even after applying PHP htmlentities().

Example code: $string = htmlentities($string , ENT_QUOTES, “UTF-8″);

Solution

The queried data from the database was inputted using the character set “ISO-8859-1″. I …

[Read more]
PHP Calendar Functions Error

I was trying to use the PHP calendar API and immediately received this error message, “Fatal error: Call to undefined function cal_days_in_month()”. This error message means PHP was not compiled with the calendar extension.

Solution

The only solution to this error message and other similar error messages relating to the PHP calendar API requires PHP to be compiled with the calendar extension by adding “–enable-calendar” to the “configure command” as stated in the PHP documentation on the Calendar functions page.

How to Tell if the Calendar Extension is Installed

You can verify weather or not the PHP Calendar extension was compiled at install by using the phpinfo() function. When viewing the output of phpinfo() look under “Configure Command” just below “Build Date” and if you do not see “–enable-calendar” present then all PHP …

[Read more]
Showing entries 1 to 10 of 30
10 Older Entries »