<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Error Message &#187; MySQL</title>
	<atom:link href="http://www.theerrormessage.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theerrormessage.com</link>
	<description>Fix your error</description>
	<lastBuildDate>Sun, 18 Jul 2010 03:21:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Updating multiple fields in multiple rows with different values in a single query in MySQL using the CASE operator does not supply a correct result</title>
		<link>http://www.theerrormessage.com/2010/07/updating-multiple-fields-in-multiple-rows-with-different-values-in-a-single-query-in-mysql-using-the-case-operator-does-not-supply-a-correct-result/</link>
		<comments>http://www.theerrormessage.com/2010/07/updating-multiple-fields-in-multiple-rows-with-different-values-in-a-single-query-in-mysql-using-the-case-operator-does-not-supply-a-correct-result/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 03:21:10 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/?p=205</guid>
		<description><![CDATA[Suppose we have a table with rows representing nodes of a tree structure, each node being represented by an id, a parent id and a position between the children of its parent node: CREATE TABLE `tree`.`node` ( `idnode` TINYINT NOT NULL AUTO_INCREMENT , `idnode_parent` TINYINT NULL , `position` TINYINT NULL , PRIMARY KEY ( `idnode` [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose we have a table with rows representing nodes of a tree structure, each node being represented by an id, a parent id and a position between the children of its parent  node:</p>
<p><code>CREATE TABLE  `tree`.`node` (<br />
`idnode` TINYINT NOT NULL AUTO_INCREMENT ,<br />
`idnode_parent` TINYINT NULL ,<br />
`position` TINYINT NULL ,<br />
PRIMARY KEY ( `idnode` )<br />
);</code></p>
<p>The nodes with <em>idnode_parent</em> set to 0 or NULL are on the first level of the tree structure.</p>
<p>Now we populate the table:</p>
<p><code>#add some first level nodes<br />
INSERT INTO `tree`.`node` VALUES (NULL, 0, 1); #idnode = 1<br />
INSERT INTO `tree`.`node` VALUES (NULL, 0, 2); #idnode = 2<br />
#add some children for the first node on the first level<br />
INSERT INTO `tree`.`node` VALUES (NULL, 1, 1); #idnode = 3<br />
INSERT INTO `tree`.`node` VALUES (NULL, 1, 2); #idnode = 4<br />
#add some children for the node with idnode 2<br />
INSERT INTO `tree`.`node` VALUES (NULL, 2, 1); #idnode = 5<br />
INSERT INTO `tree`.`node` VALUES (NULL, 2, 2); #idnode = 6<br />
INSERT INTO `tree`.`node` VALUES (NULL, 2, 3); #idnode = 7<br />
#add a child for the node with idnode 7<br />
INSERT INTO `tree`.`node` VALUES (NULL, 7, 1); #idnode = 8</code></p>
<p>Now the table looks like this:<br />
	idnode	idnode_parent	position<br />
		1		0		1<br />
		2		0		2<br />
		3		1		1<br />
		4		1		2<br />
		5		2		1<br />
		6		2		2<br />
		7		2		3<br />
		8		3		1<br />
		9		7		1</p>
<p>We want to delete:<br />
- node 1, so we move its children (3, 4) up one level (<em>idnode_parent</em> changes from 1 to 0) and update their positions to the position of their parent (<em>position</em> changes from 1, 2 respectively to 1);<br />
- node 7, so we move its children (8) up one level (<em>idnode_parent</em> changes from 7 to 2) and update their positions to the position of their parent (<em>position</em> changes from 1 to 3)</p>
<p>a) We try this query:<br />
<code>UPDATE node<br />
	SET idnode_parent = CASE(idnode_parent)<br />
			WHEN 1 THEN 0<br />
			WHEN 7 THEN 2<br />
			END,<br />
		position = CASE(idnode_parent)<br />
			WHEN 1 THEN 1<br />
			WHEN 7 THEN 3<br />
			END<br />
WHERE idnode_parent IN (1, 7);</code></p>
<p>Running this query, MySQL says 3 rows were affectedand the table looks as follows:</p>
<p>idnode	idnode_parent	position<br />
	1		0		1<br />
	2		0		2<br />
	3		0		NULL<br />
	4		0		NULL<br />
	5		2		1<br />
	6		2		2<br />
	7		2		3<br />
	8		3		1<br />
	9		2		NULL</p>
<p>We can see that only the field <em>idnode_parent</em> updated as expected, the <em>position</em> field for the moved nodes being set to NULL.</p>
<p>b) We reset the table to the initial test data and we run the above query with the difference of using the ELSE clause in the CASE expression:</p>
<p><code>UPDATE node<br />
	SET idnode_parent = CASE(idnode_parent)<br />
			WHEN 1 THEN 0<br />
			WHEN 7 THEN 2<br />
			ELSE idnode_parent<br />
			END,<br />
		position = CASE(idnode_parent)<br />
			WHEN 1 THEN 1<br />
			WHEN 7 THEN 3<br />
			ELSE position<br />
			END<br />
WHERE idnode_parent IN (1, 7);</code></p>
<p>The result is:</p>
<p>idnode	idnode_parent	position<br />
	1		0		1<br />
	2		0		2<br />
	3		0		1<br />
	4		0		2<br />
	5		2		1<br />
	6		2		2<br />
	7		2		3<br />
	8		3		1<br />
	9		2		1</p>
<p>So the <em>idnode_parent</em> field updates correctly, but <em>position</em> remains unchanged.</p>
<p>My consclusion is that that the query runs as if there were two different queries running over the bunch of rows resulted after evaluating the WHERE condition, the second CASE operator applying over the data modified by the first CASE operator:<br />
1. WHERE condition is evaluated; resulted data set:</p>
<p>idnode	idnode_parent	position<br />
	3		1		1<br />
	4		1		2<br />
	9		7		1</p>
<p>2. <em>idnode_parent</em> is set by evaluating the first CASE expression:</p>
<p>idnode	idnode_parent	position<br />
	3		0		1<br />
	4		0		2<br />
	9		2		1<br />
3. <em>position</em> is set by evaluating the second CASE expression:</p>
<p>a) Using the query without the ELSE condition, <em>position</em> is set to NULL for all the rows in the data set, as after running the first CASE expression there are no more rows with the <em>idnode_parent</em> needed for the second CASE expression:<br />
idnode	idnode_parent	position<br />
	3		0		NULL<br />
	4		0		NULL<br />
	9		2		NULL</p>
<p>b) Using the query with the ELSE condition, <em>position</em> remains unchanged for all the rows in the data set, as there are no more rows with the needed <em>idnode_parent</em>, as they were modified by applying the previous CASE operator, and the ELSE condition is evaluated:<br />
idnode	idnode_parent	position<br />
	3		0		1<br />
	4		0		2<br />
	9		2		1</p>
<p>So if we want the query to run correctly, we set the position first, and then the id of the parent node, as <em>position</em> does not modify data needed for subsequent <em>idnode_parent</em> updates in the same query:</p>
<p><code>UPDATE node<br />
	SET position = CASE(idnode_parent)<br />
			WHEN 1 THEN 1<br />
			WHEN 7 THEN 3<br />
			END,<br />
		idnode_parent = CASE(idnode_parent)<br />
			WHEN 1 THEN 0<br />
			WHEN 7 THEN 2<br />
			END<br />
WHERE idnode_parent IN (1, 7);</code></p>
<p>We finally obtain the expected result:<br />
idnode	idnode_parent	position<br />
	3		0		1<br />
	4		0		1<br />
	9		2		3</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2010/07/updating-multiple-fields-in-multiple-rows-with-different-values-in-a-single-query-in-mysql-using-the-case-operator-does-not-supply-a-correct-result/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Importing a MySQL data file using phpMyAdmin results in incomplete data in MySQL</title>
		<link>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/</link>
		<comments>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 16:56:42 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/08/17/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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\).<br />
