【问题标题】:geting an error "Couldn't fetch mysqli" but not on other computers收到错误“无法获取 mysqli”,但在其他计算机上没有
【发布时间】:2015-04-12 02:49:36
【问题描述】:

我有一个按钮,单击时会显示一个表单。它可以在我们课堂上的工作站和云 ide 中使用,但不能在我的笔记本电脑上使用。其他项目可以在我的笔记本电脑上运行。

我收到以下错误:

警告:mysqli::query():无法在第 7 行的 G:\Web Projects\kmbgis\assets\caseForm.php 中获取 mysqli
致命错误:在第 8 行对 G:\Web Projects\kmbgis\assets\caseForm.php 中的非对象调用成员函数 fetch_assoc()

这是caseForm.php中的PHP:

<?php
  include('config.php');

  //check any user action
  $action = isset( $_POST['action'] ) ? $_POST['action'] : "";

  $result = $conn->query("SHOW TABLE STATUS LIKE 'case_detail'");
  $row = $result->fetch_assoc();
  $nextCaseId = $row['Auto_increment']; 

  $result1 = $conn->query("SHOW TABLE STATUS LIKE 'suspect_data'");
  $row1 = $result1->fetch_assoc();
  $nextSuspectId = $row1['Auto_increment'];  

  $result2 = $conn->query("SHOW TABLE STATUS LIKE 'victim_data'");
  $row2 = $result2->fetch_assoc();
  $victimID = $row2['Auto_increment'];  

  //if the user hit the submit button on Case Detail
  if($action == "Add"){

    //Case Details Variables
    $r_unit = $_POST['r_unit'];
    $d_report = $_POST['d_report'];
    $t_report = $_POST['t_report'];
    $crimeCategory = $_POST['crime_category'];
    $r_station = $_POST['r_station'];


    if($crimeCategory == '101'){
      $offenseType = $_POST['crime_type1'];
    }else if($crimeCategory == '102'){
      $offenseType = $_POST['crime_type2'];
    }else if($crimeCategory == '103'){
      $offenseType = $_POST['crime_type3'];
    }else if($crimeCategory == '104'){
      $offenseType = $_POST['crime_type4'];
    }else if($crimeCategory == '105'){
      $offenseType = $_POST['crime_type5'];
    }else{
      $offenseType = "NOT NULL";
    }

    //Insert Values
    $sql = "INSERT INTO case_detail (ReportingUnit,DateOfReporting,TimeOfReporting,SuspectID,OffenseID,CrimeTypeID,VictimID,StationID)
            VALUES('$r_unit','$d_report','$t_report','$nextSuspectId','$offenseType','$crimeCategory','$victimID','$r_station')";

    if($conn->query($sql) === TRUE){
      echo "<script>
                window.location='https://localhost/kmbgis/assets/suspectArea.php?id=".$nextCaseId."';
            </script>";
    } else{
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
  }
  $conn->close();
?>

当单击按钮时,它会以&lt;?php include'caseForm'; ?&gt; 插入到 casedetail.php 文件中。而且在那个 casedetail.php 我有 PHP 代码可以打开连接、显示查询并关闭连接。

connect.php 包含:

<?php
    $servername = "localhost";
    $username = "wis1-29";
    $password = "Password";
    $dbname = "wis1-29";

    //Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    //Check connection
    if($conn->connect_errno){
        echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
    }
?>

这是包含 caseForm.phpcasedetails.php。 由于某种原因,它也没有显示我的 footer.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Police Database System</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <script src="js/jquery-1.11.2.js"></script>
        <script src="js/jquery-migrate.js"></script>
    </head>
    <body>
        <?php include 'assets/header.php'; ?>
        <section class="container-fluid">
            <?php include 'assets/loginForm.php'; ?>
            <div class="jumbotron">
                <h3 class="text-center">Police Database System</h3>
                <h4 class="text-center">Case Details</h4>
            </div>
            <div>
                <?php
                    include('assets/config.php');

                    $nameSql = "SELECT 
                                    case_detail.Crime_Case_ID, 
                                    offense.OffenseDescription,
                                    suspect_data.SuspectID,
                                    suspect_data.LastName, 
                                    suspect_data.FirstName, 
                                    case_detail.ReportingUnit,
                                    station.StationName,
                                    station.StationID,
                                    case_detail.DateOfReporting, 
                                    case_detail.TimeOfReporting,
                                    crime.CrimeTypeDescription,
                                    victim_data.vLastName,
                                    victim_data.vFirstName,
                                    victim_data.VictimID
                                FROM case_detail
                                INNER JOIN suspect_data
                                    ON case_detail.SuspectID = suspect_data.SuspectID
                                INNER JOIN offense
                                    ON case_detail.OffenseID = offense.OffenseID
                                INNER JOIN crime
                                    ON case_detail.CrimeTypeID = crime.CrimeTypeID
                                INNER JOIN victim_data
                                    ON case_detail.VictimID = victim_data.VictimID
                                INNER JOIN station
                                    ON case_detail.StationID = station.StationID";

                    $nameResult = $conn->query($nameSql);

                        if ($nameResult->num_rows > 0) {
                            echo "<table class='table table-bordered'>
                                    <thead>
                                        <tr>
                                            <th>Crime Case Number</th>
                                            <th>Crime Commited</th>
                                            <th>Suspect(s)</th>
                                            <th>Reporting unit</th>
                                            <th>Police Station</th>
                                            <th>Date of reporting</th>
                                            <th>Time of reporting</th>
                                            <th>Crime Category</th>
                                            <th>Victim(s)</th>
                                            <th>Update Case</th>
                                            <th>Delete Case</th>
                                        </tr>
                                    </thead>";
                            // output data of each row
                            while($row = $nameResult->fetch_assoc()) {
                                echo "<tbody>";
                                    echo"<tr>
                                            <td>".$row["Crime_Case_ID"]."</td>
                                            <td>".$row["OffenseDescription"]."</td>
                                            <td>".$row["LastName"].", ".$row["FirstName"]."</td>
                                            <td>".$row["ReportingUnit"]."</td>
                                            <td>".$row["StationName"]."</td>
                                            <td>".$row["DateOfReporting"]."</td>
                                            <td>".$row["TimeOfReporting"]."</td>
                                            <td>".$row["CrimeTypeDescription"]."</td>
                                            <td>".$row["vLastName"].", ".$row["vFirstName"]."</td>";
                                       echo'<td><a href="assets/caseUpdate.php?id='.$row["Crime_Case_ID"].'&s_id='.$row["SuspectID"].'&v_id='.$row["VictimID"].'&station_id='.$row["StationID"].' "  class="btn btn-default">Edit</a></td>';
                                       echo'<td><a href="assets/caseDelete.php?id='.$row["Crime_Case_ID"].' " class="btn btn-danger">Delete</a></td>';
                                    echo"</tr>
                                    </tbody>";
                            }
                            echo "</table>";
                        } else {
                            echo "0 results";
                        }
                    $conn->close();
                ?>
                <a class="addbtn btn btn-default" href="#" id="addCase">New Blotter Entry</a>
            </div>
            <div class="center formWrap" id="caseWrap">
                <button type="button" class="btn btn-danger btn-sm addbtn">
                  <span class="glyphicon glyphicon-remove"></span> Close 
                </button>
                <h2 class="text-center">Enter new case:</h2>
                <?php include 'assets/caseForm.php'; ?>
            </div>
        </section>

        <?php include 'assets/footer.php'; ?>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/main.js"></script>
    </body>
</html>

【问题讨论】:

  • 你在哪里包含“connect.php”?
  • 嗨尼塞!我将它包含在 casedetail.php 中,其中还包含了 caseForm.php。在caseForm 中,我还包含了connect.php。我做对了吗?就像我查询以在 casedetail.php 中显示结果时一样任务

标签: php mysqli


【解决方案1】:

如果没有看到您的连接设置很难判断,假设在 config.php 中,但该错误通常是由于连接。第二个,非对象错误,是因为没有获得导致第一个错误的连接。

你能发布你的连接字符串吗?应该看起来像这样......

$conn = new mysqli("HOST","USER","PASSWORD","TABLE_NAME");

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • @FuzzyTree cmets 的 50 个代表限制使得在没有实际发布的情况下无法澄清问题。如果我无法得到澄清,你还建议我如何帮助这个人?我们显然需要了解连接字符串才能调试有关它的错误。
  • connect_errno){ echo "无法连接到 MySQL: (" . $conn->connect_errno . ") " . $conn->连接错误; } ?>
  • 这看起来可能是您的笔记本电脑的配置问题。您是否看到“无法连接到 MySQL”错误?要检查的事情: 1. 你确定 MySQL 服务器正在运行并服务于本地主机吗? 2.你在笔记本的php.ini中启用并配置了mysqli吗?
  • 谢谢詹姆斯!不,我没有看到连接失败,实际上其他页面与 caseForm.php 不同。我有其他页面使用相同的 config.php 文件插入、删除和编辑(查询)。这可能是不同版本的php或mysql的问题吗?我在笔记本电脑上使用最新的 Xampp,而我们学校使用的是较旧的安装。我不知道 cloud 9 ide,它是一个在线 ide,但它也可以在那里工作
【解决方案2】:

我安装了 wamp,一切正常!我在不同的引导上重新安装了相同的 xampp 版本,但它没有工作,所以问题可能是 mysql 版本。我从 5.5.9 更新了 PHP 版本,但仍然没有工作,所以我尝试在 xampp 中更新 mysql,但没有这样做。所以我决定下载更新版本的xampp。

没有工作的 xampp 版本是 1.8.3 和 5.5.9 PHP 和 5.6.16 MYSQL 工作的 Wamp 有 PHP 5.5.12 和 5.6.17 MYSQL

【讨论】:

  • 下载了最新版本的xampp 5.6 PHP版本,还是不行!有人吗?
猜你喜欢
  • 2012-08-02
  • 1970-01-01
  • 2015-06-29
  • 2018-04-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2020-08-29
相关资源
最近更新 更多