发现错误"无法获取mysqli"但不在其他电脑上

时间:2015-04-12 02:49:37

标签: php mysqli

我有一个按钮,单击时会显示一个表单。它可以在我们的工作站上工作,也可以在云端工作,但不能在我的笔记本电脑上工作。其他项目在我的笔记本电脑上工作。

我收到这些错误:

  

警告: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();
?>

单击按钮时,会在{em> casedetail.php 文件中插入<?php include'caseForm'; ?>。还有那个 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.php casedetails.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>

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

我安装了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