Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: PHP with AJAX

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts

    PHP with AJAX

    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.

    Regards,
    D.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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?

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    I have defined the connection variables in the config.php file:
    Code:
    define('MYSQL_HOST', '********');
    define('MYSQL_USER', '********');
    define('MYSQL_PASS', '********');
    define('MYSQL_DB', '********');
    I am using .htaccess to automatically include the config.php file:
    Code:
    php_value auto_prepend_file "config.php"
    The responsexml.php file called from the ajax .js page is:
    PHP Code:
    <?php
    header
    ('Content-Type: text/xml');
    $q=$_GET["q"];
    $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"?>
    <person>'
    ;
    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>";
    mysql_close($con);
    ?>
    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?

    D.

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    I don't know what i was doing before but i tried it again and it is now working fine.

    PHP Code:
    $con mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_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.

    D.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •