警告:mysql_query()期望参数2为资源 [英] Warning: mysql_query() expects parameter 2 to be resource

查看:205
本文介绍了警告:mysql_query()期望参数2为资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有什么问题?
这充满了我的连接代码...
请帮我这段代码有什么问题吗?

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.

推荐答案

似乎您尚未初始化连接.

It seems that you have not initialized your connection.

您应该添加

mysql_connect($host, $username, $password);

只有在那之后才做

mysql_query($query);

这篇关于警告:mysql_query()期望参数2为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