php网页无法找到连接变量

时间:2018-07-08 00:55:37

标签: php mysql wamp

我正在开发在线订购系统。我想在服务器上加载网页时显示产品详细信息。我的产品详细信息存储在mysql数据库中。但是问题是当我在本地主机上运行应用程序时,出现以下错误..

  

注意:未定义变量:con in   C:\ wamp64 \ www \ onlineordering \ functions \ datafetching.php,第33行

     

警告:mysqli_query()期望参数1为mysqli,给定null   在第33行的C:\ wamp64 \ www \ onlineordering \ functions \ datafetching.php中

这是我的connection.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$db = "ecom1";

// Create connection
$con = mysqli_connect($servername, $username, $password,$db);

// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}


?>

这是datafecthing.php的代码。

<?php
include "db/connection.php";

?>

<?php


function getrecords($exePro,$msg)
{

  $counPro= mysqli_num_rows($exePro);
                    if($counPro>0)
                    { 

                       $classPro=1;
                       while($resPro = mysqli_fetch_array($exePro))
                       {
                         $productcode= $resPro["productcode"];
                         $auto_number= $resPro["auto_number"];
                         $productname= $resPro["productname"];
                         $price= $resPro["price"];
                         //setlocale(LC_MONETARY,"en_US");
                         $price = $price;
                         $show_price= $resPro["show_price"];
                         $discount = (($show_price-$price)*100)/$show_price;
                         $img = "defualt.jpg";
                        $sql = "select * from productsimage where productanum='$auto_number' and size_type='s'";
                        $exeimg = mysqli_query($con,$sql)or die(mysql_error());



                        if(mysqli_num_rows($exeimg)>0)
                        {
                        $resimg = mysqli_fetch_array($exeimg);
                        $img = $resimg['imagename'];
                        }
                        if($classPro%3==0)
                        {
                         $lastclass='no_margin_right';
                        }else{
                         $lastclass='';
                        }

                        ?>

                <div class="product_box  <?php echo $lastclass;?>" >
                <form method="post" action="productdetail.php" id="frmdetail<?php echo $classPro;?>">
                <input type="hidden" name="productname" value="<?php echo $productname?>">
                <input type="hidden" name="pid" value="<?php echo $productcode?>">
                <input type="hidden" name="id" value="<?php echo $auto_number?>">
                </form>

                <img src="productimages/<?php echo $img?>" alt="Image 
<?php echo $classPro;?>"  style="height:150px;width:200px;cursor:pointer;" onclick="subform('frmdetail<?php echo $classPro;?>')"; />

                <h3><?php echo $productname;?></h3>
                <p class="product_price"><span style="text-decoration:line-through;"><?php echo  currency.$show_price?></span><br/>
                <?php echo currency.$price;?>
                </p>

                <form method="post" action="shoppingcart.php" id="frmaddproduct<?php echo $classPro;?>">
                <input type="hidden" name="product_code" value="<?php echo $productcode?>">
                <input type="hidden" name="product_qty" value="1">
                <input type="hidden" name="type" value="add">
                <input type="hidden" name="pid" value="<?php echo $auto_number;?>">
                <input type="hidden" name="return_url" value="<?php echo $current_url;?>">

                </form>


                <a style="cursor:pointer;" onclick="subform('frmaddproduct<?php echo $classPro;?>')" class="add_to_card">Add to Cart</a>

                <a style="cursor:pointer;" onclick="subform('frmdetail<?php echo $classPro;?>')" class="detail">Detail</a>
                 </div> 


                        <?php
                        $classPro++;
                        }
                    }else
                    {
                    //echo "Product is not available";
                    echo $msg;
                    }




}

?>

这是我运行应用程序时的屏幕截图enter image description here

2 个答案:

答案 0 :(得分:2)

将连接传递给用户定义的功能 获取记录

在函数定义中再添加一个参数。

function getrecords($exePro,$msg,$con){
//Code.....
}

当您拨打 getrecords

getrecodes ($somedata,$somemsg,$con);

调用自定义函数时,它会从connection.php获得连接并将其作为自变量传递给自定义函数,因此可以在其上使用 $ con

答案 1 :(得分:0)

尝试

function getrecords($exePro,$msg)
{
    global $con;

//...
}