Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2006
    Posts
    3

    Unanswered: Trying to connect to MySQL using PHP

    Hi,

    First post.

    I'm a total PHP newbie. I'm trying to get a simple db up and running so I can submit a form and take people's email addresses to be on my mailing list.

    Here's the deal:

    I have two files, one db_login.php with that looks like this:
    <?php
    $db_host="mysql213.secureserver.net";
    $db_database="mydbname";
    $db_username="myusername";
    $db_password="mypassword";
    ?>

    That php file lives in the same folder (root/html) as the following file (called phptest.php:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>New Page 1</title>
    </head>
    <body>
    <p>&nbsp;</p>
    <p>
    <?php
    include(db_login.php);
    $connection=mysql_connect($db_host,$db_database,$d b_password);
    if (!$connection) {
    die ("Could not connect to the db");
    }
    ?>
    </body>
    </html>

    When I browse to phptest.php I get the following error:
    Warning: main(db_loginphp): failed to open stream: No such file or directory in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

    Warning: main(db_loginphp): failed to open stream: No such file or directory in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

    Warning: main(): Failed opening 'db_loginphp' for inclusion (include_path='.:/usr/local/lib/php') in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 13

    Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/content/j/o/h/johnnybregar/html/html/phptest.php on line 14
    Could not connect to the db

    Is my PHP totally messed up? Or are these directories insane? I'm lost and could use a helping hand to get up and running. Nowhere do I have a directory structure like what it is looking for....

    Thanks,

    Johnny B.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    where is your file db_login?... that is one cause for problems, or alternatively where are the other 3 lines, as PHP is reporting as the inlcude is on iline 13 yet PHP reports the fault on 10


    if you are concerned that you installation isnt working then Id suggest you run a simple testscript calling the phpinfo() function

    eg
    <?php
    phpinfo(-1)
    ?>

    the phpinfo should provide a lot of bumpf, not least of which is the database server the webserver is connected to

    Im not sure you webserver is correclty specified
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    The include(...) is expecting a string as a parameter for the filepath/filename. Try - include('db_login.php');

  4. #4
    Join Date
    Mar 2004
    Posts
    480
    try hardcoding the inofrmation you have in that file directly in your php file. see if everything connects properly. if it does then the problem is not with mysql and php connecting but with your php code including the file.

  5. #5
    Join Date
    Dec 2006
    Posts
    3
    OK. Thanks for the suggestions. By hardcoding the db connection, I got rid of the errors. There may be something up with my php.ini file, pointing to a different include dir. Not sure. Don't care.

    Now I simply need help with my form submittal - I'm not seeing any rows get added by my db with the following code:

    <?php
    $hostname="mysql213.secureserver.net";
    $username="username";
    $password="password";
    $dbname="dbname";
    $usertable="mailinglist";
    mysql_connect($hostname,$username,$password);
    mysql_select_db($dbname);
    $FirstName = $_GET[‘FirstName’];
    $LastName = $_GET[‘LastName’];
    $EmailAddress = $_GET[‘EmailAddress’];
    ?>
    <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method=GET />
    <p>First Name: <input type="text" name="FirstName" size="27" value="<?php echo $FirstName; ?>"><br>
    Last Name: <input type="text" name="LastName" size="27" value="<?php echo $LastName; ?>"><br>
    Email: <input type="text" name="EmailAddress" size="49" value="<?php echo $EmailAddress; ?>"></p>
    <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>
    <?php
    mysql_query("INSERT INTO 'mailinglist' (FirstName,LastName,EmailAddress) VALUES ($FirstName,$LastName,$EmailAddress)");
    ?>

    Any thoughts on why my data is not showing up? Have I screwed something up?

    Thank you all so much for the help here...

  6. #6
    Join Date
    Dec 2006
    Posts
    3
    Figured it out.

    Among other things, it didn't like the single quotes around my field names in my $_GET['fieldname']. Seems like there are a lot of examples out there that use these single quotes. Not sure why I couldn't use them.

    Oh well, I got data now. That's good.

    jb

  7. #7
    Join Date
    Mar 2004
    Posts
    480
    jb, just to tidy your code up, can you use tags around it please (they'll work on other sites you visit as well most of the time).

    remove the spaces in the first of the two tags.

    For wrapping php use [ php] and [/php]
    for wrapping sql code alone use [ code] and [/code]

    One last thing and this is important, take the time to read the php manual on using mysql_real_escape_string on your variables that are coming in from a user (with either GET or POST variables). Doing it the way you currently are is leaving your database vulnerable to injection attacks.

Posting Permissions

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