1

I am developing online ordering system . I want to show the products details when web page load in the server. My products details are stored inside mysql database . But the problem is when i run the application in localhost I am getting followings errors ..

Notice: Undefined variable: con in C:\wamp64\www\onlineordering\functions\datafetching.php on line 33

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\onlineordering\functions\datafetching.php on line 33

Here is my 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());
}


?>

Here is code for 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;
                    }




}

?>

Here is the screenshotenter image description here when i run the application ..

Mohammad
  • 959
  • 6
  • 16

2 Answers2

2

Pass connection to user defined function getrecords

Take one more parameter in function definition.

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

When you call getrecords

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

When you call your custom function it's get connection from connection.php and pass it to custom function as argument, so you can use $con on it.

Smartpal
  • 1,326
  • 1
  • 10
  • 17
  • 1
    @Mohammad yeah, user defined function cannot directly access $con, You must send $con through parameter. So your user defined function can use this connection for preparing mysqli statement – Smartpal Jul 08 '18 at 01:46
  • yes from database. when the web page load first i want to display the productes – Mohammad Jul 08 '18 at 02:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174574/discussion-between-smartpal-and-mohammad). – Smartpal Jul 08 '18 at 02:37
0

try this

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

//...
}
SSpoke
  • 5,239
  • 8
  • 66
  • 112
  • Using global variable is not good practice. Check this out. https://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – Smartpal Jul 08 '18 at 01:54
  • I don't understand that question page but this is how to solve it if you want to include a global connection wrapper on every page – SSpoke Jul 08 '18 at 01:57
  • global variable may affect other variable with same name from other files/classes/functions. – Smartpal Jul 08 '18 at 02:03
  • o well you have to keep count of the global names then so you don't have the same ones haha. – SSpoke Jul 08 '18 at 02:34