2014-07-15 54 views
-1

出于某种原因,查询内部的以下代码在我的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() . ")" 
    ); 

    } 

就像我说的,我得到的是仅与该查询错误消息,服务器是罚款与我的数据库连接。

+0

您需要使用mysql_error()来查看实际的错误消息是什么。 –

+1

不要再使用mysql_ *。使用mysqli_ *或PDO。 – WillardSolutions

+0

这是说我没有选择一个数据库,但我认为我做了。 –

回答

2

您正在使用mysqli_ *作为连接,但是您正在使用mysql_ *作为QUERY ...不认为您可以这样做,必须是一个或另一个(MYSQLI_ preffered)。此外,查询应该是:

$result = mysqli_query($connection,$query); 
相关问题