php - How to write the constant in my function? -


function insert_data($array){     $dbh=new pdo('sqlite:c:\test.sqlite');     $sql = "insert quote (symbol,price) values (?,?)";     $q = $dbh->prepare($sql);     $q->execute($array);     $dbh=null; }   

i want define c:\test.sqlite constant.

define('db_name','c:\test.sqlite'); 

but can't use constant in function insert_data.

function insert_data($array){     $dbh=new pdo("sqlite:db_name");     $sql = "insert quote (symbol,price) values (?,?)";     $q = $dbh->prepare($sql);     $q->execute($array);     $dbh=null; }  

an error occur :

fatal error: call member function execute() on non-object in.... 

i don't want write function format :

 function insert_data($array,$db){     $dbh=new pdo("sqlite:{$db}");     $sql = "insert quote (symbol,price) values (?,?)";     $q = $dbh->prepare($sql);     $q->execute($array);     $dbh=null; }  

how fix it?

define variable - $db = 'c:\test.sqlite';

and make global.

function insert_data($array){     global $db;     $dbh=new pdo("sqlite:{$db}");     $sql = "insert quote (symbol,price) values (?,?)";     $q = $dbh->prepare($sql);     $q->execute($array);     $dbh=null; }  

read here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -