Page 1 of 1

OOP class inside a function

Posted: Wed Sep 02, 2009 3:11 am
by orchid1
quick question here
I bet this is real simple but
I get an error that reads
"Call to a member function prepare() on a non-object"
seems like it must be a pretty common error and yet I cannot find anything on it that explains why I am getting this in my php
I tried a million things but no luck
anybody know what im doing wrong??
help please :?:

Code: Select all

 
<?php
    function upload_images($selector, $strainName, $clubId){
        if($stmt = $mysql -> prepare("INSERT INTO products (selector, labelLink, clubId) VALUES(?,?,?)")) {
         $stmt -> bind_param("sss", $selector, $strainName, $clubId);
         $stmt -> execute();
            $stmt -> close();
        }
    }
 
}
if(array_key_exists('send',$_POST)){
 
    $var1=ucfirst( $_POST['strain']);
    $var2= ucfirst($_POST['selector']);
    $var3= $displayinfo;
    
upload_images($var1, $var2, $var3);
 
}
 
?>
 

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 3:17 am
by papa
function upload_images($mysql, $selector, $strainName, $clubId)

Edit: Oops, edited code.

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 3:24 am
by stratbeans
PHP is not accepting $mysql as a valid object. Where is $mysql defined ?

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 10:02 am
by orchid1
$mysql is just a connection variable
i.e.

Code: Select all

 
<?php
$mysql= mysqli_connect("localhost", "MyUser", "MyPass", "Mydatabase");
    if (!$mysql) {
    echo "Cannot connect to database";
    exit;
    }
 
?>
 
thank you for the help
I'm gonna try passing $mysql to the function
and I'll get back to the function
sorry about the delay I posted at 1 am Im in the US its 8 am now and I just woke up
Thanks again Im going to try it now

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 10:20 am
by orchid1
Nope that does not work
a.k.a

Code: Select all

 
<?php
    function upload_images($selector, $strainName, $clubId, $conn){
                            if($stmt = $conn->prepare("INSERT INTO products (selector, labelLink, clubId) VALUES(?,?,?)")) {
                              $stmt -> bind_param("sss", $selector, $strainName, $clubId);
                              $stmt -> execute();
                              $stmt -> close();
                    
                            }
 
if(array_key_exists('send',$_POST)){
 
    $var1=ucfirst( $_POST['strain']);
    $var2= ucfirst($_POST['selector']);
    $var3= $displayinfo;
    
upload_images($var1, $var2, $var3, $mysql);
 
}
 
?>
 
 
any more ideas ??

im at a loss :banghead:

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 11:26 am
by Ollie Saunders
Put var_dump($mysql) before line 17 in your posted code. It might help if you also have error_reporting set to E_ALL.

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 12:32 pm
by orchid1
ill give var_dump() a try

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 1:04 pm
by orchid1
Nope doesn't work
Ive got an error in my code that I think is unrelated Ill fix it and get back

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 1:15 pm
by Luke
var_dump wasn't supposed to fix your code. It just allows you to inspect the variable to make sure it contains what you think it contains. What happened when you did var_dump($mysql) ?

Re: OOP class inside a function

Posted: Wed Sep 02, 2009 1:20 pm
by arjan.top
you have to add global $mysql to access global variable