问题
what's the problem in my code ?
this is full of my connection code...
please help me what's the wrong in this code ?
<?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;
}
}
?>
I try to run this but every time has error what should I do. I couldn't find my mistake.
回答1:
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);
回答2:
Change this
$query .= "WHERE id=" . $subject_id ." ";
to
$query .= "WHERE id='" . $subject_id ."' ";
Here I am assuming that $conection has the value of mysql_connect($host, $username, $password); and you have selected the database using mysql_select_db($database). Go through the following manuals:
- mysql_connect
- mysql_select_db
来源:https://stackoverflow.com/questions/23142689/warning-mysql-query-expects-parameter-2-to-be-resource