So, after I exported the data from the old MySQL Server version I tried importing it to the MySQL 5.1 using phpMyAdmin.<br />
The file to import was 42MB in size, so I had to set some PHP configuration variables (in php.ini file) to greater values:<br />
<code>post_max_size = 64M<br />
upload_max_filesize = 64M</code></p>
<p>At first it looked like the phpMyAdmin import script entered a loop or something, as it wouldn&#8217;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&#8217;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.<br />
Then I thought the script needed even more time to run, so I increased the value of the PHP configuration variable <em>max_execution_time</em>, but with the same result.</p>
<p><strong>The solution</strong>. It seems that the execution time limit for the import script in phpMyAdmin is defined by the variable <em>$cfg['ExecTimeLimit']</em> in &lt;path_to_phpMyAdmin&gt;/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.<br />
You might also need to set the MySQL configuration variable <em>max_allowed_packet</em> (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, &#8216;mysql server has gone away&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/08/importing-a-mysql-data-file-using-phpmyadmin-results-in-incomplete-data-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysqldump &#8211; with exec() function from php outputs empty file</title>
		<link>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/</link>
		<comments>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 13:25:33 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysqldump]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2008/10/21/mysqldump-with-exec-function-from-php-outputs-empty-file/</guid>
		<description><![CDATA[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." &#62; backup.sql"; exec($command, $ret_arr, $ret_code); echo [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>So we have the following code:</p>
<p><code class="php">$command = "mysqldump --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</p>
<p>exec($command, $ret_arr, $ret_code);<br />
echo "ret_arr: &lt;br /&gt;";<br />
print_r($ret_arr);<br />
</code><br />
and we get an empty file and no output.</p>
<p>So we will fix this error in a few steps:</p>
<p>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).</p>
<p>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:</p>
<p><code class="php">$command = "mysqldump"; // mysqldump.exe on Windows</code></p>
<p>So execute the php script. It&#8217;s ok if you get output like this:</p>
<pre>Array
(
    [0] =&gt; Usage: mysqldump [OPTIONS] database [tables]
    [1] =&gt; OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
    [2] =&gt; OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
    [3] =&gt; For more options, use mysqldump --help
)</pre>
<p>If you don&#8217;t see something like that then you must check to see the path to the mysqldump command.</p>
<p>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:</p>
<p><code class="php">$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." &gt; backup.sql";</code></p>
<p>If append the right path to the command and you still cannot get the output then this article can&#8217;t help.</p>
<p>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&#8217;t have writing rights.</p>
<p>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:</p>
<p>In Windows:</p>
<p><code class="php">$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" &gt; backup.sql";</code></p>
<p>In Linux:</p>
<p><code class="php">$command = "mysqldump &gt; backup.sql";</code></p>
<p>After running the file &#8220;backup.sql&#8221; should  be created.</p>
<p>3.  You must now correct your statement. This means that we must <strong>use the long versions of the options</strong> like this:</p>
<p>Windows:</p>
<p><code class="php">$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." &gt; backup.sql";</code></p>
<p>Linux:</p>
<p><code class="php">$command = "mysqldump --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." &gt; backup.sql";</code></p>
<p>The fix: <strong>The options for mysqldump when called from php must be in the longer version.</strong> Instead of &#8211;u use &#8211;user, instead of &#8211;p use &#8211;password and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2008/10/mysqldump-with-exec-function-from-php-outputs-empty-file/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>#1005 &#8211; Can&#8217;t create table</title>
		<link>http://www.theerrormessage.com/2008/04/1005-cant-create-table/</link>
		<comments>http://www.theerrormessage.com/2008/04/1005-cant-create-table/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 14:17:45 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2008/04/11/1005-cant-create-table/</guid>
		<description><![CDATA[Full error message: #1005 &#8211; Can&#8217;t create table &#8216;.\my_db\#sql-7c4_444.frm&#8217;. Using MySQL version 4.1.22-community-nt, I created two tables (`table1` and `table2`) in a database (let&#8217;s call it `my_db`) as follows: CREATE TABLE `table1` ( `idtable1` int(10) unsigned NOT NULL auto_increment, `table1_str` varchar(50) NOT NULL default '', PRIMARY KEY (`idtable1`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `table2` [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message: <em>#1005 &#8211; Can&#8217;t create table &#8216;.\my_db\#sql-7c4_444.frm&#8217;.</em></p>
<p>Using MySQL version 4.1.22-community-nt, I created two tables (`table1` and `table2`) in a database (let&#8217;s call it `my_db`) as follows:<br />
<code class="mysql">CREATE TABLE `table1` (<br />
`idtable1` int(10) unsigned NOT NULL auto_increment,<br />
`table1_str` varchar(50) NOT NULL default '',<br />
PRIMARY KEY  (`idtable1`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</code></p>
<p><code class="mysql">CREATE TABLE `table2` (<br />
`idtable2` int(10) unsigned NOT NULL auto_increment,<br />
`idtable1_fk` int(10) NOT NULL,<br />
`table2_str` varchar(50) NOT NULL default '',<br />
PRIMARY KEY  (`idtable2`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</code><br />
Then I tried to add a foreign key constraint like this:<br />
<code class="mysql">ALTER TABLE `table2`<br />
ADD CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`idtable1_fk`) REFERENCES `table1` (`idtable1`) ON DELETE CASCADE ON UPDATE CASCADE;</code></p>
<p>And then I got the error.</p>
<p>The <strong>solution</strong> in my case was adding the <em>unsigned</em> attribute to the `idtable1_fk` field in `table2` for having the same type as the field `idtable1` it references in `table1`:</p>
<p><code class="mysql">ALTER TABLE `table2` CHANGE `idtable1_fk` `idtable1_fk` int(10) unsigned NOT NULL;</code></p>
<p>After that I ran the foreign key constraint query:<br />
<code class="mysql">ALTER TABLE `table2`<br />
ADD CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`idtable1_fk`) REFERENCES `table1` (`idtable1`) ON DELETE CASCADE ON UPDATE CASCADE;</code></p>
<p>And then it worked. But if you don&#8217;t have the same problem but you get the same error message you might already have a foreign key constraint with the given name. For example, if I run again the foreign key constraint query I will get the same error. Be careful if you use phpMyAdmin as a visual interface for MySQL, because it seems to me that it does not show us all the foreign key constraints we added to a table. If you want to see which foreign key constraints a table has, you could export the structure of that table and analyze the foreign key constraints queri(es).</p>
<p>Hope this works for you, too.</p>
<p><strong>Conclusion</strong>: We should make sure that the possible values of the foreign key field are in the same range as the possible values of the field referenced by that foreign key and that there is no foreign key constraint with the same name as the constraint we are trying to add.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2008/04/1005-cant-create-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use specific language characters with PHP and MySQL (example: Romanian)</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 00:54:15 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/13/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: <em>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</em>.</p>
<p>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).</p>
<p>If you already have the database, but with the default character set (<em>latin1</em>) and collation <em>(latin1_swedish_ci</em>) 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:</p>
<p><code class="mysql">ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;</code></p>
<p>If you don&#8217;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:</p>
<p><code class="mysql">CREATE TABLE `my_table` (<br />
`idmy_table` tinyint(3) unsigned NOT NULL auto_increment,<br />
`my_field` varchar(255) NOT NULL default '',<br />
PRIMARY KEY  (`idmy_table`)<br />
) CHARSET=utf8;</code></p>
<p>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 <code class="php">mysql_query("SET NAMES utf8", $my_conn);</code>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.</p>
<p>A simple example:<br />
<code class="html">&lt;?php<br />
$my_conn = @mysql_connect("localhost", "user", "pass")<br />
or die("There was a problem connecting to MySQL. Please try again later.");<br />
if(!@mysql_select_db("test", $my_conn))<br />
{<br />
die ("There was a problem connecting to the database. Please try again later.");<br />
}<br />
mysql_query("SET NAMES utf8", $my_conn);<br />
if(!empty($_GET['mystr']))<br />
{<br />
// insert the string into the database<br />
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");<br />
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";<br />
$result = mysql_query($query, $my_conn);<br />
if($result)<br />
{<br />
// save the id of the table row inserted<br />
$last_insert_id = mysql_insert_id($my_conn);<br />
// get the last inserted value<br />
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";<br />
$result = mysql_query($query, $my_conn);<br />
if($result &amp;&amp; $row = mysql_fetch_array($result, MYSQL_ASSOC))<br />
{<br />
$db_string = $row['my_field'] ;<br />
}<br />
}<br />
}<br />
elseif(!empty($_GET['searchstr']))<br />
{<br />
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");<br />
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";<br />
$result = mysql_query($query, $my_conn);<br />
if($result)<br />
{<br />
while($row = mysql_fetch_array($result, MYSQL_ASSOC))<br />
{<br />
$search_results[$row['idmy_table']] = $row['my_field'];<br />
}<br />
}<br />
}<br />
mysql_close($my_conn);<br />
?&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Page title&lt;/title&gt;<br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?<br />
if(!empty($db_string))<br />
{<br />
echo "&lt;strong&gt;Inserted string&lt;/strong&gt;: $db_string&lt;br /&gt;";<br />
}<br />
?&gt;<br />
&lt;form method="get" action=""&gt;<br />
String to insert into the database &lt;input type="text" name="mystr"/&gt;<br />
&lt;input type="submit" value="GO"/&gt;<br />
&lt;/form&gt;<br />
&lt;?<br />
if(!empty($search_results))<br />
{<br />
echo "&lt;strong&gt;Search results&lt;/strong&gt;:&lt;br /&gt;";<br />
foreach($search_results as $id =&gt; $value)<br />
{<br />
echo $value."&lt;br /&gt;";<br />
}<br />
}<br />
?&gt;<br />
&lt;form method="get" action=""&gt;<br />
Search query &lt;input type="text" name="searchstr"/&gt;<br />
&lt;input type="submit" value="GO"/&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code></p>
<p>Tip: The search in Romanian language over the database (tested with MySQL <strong>LIKE</strong> operator) works like a charm when searching words that have special characters or not.</p>
<p>For example: In Romanian language the word &#8220;peasant&#8221; is written as &#8220;ţăran&#8221; and someone who searches it gets the same result for the search terms &#8220;taran&#8221; or &#8220;ţăran&#8221; or &#8220;ţaran&#8221; or &#8220;tărân&#8221; and so on &#8211; so this is the real magic.</p>
<p><strong>UPDATE</strong>: You may also need to add a header to the php script if you use ob_start or similar php functions like this: <code class="php">header("Content-type: text/html; charset=UTF-8");</code> this usually fixes the encoding selection in Internet Explorer for this case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8</title>
		<link>http://www.theerrormessage.com/2007/08/orghibernatehibernateexception-identifier-of-an-instance-of-membersappointment-was-altered-from-8-to-8/</link>
		<comments>http://www.theerrormessage.com/2007/08/orghibernatehibernateexception-identifier-of-an-instance-of-membersappointment-was-altered-from-8-to-8/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 22:14:45 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/08/06/orghibernatehibernateexception-identifier-of-an-instance-of-membersappointment-was-altered-from-8-to-8/</guid>
		<description><![CDATA[Full error message: Exception occurred during event dispatching: org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8 at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58) at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at members.AppointmentDialog.jButtonSaveActionPerformed(AppointmentDialog.java:279) at members.AppointmentDialog.access$000(AppointmentDialog.java:21) at members.AppointmentDialog$1.actionPerformed(AppointmentDialog.java:101) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message:</p>
<p><code>Exception occurred during event dispatching:<br />
org.hibernate.HibernateException: identifier of an instance of members.Appointment was altered from 8 to 8<br />
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)<br />
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:157)<br />
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)<br />
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)<br />
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)<br />
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)<br />
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)<br />
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)<br />
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)<br />
at members.AppointmentDialog.jButtonSaveActionPerformed(AppointmentDialog.java:279)<br />
at members.AppointmentDialog.access$000(AppointmentDialog.java:21)<br />
at members.AppointmentDialog$1.actionPerformed(AppointmentDialog.java:101)<br />
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)<br />
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)<br />
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)<br />
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)<br />
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)<br />
at java.awt.Component.processMouseEvent(Component.java:5517)<br />
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)<br />
at java.awt.Component.processEvent(Component.java:5282)<br />
at java.awt.Container.processEvent(Container.java:1966)<br />
at java.awt.Component.dispatchEventImpl(Component.java:3984)<br />
at java.awt.Container.dispatchEventImpl(Container.java:2024)<br />
at java.awt.Component.dispatchEvent(Component.java:3819)<br />
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)<br />
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)<br />
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)<br />
at java.awt.Container.dispatchEventImpl(Container.java:2010)<br />
at java.awt.Window.dispatchEventImpl(Window.java:1791)<br />
at java.awt.Component.dispatchEvent(Component.java:3819)<br />
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)<br />
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)<br />
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)<br />
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)<br />
at java.awt.Dialog$1.run(Dialog.java:535)<br />
at java.awt.Dialog$2.run(Dialog.java:563)<br />
at java.security.AccessController.doPrivileged(Native Method)<br />
at java.awt.Dialog.show(Dialog.java:561)<br />
at java.awt.Component.show(Component.java:1302)<br />
at java.awt.Component.setVisible(Component.java:1255)<br />
at admin.AdminOptionsForm.jButtonSetAppointmentActionPerformed(AdminOptionsForm.java:238)<br />
at admin.AdminOptionsForm.access$800(AdminOptionsForm.java:24)<br />
at admin.AdminOptionsForm$9.actionPerformed(AdminOptionsForm.java:145)<br />
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)<br />
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)<br />
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)<br />
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)<br />
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)<br />
at java.awt.Component.processMouseEvent(Component.java:5517)<br />
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)<br />
at java.awt.Component.processEvent(Component.java:5282)<br />
at java.awt.Container.processEvent(Container.java:1966)<br />
at java.awt.Component.dispatchEventImpl(Component.java:3984)<br />
at java.awt.Container.dispatchEventImpl(Container.java:2024)<br />
at java.awt.Component.dispatchEvent(Component.java:3819)<br />
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)<br />
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)<br />
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)<br />
at java.awt.Container.dispatchEventImpl(Container.java:2010)<br />
at java.awt.Window.dispatchEventImpl(Window.java:1791)<br />
at java.awt.Component.dispatchEvent(Component.java:3819)<br />
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)<br />
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)<br />
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)<br />
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)<br />
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)<br />
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)</code></p>
<p>In Java, I had a class Appointment mapped to a MySQL table `appointment` using Hibernate as follows:</p>
<p><code class="html">&lt;hibernate-mapping&gt;<br />
&lt;class name="members.Appointment" table="appointment"&gt;<br />
&lt;id name="idAppointment" column="idappointment" type="short"&gt;<br />
&lt;generator class="native"/&gt;<br />
&lt;/id&gt;<br />
&lt;many-to-one name="member" column="idmember" not-null="true" foreign-key="fk_idmember"/&gt;<br />
&lt;property name="startDate" type="timestamp" column="start_date"/&gt;<br />
&lt;property name="duration" type="short" column="duration" length="3"/&gt;<br />
&lt;property name="cancelled" type="boolean"/&gt;<br />
&lt;/class&gt;<br />
&lt;/hibernate-mapping&gt;</code></p>
<p>I was getting the error when inserting a new appointment into the database.</p>
<p>The problem was that the idAppointment member variable in the Java class and the idappointment field in the MySQL database table were of the type int. So why the hell did I have the type short in the mapping? The answer is simple: because of the copy paste. So the correct code is:</p>
<p><code class="html">&lt;id name="idAppointment" column="idappointment"&gt;<br />
&lt;generator class="native"/&gt;<br />
&lt;/id&gt;<br />
</code></p>
<p><strong>Conclusion</strong>: copy paste is sometimes evil.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/08/orghibernatehibernateexception-identifier-of-an-instance-of-membersappointment-was-altered-from-8-to-8/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>java.sql.SQLException: Cannot convert value &#8217;0000-00-00 00:00:00&#8242; from column 9 to TIMESTAMP</title>
		<link>http://www.theerrormessage.com/2007/07/javasqlsqlexception-cannot-convert-value-0000-00-00-000000-from-column-9-to-timestamp/</link>
		<comments>http://www.theerrormessage.com/2007/07/javasqlsqlexception-cannot-convert-value-0000-00-00-000000-from-column-9-to-timestamp/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 00:14:25 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/07/26/javasqlsqlexception-cannot-convert-value-0000-00-00-000000-from-column-9-to-timestamp/</guid>
		<description><![CDATA[Full error message: Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.loader.Loader.doList(Loader.java:2223) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) at org.hibernate.loader.Loader.list(Loader.java:2099) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338) at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) at admin.MembersTableModel.&#60;init&#62;(MembersTableModel.java:43) at admin.AdminOptionsForm.initComponents(AdminOptionsForm.java:98) at admin.AdminOptionsForm.&#60;init&#62;(AdminOptionsForm.java:32) at managemembers.Main.main(Main.java:45) Caused by: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP. [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message:</p>
