May 3, 2010
Today I ran into this problem for the second time in three years… so it took me a while to figure out the solution (with the help of my trusty partners here in the office of course).
It’s quite a common error message :
SQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails
Usually the foreign key data in the query is either blank or references a non-existing record, but in my case everything was fine with the data. Stranger yet, the same code worked on my localhost and only threw this error on the live server.
First I looked at the query itself, which was fine. Then I compared the foreign key definitions with the one in the parent table, and it was identical. After that I asked for some suggestions, and somebody provided the correct answer.
March 18, 2010
This is something that a number of people I know have struggled with, so I hope to help out with what I have learned.
To connect your PHP program with MySQL is probably step two for any PHP developer and it’s very easy. In principle, connecting with SQL Server (Microsoft SQL) should also be easy, but when most people have their first go at a MSSQL connection, they just get the following:
SQLSTATE unable to connect:
sql server is unavailable or does not exist.
The thing is, PHP and it’s MySQL buddy are both free and they like each other, so if you install a new Apache server (or similar) its already set up to run MySQL connections without problem, but it’s not the same for MSSQL.
Here is a list of things you need to change in your setup to be able to open a MSSQL connection with PHP:
February 19, 2009
According to the MySQL manual
Full-text indexes can be used only with MyISAM tables
I’m using an InnoDB table-type however, so had to look at other ways of implementing a full-text search that would give me rankings by relevance, to order the results by.
It’s possible to do the ranking with PHP, by doing a
LIKE '%term%' MySQL query and then doing a substring count for each search term, but I believe that doing it via MySQL would be faster, and way cooler.
February 19, 2009
I was setting up a database query using a PDOStatement, by looping through an array of values and binding each parameter seperately. But on executing the query, I got the following error:
Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`places/places`, CONSTRAINT `places_ibfk_3` FOREIGN KEY (`login_id`) REFERENCES `login` (`id`) ON DELETE CASCADE)’ in G:….