开发者

Trouble connecting to MySQL

I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work... Can anyone please tell me what I might be doing wrong? this is the php script I am using:

<?php

$useremail = $_POST["useremail"];
$password = $_POST["passwor开发者_StackOverflow社区d"];

if($useremail && $password){

    // open database
    $connect = mysql_connect("localhost", "carlos", "nenem");

    if(!$connect){
        die ("Not able to connected to the database: " .mysql_error());
    }

    // select database
    $select_db = mysql_select_db("vergilioDB", $connect);

    if(!$connect_db){
        die("Not able to connect to the database: "  .mysql_error());
    }

    mysql_close($connect);
} else {
    die("Please enter useremail and password, or REGISTER if you are a new user!");
}

?>


Carefull, $select_db != $connect_db

The variable names are different, rewrite to:

$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
     die("Not able to connect to the database: "  .mysql_error());
}


note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..

mysql_close($connect);

You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