连接到DB

时间:2018-03-24 17:33:50

标签: php database phpmyadmin

我与数据库的连接无效。我注册了一个用户,数据库没有输入密码。当我尝试登录时,我收到了这些错误: 这些是我收到的错误:

  

警告:mysqli_query()需要至少2个参数,1在第30行的C:\ wamp64 \ www \ Site \ login.php中给出

     

警告:mysqli_error()预计在第30行的C:\ wamp64 \ www \ Site \ login.php中给出1个参数,0

这是login.php表单。如果需要,我会发布register.php表格。 对不起,如果我看起来很蠢,我是初学者。 :d

// preia datele din formular
$username = $_POST['login_username'];
$password = md5($_POST['login_password']);

// formeaza si executa query-ul de select din baza de date
$query = "SELECT * FROM users WHERE `Username` = '".$username."' AND `Password` = '".$password."'";
$result = mysqli_query($query) or die ( "Error : ". mysqli_error() );

// verifica daca interogarea mysqli a gasit date valide
if ($result || mysqli_num_rows($result) < 1) {
    // daca nu, afiseaza un mesaj de eroare
    echo "Datele introduse sunt incorecte<br>
        Click <a href='index.php'>aici</a> pentru a reveni la pagina de login";
} else {

    // salveaza username-ul si parola in sesiune
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;

    // afiseaza un mesaj de succes
    echo "Autentificarea a fost efectuata cu succes.";
}

}

2 个答案:

答案 0 :(得分:1)

您需要先建立与数据库服务器的连接。像这样:

  $con = mysqli_connect("localhost", "my_user", "my_password", "my_db");

然后将连接参数传递给函数,如下所示:

$result = mysqli_query($con, $query) or die ( "Error : ". mysqli_error($con) );

答案 1 :(得分:0)

<?php

在session_start(); require_once(&#34; dbconnect.php&#34);

// verifica daca存在日期变换 if($ _POST [&#39; register_name&#39;]!=&#39;&#39;&amp;&amp; $ _POST [&#39; register_username&#39;]!=&#39;&#39; &amp;&amp; $ _POST [&#39; register_password&#39;]!=&#39;&#39;){

// preia datele din formular
$name = $_POST['register_name'];
$username = $_POST['register_username'];
$password = md5($_POST['register_password']);

// formeaza si executa query-ul de inserare in baza de date
$query = "INSERT INTO users(Name,Username,password) VALUES('$name','$username','$parola')";

$ result = mysqli_query($ connect,$ query)或死(&#34;错误:&#34;。mysqli_error($ connect));     // afiseaza un mesaj de succes     if($ result){         回声&#34; Inregistrarea fost efectuata cu successces             点击aici pentru a reveni la pagina de login&#34 ;;     } } ?&GT;