0

I've been sitting on the same small problem now for over 10 hours, so it's time to ask stackoverflow! I'm connected to the database but when calling mysqli_stmt_bind_param I get "invalid object or resource".

I've tried the insert statement in the console and it works fine..

<?php
    $con=mysqli_connect("127.0.0.1:3306", "myUsername", "password");
    mysqli_select_db($con, "webshop");

    if (mysqli_connect_errno($con))
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $query= mysqli_stmt_init($con);

    mysqli_stmt_prepare($query, "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)");
    mysqli_stmt_bind_param($query, "ssssi", $name, $email, $hash, $address, $tel);
    if(mysqli_stmt_execute($query))
    {
        mysqli_close($con);
    }

?>

Thankful for any help at all!

2 Answers 2

2

You have to use the statement object returned by mysqli_stmt_prepare()

$stmt = mysqli_stmt_prepare($con, "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "ssssi", $name, $email, $hash, $address, $tel);
if(mysqli_stmt_execute($stmt))

Also, the mysqli_stmt_init($con) call is not needed (I think).

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

1 Comment

If I erase the init function then prepare gets an error "expects parameter 1 to be mysqli_stmt, object given". Is this telling me something is wrong with the connection? anyway, if I keep init and uses the new $stmt in param I get "expects parameter 1 to be mysqli_stmt, boolean given". So I guess $stmt is returning false, but why.
0

mysqli_stmt_init is needed as you are accessing mysqli using the procedural style.

This returns an object of type mysqli_stmt, which then acts as a container for the query you are building. As such, you should pass this as the first parameter to mysqli_stmt_prepare, mysqli_stmt_bind_param and mysqli_stmt_execute.

So your code would look like:

<?php
    $con=mysqli_connect("127.0.0.1:3306", "myUsername", "password");
    mysqli_select_db($con, "webshop");

    if (mysqli_connect_errno($con))
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $stmt = mysqli_stmt_init($con);
    $query =  "INSERT INTO user (name, email, hash, address, tel) VALUES (?, ?, ?, ?, ?)";

    mysqli_stmt_prepare($stmt, $query);
    mysqli_stmt_bind_param($stmt, "ssssi", $name, $email, $hash, $address, $tel);
    if(mysqli_stmt_execute($stmt))
    {
        mysqli_close($stmt);
    }

?>

One, unrelated point - you appear to be requiring that your tel field (which I presume to be a telephone number) is an integer. This might be a bad idea if you have to handle telephone numbers starting with 0 (common in the UK for example) at any point.

1 Comment

Yes this is the way to use mysqli correctly. It's kinda the same as my originally solution. The problem I had was not this code, but that my user I used wasn't privileged to access insert. Which was very hard to find.

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.