Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP连接到SQL_Php_Mysql_Sql_Sql Server_Forms - Fatal编程技术网

PHP连接到SQL

PHP连接到SQL,php,mysql,sql,sql-server,forms,Php,Mysql,Sql,Sql Server,Forms,我想用php连接到sql server。我有一个用户表,但无法连接服务器 Login.php <html> <form action='LoginAction.php' method='POST'> Username: <input type='text' name='username'/><br/> Password: <input type='password' name='password'/><br/>

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

Login.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.");
  }
?>
试试这个,也许

<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.");

}

?>
试试这个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();
    }
}
?>

在您的编辑中,您提到了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']);

?>
当然,别忘了用$encrypt\u password替换$get\u user->execute(数组)中的“$password”

您也可以尝试以下方法:

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

你连接到本地主机还是远程主机?用
localhost
替换
“myphpadmin.net”
,你的两个连接字符串似乎在使用完全不同的(可能无法访问)服务器。他正在连接到另一个世界。如果你开始学习,那么就用PDO(PHP数据对象)更好地学习-这个很好用。我不想太傻,但是如果我在我的计算机上(而不是本地主机上)创建一个sql文件,我可以用这个代码连接它吗?如果是的,怎么做?我没有完全理解你。但使用此脚本,您可以连接到服务器,以便将服务器的主机名更改为
localhost
<?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 ;)
$connect = mysql_connect("myphpadmin.net", $username, $password) or die("Couldn't connect!");
mysql_select_db($database, $connect) or die("Coulnd't find db!");