0

I'm trying to swap place-holders with real values from an sql server by querying database and using the execute() function. I know the PDO syntax, but I'm having trouble with the mysqli equivalent.

I've set up, what I think appear to be correct, the mysqli equivalent of preparing the sql query, and executing it, but I receive an error.

Here is the PDO way of doing what I want:

$pdo = new PDO($dsn);
$stmt = $pdo->prepare(
    'SELECT
        title, created_at, body
    FROM
        post
    WHERE
        id = :id'
);
if ($stmt === false)
{
    throw new Exception('There was a problem preparing this query');
}
$result = $stmt->execute(
    array('id' => 1, )
);
if ($result === false)
{
    throw new Exception('There was a problem running this query');
}
// Let's get a row
$row = $stmt->fetch(PDO::FETCH_ASSOC);

Below is the code that I have created for the MySQLi way:

$servername = 'localhost';
$username = 'root';
$password = 'cassie123';
$database = 'blogdb';

//create connection
$conn = new mysqli($servername, $username, $password, $database);

//check connection
if($conn->connect_error){
    die('Connection failed: ' . $conn->connect_error);
}

$sql = 'SELECT title, created_at, body FROM post WHERE id = :id';
$stmt = $conn->prepare($sql);


if($sql === false){
    throw new Exception('There was a problem preparing this query');
}

$stmt->execute(array('id' => 1, ));

if($stmt === false){
    throw new Exception('There was a problem running this query');
}

$row = mysqli_fetch_array($sql, MYSQLI_BOTH);

So what I'm doing is swapping the placeholder which is the ':id') with the int 1 for the id, I know this is static, but I'm basically going to change this later for a variable, but for now I'm trying to assign 1 for the ':id'

I expect the output to print out the title, body, created_at for id = 1 in the mysql database.

The output is:

Uncaught error: Call to a member function execute() on boolean on line 24

I believe the problem has to do with the execute statement, but it looks pretty much identical to the PDO way, so Im trying to figure out the equivalent for MySQLi.

user3783243
  • 5,368
  • 5
  • 22
  • 41
DsDude
  • 135
  • 1
  • 8
  • 1
    Your prepare call is failing, and your check for that is wrong (you almost certainly meant `if($stmt === false)` rather than `if($sql === false)`) which is why you're not catching it. – Stephen Clouse Aug 15 '19 at 22:25
  • 3
    When in doubt about how a function works go to the manual. https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php Mysqli can't take values in the execute. Mysqli also doesn't support named placeholders. Use a `?`. – user3783243 Aug 15 '19 at 22:25
  • 3
    Additionally why are you moving from `pdo` to `mysqli`? That's like trading in a 2019 for a 2010. – user3783243 Aug 15 '19 at 22:28
  • @user3783243 I'm using MySQLi for the sake of this project because thats what I started with and what I'm just going to roll with for the rest of this project. I told myself I'll start using PDO after this. – DsDude Aug 15 '19 at 23:49
  • @user3783243 So would the query look like 'SELECT title, created_at, body FROM post WHERE id VALUES (?)' because I'm trying to select the title, body, created_at from a certain 'id' which would be $id = 1 in this case – DsDude Aug 16 '19 at 00:09
  • 2
    On a side note, the amount of code you are writing to run a single primitive query is insane. Please check out my [PDO examples](https://phpdelusions.net/pdo_examples), [mysqli articles](https://phpdelusions.net/mysqli/mysqli_connect) and [the proper error reporting principles](https://phpdelusions.net/articles/error_reporting) in general – Your Common Sense Aug 16 '19 at 02:14
  • @YourCommonSense I appreciate the links, and I'm going to read them. I'm very new to PHP and web development in general, so I'm in the learning process. Even though there are no comments in the code that I posted, I love knowing exactly what's going on with the statements and writing them out. – DsDude Aug 16 '19 at 02:33
  • But I understand now about the PHP errors, I see now that there is no need to be 'extra' with the error handling and the extra code is so unnecessary. – DsDude Aug 16 '19 at 02:40
  • The query would be `SELECT title, created_at, body FROM post WHERE id = ?`.., still not clear why you would take this approach though. It is going to be more work than the PDO approach. – user3783243 Aug 16 '19 at 04:08

0 Answers0