Question

I've seen some of the previous posts on here but they really don't explain what it is I'm looking for. maybe someone can point me in the right direction.

All I'm doing is a simple select statement that works on some other pages but not this one.

at the top of my page, I have the connection to my db: include 'myDBconnection.php'; just like any other page that uses it.

I'm using these to tell me what errors I have:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

and they are returning this: "Fatal error: Call to a member function query() on a non-object in /var/www/vhosts/mydomainname.com/httpdocs/somedirectory/index.php on line 16"

So, this is my simple select statement that I'm trying to just display on the page:

<?php
//just a simple select statement
if($row = $con->query("SELECT * FROM ErrorCodeTable WHERE ErrorCodeID = 100"))//this is line 16 in my code
{
    $eCodeId = $row['ErrorCodeID'];
    $eCode = $row['ErrorCode'];

    echo $eCodeId." = ".$eCode;
}
?>

So, I'm stumped. Why is it telling me that?

Just for clarity's sake, this is my DB connection and I'm reading from an INI file.

function getConnected()
{
    $file = "../myConnection.ini";
    if (!$settings = parse_ini_file($file, TRUE)) throw new exception('Unable to open ' . $file . '.');

    $host = $settings['connection']['default_host'];
    $user = $settings['connection']['default_user'];
    $paass = $settings['connection']['default_pw'];
    $dbName = $settings['connection']['default_db'];

    $con = 'mysqli_connect("'.$host.'", "'.$user.'", "'.$paass.'", "'.$dbName.'")'; 
    return $con;
}   

//MYSQLI CONNECTION
$con = getConnected();
//CONNECTION CHECK
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

So, why do I get the "Fatal Error" message? Did I overlook or forget something in my query? Be gentle. :-)

Was it helpful?

Solution

Remove the single quotes around the below statement

$con = 'mysqli_connect("'.$host.'", "'.$user.'", "'.$paass.'", "'.$dbName.'")';
       ^                                                                     ^ 

By enclosing it in single quotes , your $con behaves like a string instead of a connection resource. The mysqli_connect function will never be executed.

Simply rewrite it like..

$con = mysqli_connect($host,$user,$paass,$dbName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top