【问题标题】:Error message: mysql_fetch_array() expects parameter 1 to be resource, null given错误消息:mysql_fetch_array() 期望参数 1 是资源,给定 null
【发布时间】:2014-03-26 18:27:01
【问题描述】:

我已经阅读了之前标题相似的问题,但似乎没有一个可以为我提供这种特殊情况的答案。我在特定功能上收到上述错误。我不确定是什么使它弹出。这是我的第一次开发,所以除非是专门解决问题,否则请忽略我应该使用 PDO 或 mysqli 的事实。

这是我要实例化的函数。当单独执行sql命令时,它会返回正确的结果。

public function search_for_candidates_by_technology($technology, $seniority){
    $technology = $this->real_escape_string($technology);
    $seniority = $this->real_escape_string($seniority);
    $this->query("SELECT * FROM candidates WHERE technology LIKE ". $technology ."  AND         seniority LIKE ". $seniority ."");

}

函数所属的类是tecnoDB 在我尝试实例化的实际页面中,这是代码:

<form name="buscarBase" action="buscarCV.php" method="POST">Que technologia:<input         type="text" name="usertech" value=""/><br/>
        Que seniority:<input type="text" name="userSeniority" value="" />
        <input type="submit" name="buscar" value="Buscar"  />
        <input type="submit" name="back" value="Panel de Control"/>
    </form>
    <table border="black">
        <tr><th>Technology</th><th>Seniority</tr>
    <?php
    $search = tecnoDB::getInstance()->search_for_candidates_by_technology($_POST['usertech'], $_POST['userSeniority']);
                while($searchResult = mysql_fetch_array($search)){
                        echo "<tr><td>" . htmlentities($searchResult['technology']) ."</td>";
                        echo "<td>". htmlentities($searchResult['seniority']) . "</td></tr>";
                }
                        ?>
    </table>

错误即将上线:while($searchResult = mysql_fetch_array($search)).... 这让我认为问题在于 $search 没有被创建为实例。有什么想法吗?

这是我的第一个项目和第一个问题,请温柔。

