-1

Hello I have been finding issues in code that was working fine before the $conn is giving error It is a simple insert with pdo in Php I have a connection class which works fine through i perform following operations in my file

// query
try{


$sql="INSERT INTO vup_file(filename,path,category,sub_category,user_id,comment,language,duration)
VALUES (:filename,:path,:category,:subcategory,:user_id,:comment,:language,:duration)";
global $conn;
$query=$conn->prepare($sql);

$query->execute
(array(':filename'=>$filename,':path'=>$path,':category'=>$category,':subcategory'=>$subcategory,':user_id'=>$user_id,':comment'=>$comment,':language'=>$language,':duration'=>$duration));
echo 'Success';





}catch(PDOException $e)
{
echo 'ERROR OCCURED : '.$e->getMessage();
}
}

I am getting a error of Call to a member function prepare() on a non-object in line 62 I tried to make $conn but that just didn't work how to get rid of this error ?

Srinath Naidu
  • 137
  • 2
  • 11

2 Answers2

0

You global property $conn has not bee established correctly.

I would look back at your connection function and ensure that you have made a sucessful db connection.

Matthew A Thomas
  • 924
  • 7
  • 15
0

As you have mentioned, you use mysql_query() in other file and it works fine.

It means you used mysql_connect().

It means you use procedure-style to connect to DB and create connection, but HERE you are trying to connect using OOP style and using connection created by mysql_connect().

It is impossible.

You need either to use OOP style to connect to DB:

$conn = new PDO('mysql:host=xxx;port=xxx;dbname=xxx', 'xxx', 'xxx', array( PDO::ATTR_PERSISTENT => false));

Or use procedure style to query DB instead of OOP-style:

mysql_query("INSERT INTO vup_file(filename,path,ca...

Procedure style is in past, now people mostly use OOP style, PDO objects. PDO is the future, PDO is good.

And mysql_*() functions are in past, it is bad manner to write new code such way... Well it is explained here very well Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46