我的代码中出现了什么问题?
这充满了我的连接代码......
请帮我这个代码有什么问题?
<?php
define("Server", "localhost");//constant for connennection
define("Username", "root");
define("Password", "123456");
?>
<?php // connection
$conection= mysql_connect(Server, Username, Password);
if(!$conection)
die("connection faild :". mysql_error());
$db_select= mysql_select_db("widget",$conection);
if (!$db_select) {
die("selection faild :".mysql_error());
}
?>
<?php
function conform_query($result){
if (!$result) {
die("query failed :".mysql_error()); }
}
function get_subject_by_id($subject_id) {
global $conection;
$query = "SELECT * ";//data base query
$query .= "FROM subject ";
$query .= "WHERE id= " . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $conection);
conform_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
}
else {
return NULL;
}
}
?>
我试着运行这个,但每次都有错误,我该怎么办。我找不到我的错误。
答案 0 :(得分:2)
您似乎尚未初始化连接。
你应该添加
mysql_connect($host, $username, $password);
仅在此之后
mysql_query($query);
答案 1 :(得分:0)
更改此
$query .= "WHERE id=" . $subject_id ." ";
到
$query .= "WHERE id='" . $subject_id ."' ";
这里我假设$conection
的值为mysql_connect($host, $username, $password);
,并且您已使用mysql_select_db($database)
选择了数据库。阅读以下手册: