0

我想用php连接到sql server。我有一个用户表,但我无法连接服务器。

登录.php

  <html>
  <form action='LoginAction.php' method='POST'>
  Username: <input type='text' name='username'/><br/>
  Password: <input type='password' name='password'/><br/>
  <input type='submit' value='Login'/>
  </form>
  </html>

我尝试了 2 个代码到 LoginAction.php

首先:

<?php
  $username = $_POST['username'];
  $password = $_POST['password'];
  if ($username&&$password)
  {
      $connect = mysql_connect("myphpadmin.net", $username, $password, "my_db") or       die("Couldn't connect!");
      mysql_select_db("users") or die("Coulnd't find db!");
  }
  else
  {
      die("please fill in all fields.");
  }
?>

第二:

<?php
  $connection=mysqli_connect("example.com",$username, $password,"my_db");
  // Check connection
  if (mysqli_connect_errno($connection))
  {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  mysqli_close($connection);
?>

另外,我创建了一个表“users.sql”并尝试将其放入我的代码中,但它仍然无法正常工作。你能帮助我吗?

感谢帮助者!

编辑:我想我会像aldanux所说的那样尝试用PDO来做,谢谢大家!

4

4 回答 4

1

试试这个 PHP

<?php
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($username) && isset($password))
{
    try {
        $connection = new PDO('mysql:host=localhost;dbname=dbname', $username, $password);

            // to close connection 
        $connection = null;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
?>
于 2013-09-18T10:42:11.080 回答
0

试试这个也许......

<html>

<form action='LoginAction.php' method='POST'>

Username: <input type='text' name='username'/><br/>
Password: <input type='password' name='password'/><br/>
Database: <input type='text' name='database'/><br/>
<input type='submit' value='Login'/>

</form>

</html>

和PHP....

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$database = $_POST['database'];

if (isset($username) && isset($password) && isset($database))
{

$connect = mysql_connect("myphpadmin.net", $username, $password, $database) or die("Couldn't connect!");
mysql_select_db("users") or die("Coulnd't find db!");

}
else
{

die("please fill in all fields.");

}

?>
于 2013-09-18T10:14:55.407 回答
0

在您的编辑中,您提到了 PDO,这是一个示例!

<?php
$config['db'] = array(
    'host' => 'yourHost(i.e. localhost)',
    'username' => 'yourUsername',
    'password' => 'yourPassword',
    'dbname' => 'yourDBName'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';
               dbname=' . $config['db']['dbname'],
                          $config['db']['username'],
                          $config['db']['password']);

?>

您的登录情况将是这样的:

<?php
    include '../connection_file.php';

    $username= $_GET['username'];
    $password = $_GET['password'];

    $select_query = "SELECT
                   username,
                   password
                FROM user
                WHERE username = :username AND password = :password";

    $get_user = $db->prepare($select_query);

    $get_user->execute(array(':username' => $username,
                             ':password' => $password));

    $countRows = $get_user->rowCount();

    if($countRows >= 1){
        //The user is logged in
        //echo "true" or header('yourLocation')
    }else{
        //The user doesn't exist
        //echo "true" or header('yourLocation')
    }

?>

我希望这有帮助!

编辑

愚蠢的错误,也许你想加密密码,你可以这样做:

$password = $_GET['password'];
$encrypt_password = md5($password);
//As you guessed "md5" encrypts the password ;)

当然不要忘记用 $encrypt_password 替换 $get_user->execute(array) 中的 "$password" !

于 2013-09-18T14:25:55.140 回答
0

你也可以试试这个:

$connect = mysql_connect("myphpadmin.net", $username, $password) or die("Couldn't connect!");
mysql_select_db($database, $connect) or die("Coulnd't find db!");
于 2013-09-20T16:35:29.963 回答