Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a function like bind_param() in PHP?

Tags:

php

mysql

mysqli

I am trying to write a small shell for MySQL, which has some basic functionality which I need for my code. This is for test purposes and to get to know PHP a bit more.

The problem I encounter is the following:

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )

Above is the exact definition I find on the internet, but I cannot make my function like this (syntax errors, or server says: I need a var of type 'mixed').

public function bind($types, $values)
{
    $this->statement->bind_param($types, $values);
}

The above is roughly what I would like to achieve, but even with searching the web, I'm unable to find a solution.

like image 822
Techno Avatar asked Nov 18 '25 15:11

Techno


1 Answers

The type "mixed" doesn't exist, it just says that the parameter passed might be string or might be integer or anything. You should write your function with one parameter, $types and all the other parameters come from "func_get_args()". This passes all the parameters given to the function so you can work with them like an array:

function bind_param(string $types)
{
$args = func_get_args();
print_r($args);
}

bind_param("foo", "bar", 123, false, array("a","b"));

Note that it will include the first $types parameter too.

like image 82
tvili999 Avatar answered Nov 20 '25 06:11

tvili999



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!