Call to undefined method PDO::execute(), previous answers doesn't solve the error

StackOverflow https://stackoverflow.com/questions/23550953

  •  18-07-2023
  •  | 
  •  

سؤال

I'm trying to make a CSV import function via php, which stores the information in a MySQL database, but for some reason, it gives the below error :-

Fatal error: Call to undefined method PDO::execute() in /home/a/public_html/admin/admin_import_product.php on line 25

<?php  
session_start();
include '../inc/inc.functions.php';
include '../dbconnector.php';
include '../dbpdo.php';
include '../inc/inc.config.php';
if((isset($_SESSION['admin'])) && ($_SESSION['admin'] == 1))
{
    $adminusername = $_SESSION['username'];
    $date=date('Y-m-d');
    if ($_FILES[csv][size] > 0) { 

        //get the csv file 
        $file = $_FILES[csv][tmp_name]; 
        $handle = fopen($file,"r"); 
         //prepare the statement for the insertion of the record via PDO
        try{
            global $conn;

            $statement = $conn->prepare("INSERT INTO products(category,productname,baseprice,basepricewd,basepricenw,addedby,addedon) VALUES (?,?,?,?,?,?,?)");
            //loop through the csv file and insert into database 

            do { 
                if ($data[0]) { 
                    $statement=$conn->execute(array(
                        $data[0],
                        $data[1],
                        $data[2],
                        $data[3],
                        $data[4],
                        $adminusername,
                        $date));
                } 
            } while ($data = fgetcsv($handle,5000,",","'")); 
        // 
    }//try
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }


        //redirect 
       // header('Location: admin_importproducts_home.php?success=1'); die; 
        echo "Products imported successfully";

    } 

 }
   else
{
    header('Location:http://a.co.uk/' . $config["admindir"] . '/adminlogin');
}
?>

Any suggestions are helpful.

هل كانت مفيدة؟

المحلول

You are calling execute() from the wrong object/resource (whatever PDO is):

// this is the proper chain of calls as stated in the documentation
$statement = $conn->prepare();
$execute_result = $statement->execute();

// execute() returns a boolean so you can use an if/else block to know if your query is hunky-dory
if($execute_result)
{
    // yay the query had no errors!
}
else
{
    // aww snap, you messed up now!
}

http://www.php.net/manual/en/pdostatement.execute.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top