Getting PHP to talk to MySQL, worked on the course but at home….
Posted by testcrunch on 16th November 2010
On the PHP/MySQL course last weekend we wrote a very simple page to read some user input – name – and update a database with it. Can’t get much simpler than that except that that was the first time I had ever written any PHP to update a database.
Anyway I gave it a go and using the lecturers displayed example I keyed that in and butchered that to write to my own database. I did manage to get the thing to work. Once. The second time I ran it I got some db duplicate problem and that was caused by me not setting an increment setting on the database id field. Without that being set the second record was trying to be inserted, not updated, with the same non-incremented id.
On Sunday I thought I’d better see if I could get the same code, which I’d saved on a USB stick, working from home. Did it work? Did it heck. The first page was form.php which displayed the name field for the user to enter data into and a submit control. When that was clicked on control should have passed to the addme.php page which in turn called _dbconnect.php, to connect to the database and then control was returned to addme.php to actually insert the data into the database and finally display a confirmation message.
So what happened? I started XAMPP and then Apache and MySQL and the form.php page loaded OK but entering any data and clicking on submit resulted in either an error page or squat happening. Think, think. Oh yeah, I might have MySQL installed and running but I hadn’t created a database like the one on the course. I also noticed that form.php was looking for addme.php on the H: drive, which is where it was on the course PC, and it should have been the C: drive.
I amended that to get it to point at my htdocs folder and created a database with the same name as the addme.php expected. Created the one table in it that it wanted, with a couple of columns and reran form.php. Again nada.
I stuck a (PHP) print_r into the first line of the addme.php page to see if it was even started and what was displayed was ‘array()’. Looked kinda empty to me. Or is that because the print_r() command moves the array pointer to the end and therefore there was nothing to display in the array? Apparently using reset() will move the pointer back to the beginning. Question is what was the form or what parameters did I apply to the print_r command that I unleashed.
Jeez, I’m hacking like a lunatic.
Posted in PHP - what does that stand for again? | No Comments »
