SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Connection Problems

    Hi there,
    I'm a total newbie to both PHP and MySQL, so I'm hoping one of you guys can help me out
    Basically, I cannot connect to a table in my database. Here is the MySQL info...



    ...and the code I'm using...

    PHP Code:
    <?php

    $dbcnx 
    = @mysql_connect('localhost''twiddles_board''mypassword');
    if (!
    $dbcnx
        {
            echo 
    'Unable to connect to the database server. Please try again later.';
            exit();
        }


    if (!@
    mysql_select_db('tws_mailing_list'))
        {
            exit (
    'Unable to connect to the mailing list. Please try again later.');
        }
    ?>
    The webpage keeps saying
    Unable to connect to the mailing list. Please try again later.
    Any ideas guys? I'm getting this from one of the Sitepoint books (Build Your Own Database Driven Website Using PHP & MySQL) and I'm just trying to create a simple mailing list.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    mysql_select_db('tws_mailing_list'$dbcnx); 
    You need to include the connection # when selecting a database

  3. #3
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ruben K.
    PHP Code:
    mysql_select_db('tws_mailing_list'$dbcnx); 
    You need to include the connection # when selecting a database
    Thanks for your reply Ruben K, but unfortunatley, it didn't work
    Any other ideas? I can connect to the database fine, it's just connecting to the table tws_mailing_list that is the problem.

  4. #4
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Twiddlestick
    Thanks for your reply Ruben K, but unfortunatley, it didn't work
    Any other ideas? I can connect to the database fine, it's just connecting to the table tws_mailing_list that is the problem.
    Are you connecting to a table? You need to connect to a database.
    After connecting to a database you can simply use queries to fetch data from a table.

    Code:
    SELECT * FROM tws_mailing_list

  5. #5
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have already connected to the database, I'm trying to access the table within the database - I want to create a simple form that allows a visitor to enter their name and email address, and have that inserted into the database.

  6. #6
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would be something like this:

    PHP Code:
    <?php
    if( isset( $_POST['submit'] ) )
    {
        
    mysql_query("INSERT INTO tws_mailing_list VALUES('{$_POST['data1']}','{$_POST['data2']}')");
    }
    else
    {
        
    ?>
            form here
        <?php    
    }
    ?>

  7. #7
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure how to link the text boxes in the form to the fields of the sql database
    e.g. What's entered into 'name' on the form is inserted into 'm_name' on the sql.

    (Sorry, I really am new to all this)

  8. #8
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to select twiddles_board as the database, not the table (which is tws_mailing_list).

    You might want to do this instead:

    PHP Code:
    <?php

    # Connect
    $dbcnx = @mysql_connect('localhost''twiddles_board''mypassword');
    if (!
    $dbcnx)
        {
            echo 
    'Unable to connect to the database server. Please try again later.';
            exit();
        }

    # Select our database
    if (!@mysql_select_db('twiddles_board'))
        {
            exit (
    'Unable to connect to the mailing list. Please try again later.');
        }

    # If someone submitted the form
    if (isset($_GET['name']) && isset($_GET['email']))
        {
            
    # Construct the query
            
    $query "INSERT INTO tws_mailing_list (m_name, m_email) VALUES ('";
            
    # Add the escaped name
            
    $query .= mysql_real_escape_string($_GET['name']);
            
    $query .= "', '";
            
    # Add the escaped email
            
    $query .= mysql_real_escape_string($_GET['email']);
            
    # Finish the query
            
    $query .= "');";
            
    # Execute the query
            
    $sql mysql_query($query);
            
    # Get mysql_error in a variable,
            # The empty function can't rely on the return value of an other function
            
    $mysql_error mysql_error();
            
    # Make sure there are no errors
            
    if(!empty($mysql_error))
                exit(
    'There was an error in the MySQL syntax: ' $mysql_error);
            
    # If were still running, output our sucess message
            
    exit('Hi ' $_GET['name'] . ', you have successfully added the your email address, ' $_GET['email'] . ', to our mailing list');
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
     <head>
      <title>Twiddles Board - Subscribe to our Mailing List</title>
     </head>
     <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
       Name: <input type="text" id="name" name="name" />
       <br />
       Email: <input type="text" id="email" name="email" />
       <br />
       <input type="submit" value="Subscribe" />
      </form>
     </body>
    </html>
    You can style up the form and error/sucess messages
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  9. #9
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still no luck unfortunately. I've had enough now, I've been trying to figure it out for about 4 hours now and I can't even get a simple form to add data to a database. (The word useless springs to mind.) Maybe I'll try again later.

  10. #10
    SitePoint Zealot metacube's Avatar
    Join Date
    Jun 2005
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, bjcffnet posted the solution:

    mysql_select_db('tws_mailing_list') is wrong, as tws_mailing_list is a table, and not a database.

    mysql_select_db('twiddles_board') is indeed what you need.

    Try it, it'll work

  11. #11
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like according to your screenshot, is Database is twiddles_board, and the username might be something else

    PHP Code:
    $dbcnx = @mysql_connect('localhost''twiddles_board''mypassword'); 
    We don't know if the above is correct from the information submitted.

    PHP Code:
    if (!@mysql_select_db('tws_mailing_list')) 
    You are selecting the wrong Database, it should be
    PHP Code:
    if (!@mysql_select_db('twiddles_board')) 
    Good Luck!

  12. #12
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 

    # Connect 
    $dbcnx = @mysql_connect('localhost''twiddles_board''mypassword'); 
    if (!
    $dbcnx
        { 
            echo 
    'Unable to connect to the database server. Please try again later.'
            exit(); 
        } 

    # Select our database 
    if (!@mysql_select_db('twiddles_board')) 
        { 
            exit (
    'Unable to connect to the mailing list. Please try again later.'); 
        } 

    # If someone submitted the form 
    if (isset($_GET['name']) && isset($_GET['email'])) 
        { 
            
    # Construct the query 
            
    $query "INSERT INTO tws_mailing_list (m_name, m_email) VALUES ('"
            
    # Add the escaped name 
            
    $query .= mysql_real_escape_string($_GET['name']); 
            
    $query .= "', '"
            
    # Add the escaped email 
            
    $query .= mysql_real_escape_string($_GET['email']); 
            
    # Finish the query 
            
    $query .= "');"
            
    # Execute the query 
            
    $sql mysql_query($query); 
            
    # Get mysql_error in a variable, 
            # The empty function can't rely on the return value of an other function 
            
    $mysql_error mysql_error(); 
            
    # Make sure there are no errors 
            
    if(!empty($mysql_error)) 
                exit(
    'There was an error in the MySQL syntax: ' $mysql_error); 
            
    # If were still running, output our sucess message 
            
    exit('Hi ' $_GET['name'] . ', you have successfully added the your email address, ' $_GET['email'] . ', to our mailing list'); 
        } 
    ?> 

      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> 
       Name: <input type="text" id="name" name="name" /> 
       <br /> 
       Email: <input type="text" id="email" name="email" /> 
       <br /> 
       <input type="submit" value="Subscribe" /> 
      </form>
    This is what I copied and pasted into Dreamweaver and uploaded onto the server. Looking at the code and your replies, you are saying that...
    PHP Code:
    # Select our database 
    if (!@mysql_select_db('twiddles_board')) 
        { 
            exit (
    'Unable to connect to the mailing list. Please try again later.'); 
        } 
    ...is wrong, even though what you suggested is what I put in there before my last post, and it didn't work. :'(

    @ medicjoe95:
    My username and database are both twiddles_board.

  13. #13
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, try this. A little more clean in my opinion:

    PHP Code:
    //---------- [ Connect to DB ] ----------\\

    $dbUser 'twiddles_board';           
    $dbPass 'YOUR_PASSWORD';
    $dbName 'twiddles_board';

    mysql_connect ("localhost""$dbUser""$dbPass");
    mysql_select_db ("$dbName"); 
    Good Luck, Again. If the error still pertains etc, then I would actually check the username, make sure the user (twiddles_board) actually is the user account, with the database. If you are using Cpanel - You would have entered "board" for the username, then "SOMETHING" for the pass. Then, you would have created the database called "board" and then combinded them at the top of my MySQL Page. If you are using something else, then I am suspecting your not completely a "newbie" =P

  14. #14
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is annoying me now, lol, that didn't work either.
    And in answer to the cPanel point - yes, I am a newbie
    I double checked my username and password, and they are both correct, so I'm not sure what the problem is - but there obviously is a problem somewhere.

  15. #15
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I've decided to start this again from the beginning.

    I've changed the structure of the database:

    DB_NAME = twiddles_database
    DB_USER = twiddles_user
    TABLE_NAME = members

    Hopefully, that will make life a little easier

    I have also rewritten the code taking on board a few of your suggestions, and for now, am trying to focus just on selecting the database "twiddles_database".

    Here is what I have so far...

    PHP Code:
    <?php

    // CONNECT TO THE DATABASE SERVER

    $dbUser 'twiddles_user';            
    $dbPass 'password'
    $dbName 'twiddles_database'

    $dbcnx = @mysql_connect ("localhost""$dbUser""$dbPass"); 

    if (!
    $dbcnx)
        {
            exit (
    'We are unable to connect to the server. Please try again later.');
        }

    // SELECT THE CORRECT DATABASE

    if (!@mysql_select_db ("$dbName"));
        {
            exit (
    'We are unable to locate the database. Please try again later.');
        } 

    ?>
    I first ran the code just with the first bit (i.e. Connecting to the database server) and this worked fine.
    However, when I added the second bit (i.e. Selecting the database), it produced the error message "We are unable to locate the database. Please try again later."

    In my opinion, I've found where the error is, but I don't know what is causing it. Do you guys have any idea?

  16. #16
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?php

    // CONNECT TO THE DATABASE SERVER

    $dbUser 'twiddles_user';            
    $dbPass 'password';
    $dbName 'twiddles_database';

    $dbcnx mysql_connect ("localhost""$dbUser""$dbPass");

    if (!
    $dbcnx)
        {
            echo 
    mysql_error();
            exit (
    'We are unable to connect to the server. Please try again later.');
        }

    // SELECT THE CORRECT DATABASE

    if (!mysql_select_db($dbName));
        {
            echo 
    mysql_error();
            exit (
    'We are unable to locate the database. Please try again later.');
        }

    ?>
    Contact: opserty@gmail.com
    Register your portfolio at: http://portfoliodb.net

  17. #17
    SitePoint Member Twiddlestick's Avatar
    Join Date
    Jun 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yet again, it doesn't work. I copied and pasted that (changing the password of course) but to no avail. It's making me feel a right plank this.


Bookmarks

Posting Permissions

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