Page 1 of 1

XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 29. December 2012 07:51
by cover
I downloaded and successfully installed XAMPP 1.8.1 on my Windows 7 64bit machine recently. My question is that 'occasionally', I seem to write to the MySQL database but upon running a query, the entry isn't there. It only happens now and then and usually, after I've already successfully written something to the database as in, on the second or third attempt.

Here's some of my PHP code:

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
mysql_select_db($DBname) or die("Unable to select database $DBname");
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database.';
exit;
}
$sqlquery = "INSERT INTO $table VALUES('$id', '$event', '$date', '$name', '$year')";
$results = mysql_query($sqlquery);
mysql_close();

Oddly, no errors and the write always appears successful.

Some specifics on how I have the MySQL db table set up:
input text fields with a varchar type (except ID which is int). ID is primary key and default is 'latin1' with 'latin1_swedish_ci' collation. In matching another XP WAMP system that has worked flawlessly, I set the engine to MyISAM

Any ideas as to why it would only write to the database occasionally anyone?

TIA

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 29. December 2012 15:35
by cover
I'm inclined to think that my PHP code is okay so am wondering if my database setup configuration isn't to blame for the fact that it's a 'part-timer'.

I changed the engine to InnoDB, character set to UTF8, and collation to utf8_general_ci and it's doing the same thing. Appearing to have written just fine but upon a query, data isn't there. Other times it's fine.

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 29. December 2012 16:09
by JJ_Tagy
Without your full code, I'm not inclinded to think so. How do you get your variables? Are they always valued? Have you put debug code in to show each entry (or entry attempt)?

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 29. December 2012 16:16
by cover
variables pass from one page to the next (entry form to insert form which shows what was entered into the database). On the WAMP installation, the PHP and MySQL are dated but I can't recall what version. On the XAMPP, both are more recent which caused me to wonder if my PHP connect, select, etc etc might be keeping it from working 100% of the time. Odd thing is it shows as the write being successful.

Currently using:
<?php
require_once('generic_connect.php');
$DBname = "events";
$table = "events_tbl";

$id = $_POST['id'];
$event = $_POST['event'];
$date = $_POST['date'];
$name = $_POST['name'];
$year = $_POST['year'];
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
mysql_select_db($DBname) or die("Unable to select database $DBname");

$sqlquery = "INSERT INTO $table VALUES('$id', '$event', '$date', '$name', '$year')";
$results = mysql_query($sqlquery);
mysql_close();

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 29. December 2012 16:51
by hackattack142
You could try adding something like
Code: Select all
$results = mysql_query($sqlquery);
if (mysql_errno())
echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$sqlquery\n<br>";
to see if any errors are coming as a result of the query

You could also try using the mysqli extension

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 30. December 2012 22:45
by cover
Thanks for the ideas. I'll have to do some digging too, to see if there's a more modern way to write the code I posted (with the newer PHP and My SQL).

Re: XAMPP PHP/MySQL/APACHE Question v1.8.1

PostPosted: 31. December 2012 04:06
by Altrea
cover wrote:if there's a more modern way to write the code I posted

Sure, there are :D
Take some looks into MySQLi or my favorite PDO.

There are even more advanced techniques like ORM and ActiveRecord Pattern, like in Idiorm and Paris, Propel or the very famous Doctrine.

best wishes,
Altrea