what's the problem in my code ?
this is full of my connection code...
please help me what's the wrong in this code ?
define("Server", "localhost");//constant for connennection
define("Username", "root");
define("Password", "123456");
?>
$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());
}
?>
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;
}
}
?>
I try to run this but every time has error what should I do. I couldn't find my mistake.
解决方案
It seems that you have not initialized your connection.
You should add
mysql_connect($host, $username, $password);
and only after that do
mysql_query($query);