0

I have a set of functions:

One creates the connection, the others do different kinds of database interaction using that function.

I am retrieving the error;

Call to undefined method mysqli::execute()

this is telling me that I am failing to send the object through correctly, I have done some research but failed to find examples;

<?php
function con(){

$mysqli = new Mysqli("localhost","user","pass","image_blog");

if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}else{
    return $mysqli;
}};

function getAll(){

$mysqli = con();

$stmt = $mysqli->prepare("SELECT * FROM posts");
$stmt = $mysqli->execute();     <--- ERROR HERE
$stmt = $mysqli->get_result();
$stmt = $mysqli->close();

while($row = mysqli_fetch_array($stmt, MYSQL_ASSOC)) {
  echo "Username: " . $row["image"];
  echo "Username: " . $row["title"];
  echo "Username: " . $row["text"];
  echo "Username: " . $row["up"];
  echo "Username: " . $row["date"];
}
};
function anotherFunction(){

 }
 function yetAnotherFunction(){

 }

3 Answers 3

1

you should execute the statement ($stmt) it self not the $mysqli

$stmt->execute();

also consider using PDO class in the futur as it makes things mush easier

Sign up to request clarification or add additional context in comments.

Comments

0

I see what that you're trying to achieve a connection function with your mysqli. Rather than connecting from each function individually. As Mr. Smith has mentioned, you've declared the variable $stmt like:

$stmt = $mysqli->prepare('SELECT * FROM posts');

This is incorrect as a prepared statement doesn't return data to be used in the variable, hence your error.

Another method would be to use classes, and yet another more advanced method would be to extend the mySQLi class.

Class myClass{
    protected $dbh;
    public function __construct(){
        $this->dbh=new mysqli('','','','');
    }
    public function getAll(){
        $this->dbh->prepare('SELECT * FROM posts');
        $this->dbh->execute();
        $this->dbh->bind_result($image,$title,$text,$up,$date);
        while($this->dbh->fetch()){
            echo"Username:".$image;
            //and so on
        }
        $this->dbh->close();
    }
    public function anotherFunction(){
    }
    public function yetAnotherFunction(){
    }
}

If you haven't used classes before you now need to create an instance of the class:

$databaseUser = myClass();

And to start your function:

$databaseUser->getAll();

Comments

0

It should be

$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$mysqli->close();

Instead of

$stmt = $mysqli->execute();
$stmt = $mysqli->get_result();
$stmt = $mysqli->close();

See official docs (examples too).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.