php数据库中的连接错误

时间:2015-08-02 13:31:40

标签: php

我在php中连接数据库时出错。我无法在此代码中找到错误。它没有显示任何错误。这是关于登录的详细信息。请帮我解决这个问题。

<?php
    $userName =  $_POST["userName"];
    $password = $_POST["password"];

    $conn=mysql_connect("localhost","root","") or die ("failed to connect my sever");
    $dbase=mysql_select_db("project",$conn) or die("mysql dbase");

    //Database Connection
    //include("database/dbCon.php");
    $query = "SELECT userType FROM user WHERE userName = '$userName' and password = '$password'" or die(mysqli_error());
    $result = mysqli_query($conn, $query);

    if(mysqli_num_rows($result)==0){

        header("LOCATION:home.php");

    }
    else
    {
        $row = mysqli_fetch_row($result);
        $userType = $row[0];
        setcookie("log","logged");
        setcookie("userName",$userName);

        if($userType == "Admin"){
            setcookie("userType",$userType);
            header("LOCATION:Admin.php");
        }
        else if($userType == "Doctor")
        {
            setcookie("userType",$userType);
            header("LOCATION:Doctor.php");
        }

        if($userType == "Patient"){
            setcookie("userType",$userType);
            header("LOCATION:Patient.php");
        }
    }
?>

2 个答案:

答案 0 :(得分:2)

如果此类申请上线,那真的很可怕。一个片段中存在很多问题......

使用已弃用的mysql_ *函数,容易受到SQL注入......你真的需要对php基础知识做一些研究,因为到目前为止这是非常糟糕的。

无论如何,我猜你的代码有什么问题,就是混合了mysql_和mysqli_函数。仅使用mysqli_ *或PDO。您首先使用mysql_connect创建连接,然后尝试使用mysqli_fetch_row,这显然无法正常工作。

答案 1 :(得分:0)

我认为代码中没有错误,但连接到数据库时出错,请参考php mannual获取 mysql_error()功能链接error