Hi there I am a complete newbie. I have installed mysql, php and apache onto my windows xp PC. I have created a simple login page for my site and have read that I am supposed to include the following piece of code into my html page and then save it as a .php page and the page should connect to my database or return an error message.

<html>
<head><title>Connect Server</title></head>
<body>
<?
$link = mysql_connect("localhost",$POST['username'],$POST['password'])or die("Connect Error: ".mysql_error());
print "Successfully connected.\n";
mysql_close($link);
?>
</body>
</html>

However when I include the code nothing happens, is there something else I have to do to connect the page to my database????

Please help this is driving me mad!!!!!!!

Thanks😃

    how are you accessing this page? you need to open your browser and type http://localhost/nameoffile.php in your address bar. where nameoffile.php is the name of the php file you have written.

    ps; you might want to make sure you have php and mysql running aswell.

    what do you get when you type just http://localhost in your browser?

      also... just realised. this page is set up to get your user name and pass from a form posted to it. try replacing this;

      ("localhost",$POST['username'],$POST['password'])

      with your actual username / pass

      ("localhost","bob","bobspass")

        When i type in http://localhost the default apache page comes up telling me i have set up my server properly

        I know my database is on localhost as i can access it using phpmyadmin.

        I just need to know how to link my login.php page to my mysqyl database?

        Thanks in advance

          did you try hardcoding the username / pass as i suggested? this is the username and pass for your mysql db.

            I did what you suggested here is my code:

            <html>
            <head>
            <title>Barnet council Online</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            </head>

            <body>
            <?
            $link = mysql_connect ("localhost","root","") or die("Connect Error: ".mysql_error());
            print "Successfully connected.\n";
            mysql_close($link);
            ?>
            </body>
            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
            <tr>
            <td width="100%">
            <font size="7" color="#008080">LOGIN SCREEN</font></td>
            </tr>
            </table>
            <form method=post action="<?echo $PHP_SELF?>">
            <table cellpadding=2 cellspacing=0 border=0>
            <td>Account Number:</td><td><input type="text" name="accountref" size=10></td><tr>
            <td>PIN Code</td><td><input type="password" name="pin" size=10></td><tr>
            <td>&nbsp;</td><td><input type="submit" name="submit" value="Log In"></td>
            </table></form>

            </body>
            </html>

            The page comes out the same as my html page no error message or anything. Am i being stupid and missing something really obvious out???

              you might try using complete php tags. eg;

              <?php

              ?>

              instead of

              <?

              ?>

              the html in your previous post is a bit of a mess. you have two </body> tags. keep this test script simple.

              put just this in a page and see what you get.

              <?php 
                $link = mysql_connect("localhost","root","") or die("error");
                print "success";
                mysql_close($link);
              ?>
              

                I put in the code and nothing happened.

                I created a test page with just the code u gave me and a blank page came up. I guess my php isnt working????

                  ok I accessed the login page by HTTP://localhost/login.php and it said success. However when i changed the user name it still printed success. Does this mean it is working??

                    Doubt this will make a difference but try this:

                    <?php
                    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
                    if (!$link) {
                       die('Could not connect: ' . mysql_error());
                    }
                    echo 'Connected successfully';
                    mysql_close($link);
                    ?> 
                    

                      sounds like php is working. you might have some security issues with your db, but that is another matter.

                      try creating a db and some tables.... run some queries.... see what you get.

                        Try this... took me about 30 minutes to come up with this so I hope this was time well spent!

                        Database Test Instructions

                        1) Download the most stable version of phpMyAdmin from http://www.phpmyadmin.net
                        2) Install it on your system off the root (htdocs for Apache; wwwroot for IIS)
                        3) Edit phpMyAdmin's config.inc.php:
                        For this test, allow database drops (test.sql will have a sql command to drop it)
                        $cfg['AllowUserDropDatabase'] = TRUE;

                        Set the server info:
                        $cfg['Servers'][$i]['auth_type'] = 'config';
                        $cfg['Servers'][$i]['user'] = 'root';
                        $cfg['Servers'][$i]['password'] = 'password for root account';

                        4) Run phpMyAdmin from your browser (http://localhost/phpMyAdmin)
                        a) Name your database to be 'Test'
                        b) Choose latin1 binary collation
                        c) Click the create button to create the database
                        d) Click on the SQL tab
                        e) Cut and paste Test.sql (see below) in the "Run SQL query/queries on database" box
                        f) Click the Go button to run this SQL query
                        g) You should now have a Test database with 4 records in a Users table
                        h) Browse the Users table to prove to yourself that it exists

                        5) Create a PHP script file called DBTest.php (see below)

                        6) Run it from your browser (http://localhost/DbTest.php)

                        #---------------------------------------
                        #Test.sql
                        #---------------------------------------

                        --------------------------------------

                        Drop the Test database

                        --------------------------------------

                        DROP DATABASE Test;

                        ------------------------------------

                        Create Test database

                        ------------------------------------

                        CREATE DATABASE Test DEFAULT CHARACTER SET latin1 COLLATE latin1_bin;

                        ------------------------------------

                        Use Test database

                        ------------------------------------

                        USE Test;

                        ------------------------------------

                        Create a Users table

                        ------------------------------------

                        CREATE TABLE Users (
                        idUsers INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
                        username VARCHAR(20) NULL,
                        joinDate TIMESTAMP NULL,
                        PRIMARY KEY(idUsers)
                        ) Type=MYISAM;

                        ------------------------------------

                        Insert some user records

                        ------------------------------------

                        insert into Users (idUsers, username, joinDate) values (1, 'john kobashi', 20050101120000);
                        insert into Users (idUsers, username, joinDate) values (2, 'helen kobashi', 20050201120000);
                        insert into Users (idUsers, username, joinDate) values (3, 'kerry kobashi', 20050301120000);
                        insert into Users (idUsers, username, joinDate) values (4, 'mitzi kobashi', 20050401120000);

                        <?php

                        // ============================================
                        // File: DBTest.php
                        // Purpose: Test the mySQL database connection and print out some records
                        //
                        // Note: Change the password in the connection line below
                        // ============================================

                        echo "Start database test. <br />";

                        // Connect to mySQL
                        $dbLink = mysql_connect("localhost", "root", "yourpasswordhere");
                        if ($dbLink == FALSE)
                        echo "Cannot connect to database<br />";

                        // Select the Test database
                        if (mysql_select_db("Test", $dbLink) == FALSE)
                        echo "Cannot select database<br />";

                        // List out the users table

                        $result = mysql_query("SELECT idUsers, username, joinDate FROM Users");
                        if (!$result)
                        {
                        echo "Could not run query: " . mysql_error();
                        exit;
                        }

                        while ($row = mysql_fetch_row($result))
                        {
                        echo "UserId: " . $row[0] . "<br />";
                        echo "Username: " . $row[1] . "<br />";
                        echo "JoinDate: " . $row[2] . "<br />";
                        }

                        // Now close it
                        if (mysql_close($dbLink) == FALSE)
                        echo "Cannot disconnect from database<br />";

                        echo "End database test. <br />";

                        ?>

                          THANK YOU

                          I think I know what I'm doing now.

                            Write a Reply...