SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHp wont conncet to MySql database

    Hi,

    Have anyone encountered this error before. I am writing a web application using PHP5 & MySQL 5. The application is simple enough, users can register online and log in to access a personal control panel. I have a php file that i use to set-up the database. The contents of this are as follow:

    $username = "someuser";
    $password = "pass";
    $hostname = "localhost";
    $db = "dbase";
    $conn=mysql_connect($hostname,$username,$password) or die("Cannot connect to the database");
    $db=mysql_select_db($db,$conn)or die("Cannot select the database");

    I then have another script to log in thus:

    include("database.php");
    session_start();
    if (isset($_POST["submit"])) {
    $sql = "SELECT * FROM users WHERE Email = '" . $_POST['LogonID']
    . "' AND password = '" . $_POST['Password'] . "';";

    $result = mysql_query($sql);
    $numrows = mysql_num_rows($result);

    if($numrows == 1) {
    //login successful
    $row= mysql_fetch_array($result);

    $_SESSION['USERNAME'] = $row["firstname"];

    redirecttopage("controlpanel.php");

    }
    }
    else {
    redirecttopage("logon.php");


    Note: redirecttopage is a function I created myself.

    The code looks ok to me but whenever I launch the log-on script, I keep on getting an error that php couldnt not connect to localhost. I have verified tha MYSQL service is running and that I can connect as the user and have all the right privileges. Another funny thing is the script to register users use the same database setup script and that works fine. So why isnt the logon script working?

    Any thots guys cos am totally stumped by this. Thanx.

  2. #2
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    run your connection code and echo out when it connects etc...

    PHP Code:
    <?
    $conn 
    mysql_connect('server','root','password') or die("Cannot connect to the database");
    echo (
    $conn) ? "Connected with Success\r\n" mysql_error();

    $db=mysql_select_db('database'$conn) or die("Cannot select the database");
    echo (
    $db) ? "Selected DB\r\n" mysql_error();

    mysql_close($conn);
    ?>
    if you get a good error message be sure to post your code

  3. #3
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have since sorted out the problem. I just decided to do without the database setup script and hard code the connection into the page. not very flexible but it works. Thanx anyway.

  4. #4
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think including the connection file would work fine. I don't think there is any special rule that prohibts this as I see it all the time. Maybe the script was having trouble finding the file? Not sure.

    BTW

    use 'php' '/php' tags where ' = [ and ]
    ]


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
  •