PHP isset() function returns FALSE even though the variable is defined
Let’s say we have a table item in a MySQL database, with the primary key iditem and a field code that can be NULL:
CREATE TABLE `item` ( `iditem` int(10) unsigned NOT NULL, `code` VARCHAR(10) DEFAULT NULL, PRIMARY KEY (`iditem`), ); -- -- Dumping data for table `item` -- INSERT INTO `item` (`iditem`, `code`) VALUES (1, 'as1435v'), (2, NULL), (3, 1, 'YHryt4t90');
In PHP we define a variable $item_code that we initialize with 0 and then we run a query for setting the value of $item_code to the code of the item which has the id 2 in our database:
$item_code = 0;
$query = "SELECT code FROM item WHERE iditem = 2";
$result = mysql_query($conn); //$conn is our database connection
if($result) {
if($row = mysql_fetch_row($result)) {
$item_code = $row[0];
}
}
if(isset($item_code)) {
echo "item code: ".$item_code;
} else {
echo "item code is not set";
}
The output will be:
item code is not set
That is because the code is NULL in the database and it is transferred into PHP not as the empty string, but as NULL.
And the name of the isset() function is misleading as it returns FALSE not only when a variable is not defined, but also when a variable has been set to NULL.
Strange empty text line appearing inside my page
Usually you see an empty text line and you look at the source and you see nothing there just a space…
That happens often when the files that are outputted are using the encoding UTF8 with signature, instead of using the correct encoding UTF8 without signature.
How to convert server date and time to the local date and time taking into account daylight saving time
‘Many countries, or even parts of countries, adopt daylight saving time (DST, also known as “Summer Time”) during part of the year. This typically involves advancing clocks by an hour near the start of spring and adjusting back in autumn (“spring” forward, “fall” back).’ (see Wikipedia)
Now I will demonstrate how we can convert a server date and time to the corresponding date and time in another timezone in Europe, taking Romania as example.
‘All countries in Europe except Iceland observe DST, and most change on the same date and time, starting on the last Sunday in March and ending on the last Sunday in October.’
‘In the West European (UTC), Central European (CET, UTC+1), and East European (UTC+2) time zones the change is simultaneous: on both dates the clocks are changed everywhere at 01:00 UTC, i.e. from local times of 01:00/02:00/03:00 to 02:00/03:00/04:00 in March, and vice versa in October.’ (see Wikipedia)
Romania is a country in the Eastern Europe, which falls under the East European Time (EET) also known as UTC+2 time zone and uses Eastern European Summer Time (UTC+3) as a summer daylight saving time.
The next function will be used to find out the offset in hours from UTC (GMT) of a local date and time of Romania or another region that switches between DST and standard time on the same date and time as Romania (is the local date and time in DST or in standard time?)
function local_offset_from_utc($timestamp, $standard_offset)
//$timestamp - the timestamp of the server date and time we want to convert to local date and time
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1
{
$year = gmdate("Y", $timestamp); //get the UTC year of the given date and time
//get the date to 'spring forward'
$mar_31_utc_ts = gmmktime(0, 0, 0, 4, 0, $year); //the UTC timestamp of March 31, the last day of March, equivalent to gmmktime(0, 0, 0, 3, 31, $year)
$last_sun_mar = 31 - gmdate("w", $mar_31_utc_ts); //the day of the month under which falls the last Sunday in March
$spring_ts = gmmktime(1, 0, 0, 3, $last_sun_mar, $year); //the timestamp of the last Sunday in March 01:00 UTC
//get the date to 'fall back'
$oct_31_utc_ts = gmmktime(0, 0, 0, 11, 0, $year); //the UTC timestamp of October 31, the last day of October, equivalent to gmmktime(0, 0, 0, 10, 31, $year)
$last_sun_oct = 31 - gmdate("w", $oct_31_utc_ts); //the day of the month under which falls the last Sunday in October
$fall_ts = gmmktime(1, 0, 0, 10, $last_sun_oct, $year); //the timestamp of the last Sunday in October, 01:00 UTC
if($timestamp >= $spring_ts && $timestamp < $fall_ts) //daylight saving time
{
return $standard_offset + 1;
}
//standard time
return $standard_offset;
}
If the server switches between DST and standard time simultaneously with your timezone, you can simply use date("I") to find out if your local date and time is in daylight saving time or standard time:
if(date("I", $timestamp) == 1)
{
$offset = $standard_offset + 1;
}
else
{
$offset = $standard_offset;
}
Be careful with that, because a sever don't always switch between DST and standard time at the same hour as the timezone it has set, for example my server switches from DST to standard time at 03:00 UTC+3, which becomes 02:00 UTC+2, when it should go from 04:00 UTC+3 to 03:00 UTC+2). So I better use the function above that works not caring if or when the timezone of the server switches between DST and standard time.
If an offset of a timezone from UTC is negative, then the UTC date and time is greater than the corresponding date and time in the considered timezone; if the offset is positive then the UTC date and time is lower than the corresponding date and time in the considered timezone. If we consider t being the date and time in a given timezone, utc being the corresponding UTC date and time and offset being the offset of the given timezone from UTC, then we have:
t = utc + offset
utc = t - offset
So if we consider t1 being a server date and time and o1 being the offset of the timezone of the server from UTC (in hours) and t2 being the local date and time and o2 being the offset of the timezone of the server from UTC (in hours) we have:
utc = t1 - o1
t2 = utc + o2 = t1 - o1 + o2
In practice we only have a server date and time which we convert to timestamp ts1 based on which we find out the timestamp ts2 when the date and time in the timezone of the server equaled/will equal to the local date and time corresponding to ts1:
ts2 = ts1 - os1 + os2, where os1 = o1 * 60 *60 seconds and os2 = o2 * 60 *60 seconds
E.g. Let's consider the timezone of the server date and time is UTC-5 and Romania is in standard time (has an offset of 2 hours from UTC).
The UTC date and time that corresponds to the given timestamp equals to the server date and time that was/will be 5 hours later than the given timestamp:
ts_aux = ts1 - (-5 * 3600) = ts1 + 5 * 3600
The local date and time of Romania corresponding to the given timestamp equals to the UTC date and time that was/will be 2 hours later than the UTC date and time computed above:
ts2 = ts_aux + 2 * 3600 = ts1 + (5 + 2) * 3600 = ts1 + 7 * 3600
But this is NOT true if the server time and date that equals the needed local date and time falls under DST and the original server date falls under standard time or vice versa, which means that the offsets from UTC of the two server dates are different by 1 hour.
For example, if the server we considered before is in UTC-5 DST and goes back one hour on the first of November at 03:00:00 local server time and we want to see what Romania local date and time corresponds to the server date and time November 1st 01:00:00, then, applying the formula above, we will obtain a timstamp that corresponds to the server date and time November 1st 07:00:00 which should correspond to the local Romania time for the original server time, but the correct Romania date and time being November 1st 08:00:00. That is because, between November 1st 01:00:00 and November 1st 07:00:00, the server goes back one hour so that 03:00:00 DST becomes 02:00:00 standard time and 08:00:00 becomes 07:00:00, while the Romania time does not, Romania switching to DST one week earlier, so the correct formula in this case is:
ts2 = ts1 - os1 + os2 + 3600
ts2 = ts1 + (5 + 2 + 1) * 3600 = ts1 + 8 * 3600
Similarly, if the server switches from standard time to DST between the two dates, the formula becomes:
ts2 = ts1 - os1 + os2 - 3600
ts2 = ts1 + (5 + 2 - 1) * 3600 = ts1 + 6 * 3600
The next function is used to determine what the timestamp was/will be in the timezone of the server when the server date and time equaled/will be equal to the needed local date and time.
function get_timestamp_for_local($date = "", $standard_offset = 2)
{
if(!empty($date))
{
$timestamp = strtotime($date);
}
else
{
$timestamp = time();
}
$os1 = date("Z", $timestamp);
$os2 = local_offset_from_utc($timestamp, $standard_offset) * 3600;
$ts = $timestamp - $os1 + $os2;
//check the offset from UTC of the server date and time that equal the needed local date and time
$os3 = date("Z", $ts);
if($os3 == $os1) //the two server dates are both in DST or both in standard time
{
return $ts;
}
elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time
{
return $ts - 3600;
}
//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST
return $ts + 3600;
}
And finally we get the needed date and time:
function get_ro_date($date)
{
return date("d.m.Y H:i:s", get_timestamp_for_local($date));
}
Next l will show how we can convert a server date and time to the corresponding date and time in another timezone in North America, taking the State of Washington as example.
'North America generally follows the same procedure, with each time zone switching at 02:00 LST (local standard time) to 03:00 LDT (local daylight time) on the second Sunday in March, and back from 02:00 LDT to 01:00 LST on the first Sunday in November since 2007.'
In the United States of America, 'the start of DST now occurs on the second Sunday in March and ends on the first Sunday in November.' (see Wikipedia)
The State of Washington falls under the Pacific Time Zone (PT), which is Pacific Standard Time (PST) or UTC-8 when observing standard time and Pacific Daylight Time (PDT) or UTC-7 during daylight saving time.
So we have to change the previous local_offset_from_utc function to find out the offset in hours from UTC (GMT) of a local date and time of Washington:
function local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour)
//$timestamp - the timestamp of the server date and time we want to convert to local date and time
//$standard_offset - the offset from UTC of the local date and time when observing the standard time; in daylight saving time the offset is $standard_offset + 1
//$spring_forward_hour - the hour of the local time when switching to DST
//$fall_back_hour - the hour of the local time when switching to standard time
{
//find out the year of the local date and time corresponding to the given server date and time, based on the corresponding UTC date and time
$year = gmdate("Y", $timestamp); //UTC year of the given server date and time
if($standard_offset < 0) //local timezone is behind UTC
{
//if the given sever date and time corresponds to January the 1st in UTC and the UTC hour is lower than the absolute value of the offset of the local time from UTC, then the corresponding local date and time falls in the year previous to the UTC year
$month = gmdate("n", $timestamp);
if($month == 1)
{
$day = gmdate("j", $timestamp);
if($day == 1)
{
$hour = gmdate("G", $timestamp);
if($hour + $standard_offset < 0)
{
$year -= 1;
}
}
}
}
else //local timezone time is ahead of UTC
{
//if the given sever date and time corresponds to December 31 in UTC and the sum of UTC hour and the offset of the local time from UTC is greater than 24, then the corresponding local date and time falls in the year subsequent to the UTC year
$month = gmdate("n", $timestamp);
if($month == 12)
{
$day = gmdate("j", $timestamp);
if($day == 31)
{
$hour = gmdate("G", $timestamp);
if($hour + $standard_offset > 24)
{
$year += 1;
}
}
}
}
//get the date to 'spring forward'
$mar_1_utc_ts = gmmktime(0, 0, 0, 3, 1, $year); //the timestamp for the UTC time of March the 1st, 00:00:00
$mar_1_day_of_week = gmdate("w", $mar_1_utc_ts);
//compute the day of the month under which falls the second Sunday in March
if($mar_1_day_of_week > 0) //the day of the week of the 1st of March is not Sunday
{
$second_sun_mar = 15 - $mar_1_day_of_week;
}
else
{
$second_sun_mar = 8;
}
$spring_local_ts = gmmktime($spring_forward_hour - $standard_offset, 0, 0, 3, $second_sun_mar, $year);
//get the date to 'fall back'
$nov_1_utc_ts = gmmktime(0, 0, 0, 11, 1, $year); //the timestamp for the UTC time of November the 1st, 00:00:00
$nov_1_day_of_week = gmdate("w", $nov_1_utc_ts);
//compute the day of the month under which falls the first Sunday in November
if($nov_1_day_of_week > 0) //the day of the week of the 1st of November is not Sunday
{
$first_sun_nov = 8 - $nov_1_day_of_week;
}
else
{
$first_sun_nov = 1;
}
$fall_local_ts = gmmktime($fall_back_hour - ($standard_offset + 1), 0, 0, 11, $first_sun_nov, $year);
if($timestamp >= $spring_local_ts && $timestamp < $fall_local_ts) //daylight saving time
{
return $standard_offset + 1;
}
//standard time
return $standard_offset;
}
The function get_timestamp_for_local remains the same, only we add 2 more paramters to it which will be needed when calling the function:
function get_timestamp_for_local2($date = "", $standard_offset = -8, $spring_forward_hour = 2, $fall_back_hour = 3)
{
if(!empty($date))
{
$timestamp = strtotime($date);
}
else
{
$timestamp = time();
}
$os1 = date("Z", $timestamp);
$os2 = local_offset_from_utc2($timestamp, $standard_offset, $spring_forward_hour, $fall_back_hour) * 3600;
$ts = $timestamp - $os1 + $os2;
//check the offset from UTC of the server date and time that equal the needed local date and time
$os3 = date("Z", $ts);
if($os3 == $os1) //the two dates are both in DST or both in standard time
{
return $ts;
}
elseif($os3 > $os1) //the server date and time that equal the needed local date and time are in DST and the original server date and time are in standard time
{
return $ts - 3600;
}
//the server date and time that equal the needed local date and time are in standard time and the original server date and time are in DST
return $ts + 3600;
}
And now we can find out the date and time in the State of Washington that corresponds to the given server date and time:
function get_wa_date($date)
{
return date("Y-m-d H:i:s", get_timestamp_for_local2($date));
}
SoftException in Application.cpp:544: Directory “/path/dir” is writeable by group
Full error message: SoftException in Application.cpp:544: Directory “/path/to/directory” is writeable by group
The error apeared in cpanel logs at a hosting company and was related to a directory I created via ftp and where I tried to run some php script. When accessing the script from the web browser i got the error “500 Internal Server Error”
The problem is that Apache 2.0 server doesn’t allow in some cases (i don’t know these cases yet) that the directories created with write access rights set to all unless it’s set by the same unix user as the user running apache process. In order to fix this we need to create the folder from the same user as apache user. To do this we need to create the folder or to change the access rights from php.
The fix is to upload a small php file with the following code:
<?php
chmod("./dir", 0755);
?>
After we set the access right on “dir” to 777 we run the php script then the error should not appear anymore.
When submitting a PHP form, the script does not stop
If you submit a PHP form which passes a large amount of data using the POST method, like a file upload form, it would be a good idea to increase the value of the PHP configuration variable post_max_size (you will find it in the php.ini file) if it seems like the script is running forever. post_max_size should be set to a value larger than the maximum sum of the sizes of the files the script is used to upload.
Importing a MySQL data file using phpMyAdmin results in incomplete data in MySQL
I tried moving all my databases from MySQL 4.1 to MySQL 5.1 on Windows and since I had some InnoDB tables I could not just copy the data files between MySQL Server versions (from C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 4.1\data\ to C:\Documents and Settings\All Users\Application Data\MySQL\MySQL 5.1\data\).
So, after I exported the data from the old MySQL Server version I tried importing it to the MySQL 5.1 using phpMyAdmin.
The file to import was 42MB in size, so I had to set some PHP configuration variables (in php.ini file) to greater values:
post_max_size = 64M upload_max_filesize = 64M
At first it looked like the phpMyAdmin import script entered a loop or something, as it wouldn’t stop even after running for several minutes and, as I was looking into the data files it was producing, no new files appeared. It was like it blocked on a table with a large number of records, which didn’t make any sense. Then I restarted the Apache server and I tried the import all over again. The same thing was happening. I finally decided to let it run for more time and the script finally stopped. But the data imported was incomplete; a part of the records in the large table I thought script blocked on, and every table and database from there on were missing, with no PHP or MySQL error returned.
Then I thought the script needed even more time to run, so I increased the value of the PHP configuration variable max_execution_time, but with the same result.
The solution. It seems that the execution time limit for the import script in phpMyAdmin is defined by the variable $cfg['ExecTimeLimit'] in <path_to_phpMyAdmin>/libraries/config.default.php and the value of this variable should be a lot greater than its default value of 300 seconds when importing a great amount of data. In my case it needed about an hour (3600 seconds) to complete. So you have to set this variable depending on the quantity of data you are importing.
You might also need to set the MySQL configuration variable max_allowed_packet (in my case, in C:\Program Files\MySQL\MySQL Server 5.1\my.ini) to a greater value, if you have large queries in the data file you want to import, for avoiding MySQL error 2006, ‘mysql server has gone away’.
Php script redirects even when output started before calling header() function
This happens because of output_buffering is on.
In order to turn off output buffering modify php.ini if you have access and set:
output_buffering = Off
or add in the local directory a .htaccess file containing this line:
php_value output_buffering Off # or 0
mysqldump – with exec() function from php outputs empty file
This error occurs on any operating system (windows, linux). The problem is that instead of getting a sql file with the database data you get a empty (0 kb.) file.
So we have the following code:
$command = "mysqldump --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." > backup.sql"; exec($command, $ret_arr, $ret_code); echo "ret_arr: <br />"; print_r($ret_arr);
and we get an empty file and no output.
So we will fix this error in a few steps:
1. First we need to make sure that we have access to mysqldump command. For Linux machines this command is accessible from anywhere if not you will have to find the place where mysqldump file is (usually the bin folder of mysql).
In order to do this we have to get some output from our command so we will strip all the options from the command and we will remain with this:
$command = "mysqldump"; // mysqldump.exe on Windows
So execute the php script. It’s ok if you get output like this:
Array ( [0] => Usage: mysqldump [OPTIONS] database [tables] [1] => OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] [2] => OR mysqldump [OPTIONS] --all-databases [OPTIONS] [3] => For more options, use mysqldump --help )
If you don’t see something like that then you must check to see the path to the mysqldump command.
If you are in windows make sure you append the full path to the command. If you have a folder like I have C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe with spaces in it you must make sure that you enclose the command between quotes like this:
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." > backup.sql";
If append the right path to the command and you still cannot get the output then this article can’t help.
2. Make sure you have the rights to create the sql file. This step is mostly for Linux machines where it is very possible that you may try to create a file from php in a folder where you don’t have writing rights.
So to test this after the previous step is done you can do the following: append to the previous command extra options so that the output is not returned but instead written in a file. So we have the previous command:
In Windows:
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" > backup.sql";
In Linux:
$command = "mysqldump > backup.sql";
After running the file “backup.sql” should be created.
3. You must now correct your statement. This means that we must use the long versions of the options like this:
Windows:
$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." > backup.sql";
Linux:
$command = "mysqldump --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." > backup.sql";
The fix: The options for mysqldump when called from php must be in the longer version. Instead of –u use –user, instead of –p use –password and so on.
The attachment of an email sent via PHP has 0 bytes
I wanted to send an email that only contained an attachment as follows:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "To: <".$to_email.">\r\n";
$headers .= "From: ".$from_name." <".$from_email.">";
$random_hash = md5(date('r', time()));
// add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
// read the atachment file contents from a string previously formed,
// encode it with MIME base64,
// and split it into smaller chunks
$attachment = chunk_split(base64_encode($content));
// construct the body of the message
$message = "--PHP-mixed-".$random_hash;
// my attachment was an html file
$message .= "\r\nContent-Type: text/html; name=\"".$filename."\"";
$message .= "\r\nContent-Transfer-Encoding: base64";
$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";
$message .= "\r\n--PHP-mixed-".$random_hash."--";
// Windows
ini_set('sendmail_from', $return_email);
mail($to_email, addslashes($subject), $message, $headers);
ini_restore('sendmail_from');
// Linux
// mail($to_email, addslashes($subject), $message, $headers, "-r ".$return_email);
I received an email with an attachment, but the problem was that the attached file had 0 bytes.
To my surprise, after comparing the source of the email with the source of an email with a complete attachment, I found out the problem: before the actual content of the attachment ($attachment) there should be an empty line.
I replaced the line of code:
$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";
with:
$message .= "\r\nContent-Disposition: attachment\r\n\r\n".$attachment."\r\n"; // notice the double \r\n
and it worked! This way I received the email with the correct attachment.
How to use specific language characters with PHP and MySQL (example: Romanian)
Problem: Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones.
My fix for this problem is using UTF-8 character set encoding for every page of the website and the MySQL tables that contain the fields you are using. Also all the html encodings from PHP use the UTF-8 character set encoding (this is not mandatory).
If you already have the database, but with the default character set (latin1) and collation (latin1_swedish_ci) for the tables with text fields (of type CHAR, VARCHAR, TEXT etc) in which you need to have special characters, you should change the character set of each of those tables like this:
ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;
If you don’t have the database then you should create it and when you create a table that you need to use with specific language characters, you should specify the character set for that table:
CREATE TABLE `my_table` ( `idmy_table` tinyint(3) unsigned NOT NULL auto_increment, `my_field` varchar(255) NOT NULL default '', PRIMARY KEY (`idmy_table`) ) CHARSET=utf8;
The most important thing is that in PHP, after opening a database connection, before executing any query to the database, you should ensure that this code is executed
mysql_query("SET NAMES utf8", $my_conn);
This tells the server what character set the client is using for sending SQL statements and the character set the server should use to return the results to the client.
A simple example:
<?php
$my_conn = @mysql_connect("localhost", "user", "pass")
or die("There was a problem connecting to MySQL. Please try again later.");
if(!@mysql_select_db("test", $my_conn))
{
die ("There was a problem connecting to the database. Please try again later.");
}
mysql_query("SET NAMES utf8", $my_conn);
if(!empty($_GET['mystr']))
{
// insert the string into the database
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";
$result = mysql_query($query, $my_conn);
if($result)
{
// save the id of the table row inserted
$last_insert_id = mysql_insert_id($my_conn);
// get the last inserted value
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";
$result = mysql_query($query, $my_conn);
if($result && $row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$db_string = $row['my_field'] ;
}
}
}
elseif(!empty($_GET['searchstr']))
{
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";
$result = mysql_query($query, $my_conn);
if($result)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$search_results[$row['idmy_table']] = $row['my_field'];
}
}
}
mysql_close($my_conn);
?>
<html>
<head>
<title>Page title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<?php
if(!empty($db_string))
{
echo "<strong>Inserted string</strong>: $db_string<br />";
}
?>
<form method="get" action="">
String to insert into the database <input type="text" name="mystr"/>
<input type="submit" value="GO"/>
</form>
<?php
if(!empty($search_results))
{
echo "<strong>Search results</strong>:<br />";
foreach($search_results as $id => $value)
{
echo $value."<br />";
}
}
?>
<form method="get" action="">
Search query <input type="text" name="searchstr"/>
<input type="submit" value="GO"/>
</form>
</body>
</html>
Tip: The search in Romanian language over the database (tested with MySQL LIKE operator) works like a charm when searching words that have special characters or not.
For example: In Romanian language the word “peasant” is written as “ţăran” and someone who searches it gets the same result for the search terms “taran” or “ţăran” or “ţaran” or “tărân” and so on – so this is the real magic.
UPDATE: You may also need to add a header to the php script if you use ob_start or similar php functions like this:
header("Content-type: text/html; charset=UTF-8");
this usually fixes the encoding selection in Internet Explorer for this case.
