2 Replies - 1440 Views - Last Post: 02 July 2012 - 01:32 AM Rate Topic: -----

#1 Skrfenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-July 12

Fatal error: Call to a member function prepare() on a non-object in

Posted 01 July 2012 - 10:19 PM

I can't figure out why I am getting this fatal error when I run this PHP. I'm new to the who thing. Trying to figure it out for a class. Thanks.
session_start();

require $_SERVER['DOCUMENT ROOT'].'connections/conguest.php';
require $_SERVER['DOCUMENT ROOT'].'connections/conadmin.php';

/*function regUser(){
    global $conguest;
}*/

  $server = 'website.com';
  $username = 'guest';
  $password = 'password';
  $database = 'websitedb';
  
  $conguest = mysql_connect($server, $username, $password, $database);
if (!$conguest)
  {
  die('Could not connect: ' . mysql_error());
  }
function updateUser($fname, $lname, $gender, $city, $state, $username){
    $sql = "UPDATE thatswhatyousay_test.Users
            SET    first_name = ?
            ,      last_name = ?
            ,      gender = ?
            ,      city = ?
            ,      state = ?
            WHERE  username = ?";
    /*Paste if here*/
if($stmt = $conguest->prepare($sql)){
    //provide the bind parameters
    $stmt->bind_param('ssssss', $fname,$lname,$gender,$city,$state,$username);
    // bind results
    echo $stmt;
    // check the result
    $stmt->execute();
    $success = $stmt->affected_rows;
    $stmt->close();
    //Return the result to the controller
  } else {
    return 0;
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error: Call to a member function prepare() on a non-object in

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,250
  • Joined: 08-June 10

Re: Fatal error: Call to a member function prepare() on a non-object in

Posted 02 July 2012 - 01:31 AM

there are two reasons:
- the outdated mysql extension is not the same as MySQLi
- functions do not automatically import variables from the outer scope
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Fatal error: Call to a member function prepare() on a non-object in

Posted 02 July 2012 - 01:32 AM

You seem to be mixing up the Original MySQL API and the Improved MySQL Extension. The way you are connecting on line #15 indicates the former, while your use of it inside the updateUser function indicates the latter. (The Improved version is far superior, by the way.)

There is also a scope issue there. The $conguest variable is defined in the global scope, but you use it inside the function without importing it or passing it. Global variables aren't available inside functions unless imported or passed.
// Imported:
$var = "Some value";
function foo() {
    global $var;
    echo $var;
}

// Passed:
$var = "Some value";
function foo($var) {
    echo $var;
}
foo($var); // This is where you pass the $var into the function.


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1