6 Replies - 5581 Views - Last Post: 13 September 2010 - 07:47 PM Rate Topic: -----

#1 makaialexa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-May 06

"Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 01:58 PM

I'm a noob in PHP, but the office I work now uses it. I have to insert the data from an Excel file to a MySQL DB, and I was looking around for help. When I use Netbeans' Debug, I get an error like this:

"Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\Continental\rutas\upload_routes_file.php on line 23"

This is the code:

<?php
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();

//this should the the absolute path to the config.php file
//(ie /home/website/yourdomain/login/config.php or
//the location in relationship to the page being protected - ie ../login/config.php )
require('../config.php');

//this should the the absolute path to the functions.php file - see the instrcutions for config.php above
require('../functions.php');
$link = Conectarse($db_server, $db_name, $db_username, $db_password);

$data = array();

function add_person( $first, $middle, $last, $email )
{
 global $data, $dbh;

  $sth = $dbh->prepare("INSERT INTO rutas_programadas (fecha, zona, ruta, area) VALUES(?, ?, ?, ?)");
 //$dbh->bindParam(1, $first);
 //$dbh->bindParam(2, $middle);
 //$dbh->bindParam(3, $last);
 //$dbh->bindParam(4, $email);
 $dbh->execute($sth,array($first,$middle,$last,$email));
 //$dbh->execute( $sth, array( $first, $middle, $last, $email ) );

 $data []= array(
   'first' => $first,
   'middle' => $middle,
   'last' => $last,
   'email' => $email
 );
}

if ( $_FILES['file']['tmp_name'] )
{
 $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
 echo $dom->saveXML();
 $rows = $dom->getElementsByTagName( 'Row' );
 $first_row = true;
 foreach ($rows as $row)
 {
   if ( !$first_row )
   {
     $first = "";
     $middle = "";
     $last = "";
     $email = "";
     $index = 1;
     $cells = $row->getElementsByTagName( 'Cell' );
     foreach( $cells as $cell )
     {
       $ind = $cell->getAttribute( 'Data' );
       if ( $ind != null ) $index = $ind;

       if ( $index == 1 ) $first = $cell->nodeValue;
       if ( $index == 2 ) $middle = $cell->nodeValue;
       if ( $index == 3 ) $last = $cell->nodeValue;
       if ( $index == 4 ) $email = $cell->nodeValue;

       $index += 1;
     }
     add_person( $first, $middle, $last, $email );
   }
   $first_row = false;
 }
}
?>
<html>
<body>
These records have been added to the database:
<table>
<tr>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Email</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['first'] ); ?></td><
<td><?php echo( $row['middle'] ); ?></td><
<td><?php echo( $row['last'] ); ?></td><
<td><?php echo( $row['email'] ); ?></td><
</tr>
<?php } ?>
</table>
Click <a href="list.php">here</a> for the entire table.
</body>
</html>



Can someone help me where the error is??

Is This A Good Question/Topic? 0
  • +

Replies To: "Call to a member function prepare() on a non-object" Error

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 02:06 PM

you forgot to define $dbh. one disadvantage of imported variables through the global keyword (that’s why it is considered bad coding practice)
Was This Post Helpful? 1
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 02:06 PM

You define $dbh as a global right before using it, you never make it into an object, this is what your error means. You're trying to use a method from a variable that isn't even an object.

Edit: :ninja:'d

Yours,
Shane~

This post has been edited by ShaneK: 13 September 2010 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 02:07 PM

The variable $dbh is not an object, so you can not call a member function on it. (Try doing var_dump($dbh); to see exactly what that variable is.)

I'm guessing you forgot to include your database script?

Edit: On second glance, line #15:
$link = Conectarse($db_server, $db_name, $db_username, $db_password);

Perhaps you should rename $link to $dbh?

This post has been edited by Atli: 13 September 2010 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 makaialexa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-May 06

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 02:23 PM

That was exactly what I did... I used $link. This is the Conectarse() function:

function Conectarse($db_server, $db_name, $db_username, $db_password) {
    //verifica si hay conexion al servidor de BD con el usuario y contrasena dados
    if (!($link = mysql_connect($db_server, $db_username, $db_password))) {
        exit("Error conectando a la base de datos");
    }
    //verifica si la BD existe
    if (!mysql_select_db($db_name, $link)) {
        exit("Error seleccionando la base de datos.");
    }
    return $link;
}



And I already changed the code like this:

<?php
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();

//this should the the absolute path to the config.php file
//(ie /home/website/yourdomain/login/config.php or
//the location in relationship to the page being protected - ie ../login/config.php )
require('../config.php');

//this should the the absolute path to the functions.php file - see the instrcutions for config.php above
require('../functions.php');
$link = Conectarse($db_server, $db_name, $db_username, $db_password);
var_dump($link);
$data = array();

function add_person( $first, $middle, $last, $email )
{
 global $data, $link;

  $sth = $link->prepare("INSERT INTO rutas_programadas (fecha, zona, ruta, area) VALUES(?, ?, ?, ?)");
 //$dbh->bindParam(1, $first);
 //$dbh->bindParam(2, $middle);
 //$dbh->bindParam(3, $last);
 //$dbh->bindParam(4, $email);
 $link->execute($sth,array($first,$middle,$last,$email));
 //$dbh->execute( $sth, array( $first, $middle, $last, $email ) );

 $data []= array(
   'first' => $first,
   'middle' => $middle,
   'last' => $last,
   'email' => $email
 );
}

if ( $_FILES['file']['tmp_name'] )
{
 $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
 echo $dom->saveXML();
 $rows = $dom->getElementsByTagName( 'Row' );
 $first_row = true;
 foreach ($rows as $row)
 {
   if ( !$first_row )
   {
     $first = "";
     $middle = "";
     $last = "";
     $email = "";
     $index = 1;
     $cells = $row->getElementsByTagName( 'Cell' );
     foreach( $cells as $cell )
     {
       $ind = $cell->getAttribute( 'Data' );
       if ( $ind != null ) $index = $ind;

       if ( $index == 1 ) $first = $cell->nodeValue;
       if ( $index == 2 ) $middle = $cell->nodeValue;
       if ( $index == 3 ) $last = $cell->nodeValue;
       if ( $index == 4 ) $email = $cell->nodeValue;

       $index += 1;
     }
     add_person( $first, $middle, $last, $email );
   }
   $first_row = false;
 }
}
?>
<html>
<body>
These records have been added to the database:
<table>
<tr>
<th>First</th>
<th>Middle</th>
<th>Last</th>
<th>Email</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['first'] ); ?></td><
<td><?php echo( $row['middle'] ); ?></td><
<td><?php echo( $row['last'] ); ?></td><
<td><?php echo( $row['email'] ); ?></td><
</tr>
<?php } ?>
</table>
Click <a href="list.php">here</a> for the entire table.
</body>
</html>



If I'm doing something wrong, feel free to smack me, LOL.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 02:26 PM

then you should call the variable $dbh not $link. though they have the same meaning to you, the PHP interpreter doesn’t know the concept of meaning (that should be that).
Was This Post Helpful? 0
  • +
  • -

#7 makaialexa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-May 06

Re: "Call to a member function prepare() on a non-object" Error

Posted 13 September 2010 - 07:47 PM

Guys, I managed to fix everything. I just removed the prepare and execute statements, and used the myqsl_query() functions, which worked wonders.

I appreciate a lot the people who tried to help me~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1