【问题讨论】:

  • 我不确定您为什么使用 2 个提交按钮?
  • 发布您的 TecnoDB 类的代码。这实际上并没有告诉我们任何事情。但无论如何, $search 显然是空的。对于 mysql_fetch_array 调用,它应该是 mysql resource(不是对象,绝对不是 null
  • @Adarsh - 一个原因:它允许您向表单处理代码发送不同的值。例如,您可以有两个提交按钮来处理用户请求:一个用于批准请求,一个用于拒绝请求。
  • 我正在发布我的 tecnoDB 类以及 buscarCV 页面的整个代码。回答 Adarse,我有两个提交按钮,因为一个会将用户返回到控制面板,另一个将执行搜索。这里是 buscarCV 的代码:
  • 还不能发布代码,我会在允许的时候发布。我想知道页面顶部代码中的退出是否正在结束会话,因此不让 $instantiate...

标签: php pdo mysqli


【解决方案1】:
    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        session_start();
if (!array_key_exists("user", $_SESSION)) {
    header('Location: index.php');
    exit;
}
require_once("Includes/tecnoDB.php");
    $company_id = tecnoDB::getInstance()->get_company_id_by_name($_SESSION['user']);

if ($_SERVER['REQUEST_METHOD'] == "POST"){
        if (array_key_exists("back", $_POST)) {
           header('Location: companyControlPanel.php' ); 
           exit;
        } 
else{
$service_user = tecnoDB::getInstance()->verify_service_status($company_id);    
         $access = $service_user->fetch_row();       
         if (array_key_exists ("buscar", $_POST)){
                   if($access[0] < 2 ){
               header("Location: selectServicePackage.php" );                
               exit;
                   }
                             }

}
}

        // put your code here ?>
        <form name="buscarBase" action="buscarCV.php" method="POST">Que tecnologia:<input type="text" name="usertech" value=""/><br/>
            Que seniority:<input type="text" name="userSeniority" value="" />
            <input type="submit" name="buscar" value="Buscar"  />
            <input type="submit" name="back" value="Panel de Control"/>
        </form>
        <table border="black">
            <tr><th>Technology</th><th>Seniority</tr>
        <?php
        $search = tecnoDB::getInstance()->search_for_candidates_by_technology($_POST['usertech'], $_POST['userSeniority']);
                    while($searchResult = mysql_fetch_array($search)){
                            echo "<tr><td>" . htmlentities($searchResult['technology']) ."</td>";
                            echo "<td>". htmlentities($searchResult['seniority']) . "</td></tr>";
                    }
                            ?>
        </table>
    </body>
</html>



here goes the tecnoDB class:

    class tecnoDB extends mysqli {


    // single instance of self shared among all instances
    private static $instance = null;


    // db connection config vars
    private $user = "phpuser";
    private $pass = "phpuserpw";
    private $dbName = "tecnosearch";
    private $dbHost = "localhost";

     //This method must be static, and must return an instance of the object if the object
 //does not already exist.
     public static function getInstance() {
   if (!self::$instance instanceof self) {
     self::$instance = new self;
   }
   return self::$instance;
 }

 // The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
 // thus eliminating the possibility of duplicate objects.
     public function __clone() {
   trigger_error('Clone is not allowed.', E_USER_ERROR);
 }
 public function __wakeup() {
   trigger_error('Deserializing is not allowed.', E_USER_ERROR);
 }

 // private constructor
    private function __construct() {
    parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
    if (mysqli_connect_error()) {
        exit('Connect Error (' . mysqli_connect_errno() . ') '
                . mysqli_connect_error());
    }
    parent::set_charset('utf-8');
}

public function get_company_id_by_name($name) {

    $name = $this->real_escape_string($name);

    $company = $this->query("SELECT id FROM company WHERE name = '"

            . $name . "'");
    if ($company->num_rows > 0){
        $row = $company->fetch_row();
        return $row[0];
    } else
        return null;
}

public function get_searches_by_company_id($company_id) {
    return $this->query("SELECT id, description, technology FROM searches WHERE company_id=" . $company_id);
}

public function create_company ($name, $password){
    $name = $this->real_escape_string($name);
    $password = $this->real_escape_string($password);
    $this->query("INSERT INTO company (name, password) VALUES ('" . $name . "', '" . $password . "')");
}

public function verify_company_credentials ($name, $password){
   $name = $this->real_escape_string($name);

   $password = $this->real_escape_string($password);
   $result = $this->query("SELECT 1 FROM company
               WHERE name = '" . $name . "' AND password = '" . $password . "'");
   return $result->data_seek(0);   
}

public function verify_service_status ($company_id){
    $company_id = $this->real_escape_string($company_id);
    $service = $this->query("SELECT service FROM company WHERE id = '". $company_id ."'");
    return $service;   
}

function insert_search($company_id, $description, $technology){
    $description = $this->real_escape_string($description);
    $technology = $this->real_escape_string($technology);
    $this->query("INSERT INTO searches (company_id, description, technology)" . 
                       " VALUES (" . $company_id . ", '" . $description . "','" .$technology. "')");
}

public function search_for_candidates_by_technology($technology, $seniority){
    $technology = $this->real_escape_string($technology);
    $seniority = $this->real_escape_string($seniority);
    $this->query("SELECT * FROM candidates WHERE technology LIKE ". $technology ."  AND seniority LIKE ". $seniority ."");
}
}
?>

我通过在 search_for_candidates_by_technology = $variable 中设置查询并返回变量以及在需要我指定此函数的文件的实际页面中修复了该错误。我将 search_for_candidates_by_technology 的实例设置为等于 $variable1 并创建了另一个对象作为 $variable1->get_array; 的结果。 .我的错误消息现在消失了,但结果没有出现在搜索中。我假设是因为该操作在同一页面上,它会导致页面重新加载,而当它重新加载时,它本质上是在重置。我正在考虑使用 AJAX 来显示结果,但我从未使用过异步 javascript,并且只短暂地看到过 XML。任何不需要 AJAX 的指针或想法?

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2018-01-05
    • 1970-01-01
    • 2011-06-21
    • 2011-06-05
    相关资源
    最近更新 更多