【问题标题】:Display all the items into the div display box [duplicate]将所有项目显示到div显示框中[重复]
【发布时间】:2016-03-04 12:51:12
【问题描述】:

这里我试图在 div 显示区域中显示所有项目。但是我不断收到这些错误;

注意:未定义的变量:连接 C:\xampp\htdocs\includes\landlord_profile.php 第 76 行

警告:mysqli_query() 期望参数 1 为 mysqli,给定 null 在 C:\xampp\htdocs\includes\landlord_profile.php 第 76 行

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result, 第 78 行 C:\xampp\htdocs\includes\landlord_profile.php 中给出的 null

<?php
session_start(); 

include ("includes/connect.php");
?>

<!doctype html>
<html>
<head>
    <title>Landlord Profile</title>


  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="css/index.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>
<body>

<div class="whole_page_div" style="width:100%; height:935px;;">
        <div class="container-fluid" style="margin-top: 15px;>
                <div class="row">
                <div class="col-md-12">
                <div class="logo" style="margin-left: 750px;"><a href="Index.html"><img class="logo" src="images/logo.png"/></a></div>
                 <a href="logout.php"> <button type="submit" class="btn btn-primary" name="logout" value="Log out" style="background-color:#337AB7; margin-top: -130px; margin-left: 1600px; ">Log Out</button></a>

                </div>


<hr>
    <nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
    <div class="container topnav">


            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand topnav" href="#">MyAstonSpace</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="#Home">Home</a>
                    </li>
                    <li>
                        <a href="#Undergraduate Information">Undergraduate Information</a>
                    </li>
                    <li>
                        <a href="#Post-Grad Information">Post-Grad Information</a>
                    </li>
                     <li>
                        <a href="#International Students">International Students</a>
                    </li>
                     <li>
                        <a href="#Contact Us ">Contact Us </a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
      </div>
        <!-- /.container -->
    </nav>


    <?php
    function getprop (){

        $getprop = mysqli_query($connect,"SELECT * FROM properties");

        while ($rowprop = mysqli_fetch_array($getprop)){

            $prop_id = $rowprop['prop_id'];
            $prop_name = $rowprop['prop_name'];
            $prop_address = $rowprop['prop_address'];
            $prop_img = $rowprop['prop_img'];
            $prop_radius = $rowprop['prop_radius'];
            $prop_bedrooms = $rowprop['prop_bedrooms'];
            $prop_type = $rowprop['prop_type'];
            $prop_price = $rowprop['prop_price'];

            echo "
                <div class='singleproperty'>

                <h3>$prop_name</h3>
            "   ;       

            }


        }


    ?>
    <div class="container-fluid">
  <h3>Your Profile <h5> Landlord </5></h3>
  <ul class="nav nav-pills nav-stacked" style="width:300px;">
     <li><a href="#">Personal Information</a></li>
    <li><a href="includes/landlord_profile.php">My Proporties</a></li>
    <li><a href="#">Upload Profile Image</a></li>
    <li><a href="incldues/deactivate_myaccount.php">Deactivate My Account</a></li>

  </ul>
</div>


<center>
<div class="panel panel-default" style="width: 1200px; height: 400px; margin-top: -200px; margin-left: 100px; border: 2px solid blue;  " >
    <div class="panel-body" style="text-align:center; margin-left:30px; margin-bottom:10px;"> 
        <div class="listproperties">
        <?php
        getprop();
        ?>
        </div>

     </div>
</div>
</center>


<?php
include ("includes/footer-inc.php");
?>

【问题讨论】:

    标签: php function variables mysqli scope


    【解决方案1】:

    我想$connect 是在您的includes/connect.php 中定义的。您尝试在一个根本无法访问该变量的函数中访问该变量(也就是在另一个范围内)。

    您应该将$connect 作为参数添加到您的函数getprop() 以使其可访问:

    function getprop ($connect) {
      ...
    }
    

    每次使用该函数时,请通过 using 添加参数

    getprop($connect);
    

    而不是

    getprop();
    

    另一种可能性是使用全局变量,但这是非常糟糕的编码风格,所以不要这样做。

    您应该阅读关于变量范围的手册页,其中解释了谁可以访问哪些变量:http://php.net/manual/en/language.variables.scope.php

    【讨论】:

      【解决方案2】:

      这是因为您的变量$connect 在函数内部使用。

      但是,那里无法访问。

      使用global 喜欢:

      function getprop (){
        global $connect;
      // Your code ...
      

      【讨论】:

        猜你喜欢
        • 2018-04-23
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多