Hello All.
I desperatly need help putting my webpage together. What I am doing is going to be like a address book of sorts...
I have been working on this for about a month now, and no matter what I do, I cannot get my php to connect to my database. I have read many tutorials, and don't understand what I am doing wrong. I have tried six different ways that I have found online. All I get is a blank page... I'm not sure if it is connecting....
It is asking for my hostname.com.... what is this? I posted my site name.com, and then that didn't work so I tried my ip address.com that didn't work. I know I have my username, password, and db name right....
Also, I don't understand what I should expect to come up once it connects to the database.... all I get is a blank page... how do I know if it is connected?
Will someone please give me some direction??? I am frustrated beyond belief.

Thanks for your help,
Christina

    If using a "typical" web host set-up and the MySQL functions, you can usually connect via the generic host "localhost", e.g.:

    mysql_connect('localhost', 'userid', 'password');
    

    If that does not work, then most likely you need to contact your hosting support or check your site set-up information to find the exact hostname string that needs to be used.

      Thanks for your quick response.
      So let me get this straight, I just leave it saying localhost?
      I'm sorry, I'm so new to this whole thing....

      I wish someone would just start a tutorial and tell you from the beginning what you are suppose to do. Starting at the beginning.

      Thanks for your help! 🙂
      Christina

        Yes, you should be able to leave the host as localhost. Have you set up the database on your site? You will need to set a user and a password. and then use those to connect. If your data base is named test and your username is christina with a password of mules, then your code would be mysql_connect('localhost', 'test', 'christina', 'mules');

          williamg;10881252 wrote:

          Yes, you should be able to leave the host as localhost. Have you set up the database on your site? You will need to set a user and a password. and then use those to connect. If your data base is named test and your username is christina with a password of mules, then your code would be mysql_connect('localhost', 'test', 'christina', 'mules');

          Actually, it would be:

          mysql_connect('localhost', 'christina', 'mules');
          mysql_select_db('test')
          

            Or, better yet:

            $conn = new mysqli('localost', 'christina', 'mules', 'test');

              I have set up my database already.... I have done all that already and its not working. I've never gotten an error except one time... So how do I know if it is connecting?
              Thanks for all your help.
              Christina

                It's all about checking return values and reporting/logging errors when you get unexpected results (usually a boolean false). This means checking for false from your mysql_connect(), your mysql_select_db(), and your mysql_query() commands. In each case, if false is returned you can use the [man]mysql_error/man function to give you more info in your error output/logging.

                  Hi Christina,

                  I should really be continuing my learning process, but I thought I'd try to help!

                  To start with you should do a very VERY basic check that will display 'something' to let you know that you have a connection. This might eliminate connection issues

                  <?
                  mysql_connect("localhost","your username","password");
                  echo "Connected to MySQL";
                  ?>

                  To confirm, everything entered in that first bracket needs double quotation marks.

                  As you can guess, you should see Connected to MySQL

                  You might be able to help this forum help you, if you quote your code within a thread so people can check it out.

                  Good luck with the slow learning curve - I too am finding it difficult to understand why certain things (examples) don't work, but with patience ya never know, both of us might be answering peoples questions on here lol!

                  Cheers,
                  Chris

                    Chris, note that your code will output "Connected to MySQL" regardless of whether it actually does. You need to check the return value:

                    $connx = mysql_connect("localhost","your username","password");
                    if(!$connx) // it returned false
                    {
                       user_error("DB connect failed: " . mysql_error(), E_USER_ERROR); // fatal error
                    }
                    $db = mysql_select_db("database_name");
                    if(!$db) // it returned false
                    {
                       user_error("DB selection failed: " . mysql_error(), E_USER_ERROR); // fatal error
                    }
                    $sql = "SELECT * FROM table";
                    $result = mysql_query($sql);
                    if(!$result)
                    {
                       user_error("Query failed: " . mysql_error(), E_USER_ERROR);
                    }
                    

                      Nogdog, I'll have to bow out of this one - for lack of experience! I would have thought it would at least confirm some sort of connection, i.e. if the passwords wrong it confirm so.

                      Christine, you'll have to ignore my post, sorry!

                        $dbhost = 'localhost';
                        $dbuser = 'root';
                        $dbpass = 'pass';
                        $dbname = 'table';
                        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      
                        ('Error connecting to mysql'); mysql_select_db($dbname);

                        This is what I use in all of my work to check for db connectivity. It will only show if there is an issue. Of course, you need to set your variables correctly.

                        $sql = "DELETE FROM `table` WHERE `table`.`column` = '$_POST[username]' LIMIT 1";
                        if (!mysql_query($sql))
                          {
                          die('Error: ' . mysql_error());
                          }
                        

                        That is to see if a query is preformed correctly or not. Often times, that is very helpful...If you find this post confusing, use NogDog's instead. I just tried to use a simpler method that I use all the time.

                          Write a Reply...