错误消息:数据库查询失败

时间:2014-07-15 19:18:06

标签: php mysql sql

由于某种原因,查询中的以下代码可以在我的MySQL命令控制台中运行,但是当我尝试在PHP中将其作为查询运行时,出现问题并且我不确定是什么。这是我到目前为止所做的代码。

    //2. Perform database query 

    $query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";

    $result = mysql_query($query);

    //Tests if there was a query error
    if(!$result){
    die("Database query failed.");
    }

是否存在阻止MySQL中的代码(使用SELECT的行)起作用的东西,或者我的语法是否有些错误?

编辑:所以它说我没有选择数据库。但我以为我有。这是上面的代码:

//1. Create a database connection

$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.

//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
    die("Database connection failed: ".
    mysqli_connect_error().
    " (". mysqli_connect_errno() . ")"
    );

    }

就像我说的,我得到的错误信息只与查询有关,服务器与我的数据库连接没问题。

1 个答案:

答案 0 :(得分:2)

您正在使用mysqli_ *进行连接,但您正在使用mysql_ *进行查询...不要认为您可以这样做,必须是其中一个(MYSQLI_)优先停留)。查询也应该是:

$result = mysqli_query($connection,$query);