Warning: mysql_query() expects parameter 2 to be resource

感情迁移 提交于 2019-12-12 01:43:53

问题


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:

  1. mysql_connect
  2. mysql_select_db


来源:https://stackoverflow.com/questions/23142689/warning-mysql-query-expects-parameter-2-to-be-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!