<p><code>Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query<br />
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)<br />
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)<br />
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)<br />
at org.hibernate.loader.Loader.doList(Loader.java:2223)<br />
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)<br />
at org.hibernate.loader.Loader.list(Loader.java:2099)<br />
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)<br />
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)<br />
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)<br />
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)<br />
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)<br />
at admin.MembersTableModel.&lt;init&gt;(MembersTableModel.java:43)<br />
at admin.AdminOptionsForm.initComponents(AdminOptionsForm.java:98)<br />
at admin.AdminOptionsForm.&lt;init&gt;(AdminOptionsForm.java:32)<br />
at managemembers.Main.main(Main.java:45)<br />
Caused by: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP.<br />
at com.mysql.jdbc.ResultSet.getTimestampFromBytes(ResultSet.java:6864)<br />
at com.mysql.jdbc.ResultSet.getTimestampInternal(ResultSet.java:6899)<br />
at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:6218)<br />
at com.mysql.jdbc.ResultSet.getTimestamp(ResultSet.java:6256)<br />
at com.mchange.v2.c3p0.impl.NewProxyResultSet.getTimestamp(NewProxyResultSet.java:3394)<br />
at org.hibernate.type.TimestampType.get(TimestampType.java:30)<br />
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)<br />
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)<br />
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)<br />
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)<br />
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)<br />
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)<br />
at org.hibernate.loader.Loader.getRow(Loader.java:1206)<br />
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)<br />
at org.hibernate.loader.Loader.doQuery(Loader.java:701)<br />
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)<br />
at org.hibernate.loader.Loader.doList(Loader.java:2220)<br />
... 11 more</code></p>
<p>My example:</p>
<p>// the Java class</p>
<p><code class="java">public class Member  {<br />
// some private member attributes<br />
private Date addedDate;<br />
// proper getter and setter methods<br />
}</code></p>
<p>&lt;!&#8211; the Hibernate mapping&#8211;&gt;</p>
<p><code class="html">&lt;hibernate-mapping&gt;<br />
&lt;class name="managemembers.Member" table="member"&gt;<br />
&lt;!-- some mappings here--&gt;<br />
&lt;property name="addedDate" type="timestamp" column="added_date"/&gt;<br />
&lt;/class&gt;<br />
&lt;/hibernate-mapping&gt;</code></p>
<p># The MySQL table</p>
<p><code class="mysql">CREATE TABLE `member` (<br />
# some fields here<br />
`added_date` DATETIME,<br />
) ;</code></p>
<p>So I was mapping a java.util.Date field in the POJO to a MySQL DATETIME field.</p>
<p>The error occured while I was trying to retrieve the data from the member database table.</p>
<p>The problem was that I had DATETIME values in the database with all-zero components (0000-00-00 &#8230;), so the solution was to set all the added_date field values with all-zero components to NULL and to be careful to set a valid DATETIME value or NULL into the added_date field each time I inserted a new row.</p>
<p><strong>Conclusion</strong>: You have 2 choices: either set the date field in the table to a NULL value either set it to a valid date.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/07/javasqlsqlexception-cannot-convert-value-0000-00-00-000000-from-column-9-to-timestamp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
