View Full Version : PHP with AJAX

06-07-2007, 05:30 PM
Hi guys,

new to the forum and new to AJAX.
I have downloaded the examples from W3C for PHP with AJAX where the index.php file calls a .js file that in turns calls a .php file that queries a db and returns an xml file.

I understand all this but my question is concerning globally defined variables.
I define my db connections in a config file - HOST,USERNAME, PASSWORD, MYSQL_DB etc.
When I try and require the config file into responsexml.php file it falls over and does not recognise the required file. When I use the satndard calls for a db connection it works fine.

If anyone can help I would be very grateful.


06-07-2007, 05:54 PM
So are you making the db connection inside the config file, or are you just using the variables from the config file? Are you trying to use those variables inside a function? Are you passing the variables to the function, or declaring the variables as global using the GLOBAL directive?

06-08-2007, 09:41 AM
I have defined the connection variables in the config.php file:

define('MYSQL_HOST', '********');
define('MYSQL_USER', '********');
define('MYSQL_PASS', '********');
define('MYSQL_DB', '********');

I am using .htaccess to automatically include the config.php file:

php_value auto_prepend_file "config.php"

The responsexml.php file called from the ajax .js page is:

header('Content-Type: text/xml');
$con = mysql_connect('localhost', 'apachewrite', 'WHPWBZvX');
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("newcreation", $con);

$sql="SELECT * FROM user WHERE ID = ".$q."";

$result = mysql_query($sql);

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
while($row = mysql_fetch_array($result))
echo "<firstname>" . $row['first_name'] . "</firstname>";
echo "<lastname>" . $row['last_name'] . "</lastname>";
echo "<age>" . $row['age'] . "</age>";
echo "<hometown>" . $row['hometown'] . "</hometown>";
echo "<job>" . $row['job'] . "</job>";
echo "</person>";

This works as it stands but when i replace the mysql_connect parameters with the global variables MYSQL_HOST,MYSQL_USER,MYSQL_PASS it fails.

Does .htaccess prepend the config to ALL php files or only those that are directly called ie /index.php?


mr e
06-08-2007, 10:12 AM
Try manually including it in the responsexml.php page and see if it works, that would answer your question about auto prepend, I'm not sure of the actual answer though, but try it and see what happens

06-08-2007, 10:19 AM
I don't know what i was doing before but i tried it again and it is now working fine.

$con = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db(MYSQL_DB, $con);

So there you go!
My code above was just the example given on the W3C ajax/php pages.
I am planning on using the ajax to create pages with paragraph content displaying paragraphs one at a time - with back and forward buttons. Don't ask me why as the client wants it to happen!

The site is http://aegeandreamproperty.com and should be going live sometime next week.