0

This is my sql connection code

$config['db'] = array('host' => 'localhost', 'username' =>  'root', 'password' => '', 'dbname' => 'reputize');

try
{
    $db = new PDO('mysql:host='.$config['db']['host'].';dbname'.$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    echo "Database connected successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

and this is the code i am using to fetch data from the table

$query = $db->query("select * from metaTags where filename='index'");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) 
{   
    $ptitle=$row['MetaTitle'];
    $desc=$row['MetaDisc'];
    $kwords=$row['MetaKwd'];    
}

but the error keeps on showing

Call to a member function query() on a non-object in C:\xampp\htdocs\template OOP\include\class\websiteClasses.php on line 14

I have tried many solutions here and on other sites as well but of no use. Please help me out with this problem

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • Is the two snippets you showed in the same file? If not, did you include the connection-file? – Qirel Jun 06 '17 at 11:15
  • You're already using an API that supports **prepared statements**, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`PDO::prepare()`](http://php.net/pdo.prepare). – Qirel Jun 06 '17 at 11:17
  • You are cathing possible errors when making the database connection - but when making the actual query, you can not be bothered to do error checking at all? _"I have tried many solutions here and on other sites as well but of no use"_ - all you need to do is find out what is wrong with your query in the first place. And how to do that, is explained all over the net. – CBroe Jun 06 '17 at 11:17
  • no the above snippet is in other file but i have included the file in the other one – Deepanshu Jun 06 '17 at 11:17

2 Answers2

2

you have an error here dbname'.$config['db']['dbname'] it should be dbname='.$config['db']['dbname']

Omis Brown
  • 199
  • 2
  • 16
1

You forget to add '=' after dbname.

Your config should look like below

dbname='.$config['db']['dbname']

From your comment, I understand that your '$db' variable must be declared accessible to other functions. Please check $db has valid database connection before query the database.

I usually use below code for connecting and getting data for my play projects.

function getDbConnection()
{
  $pdo = null;
  try
  {
    $pdo = new PDO("mysql:host=" . HOST . ";dbname=" . DBNAME,USERNAME,PWD);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  }
  catch(PDOException $pe)
  {
    die("Sorry! Could not connect to database " . DBNAME. ": " . $pe->getMessage());
  }
  return $pdo;
}

function getData($qry)
{
  $res = null;
  try
  {
    if(!empty($qry))
    {
      $pdo = getDbConnection();
      if(!empty($pdo))
      {
        $res = $pdo->query($qry);
      }
    }
  }
  catch(PDOException $pe)
  {
    throw $pe;
  }
  return $res;
}

Note: The above functions are not robust. But It works.

sathish R
  • 402
  • 4
  • 8