Search code examples
phpprepared-statementezsql

prepared statement with ezsql


I find ezSQL library very useful but as far as I see there is no implementation of prepared statements in it, am I right? Or is there something I don't know?

I have checked out the help file which I downloaded from http://justinvincent.com/ezsql

For example: I have some queries like

$stmt = $conn->prepare("INSERT INTO gecici_magaza_detay VALUES ($geciciMagazaId,?,?,?,?)");
$stmt->bind_param("iiss",$gunId,$acikMi,$girisSaati,$cikisSaati);
for($j=0; $j<7; $j++) {
    $gunId = $j+1;
    $acikMi = (empty($acilis[$j]) || empty($kapanis[$j])) ? 0 : 1;
    $girisSaati = $acikMi ? $acilis[$j] : null;
    $cikisSaati = $acikMi ? $kapanis[$j] : null;
    $stmt->execute();
}

where $conn is a mysqli object.

$conn = new mysqli($servername, $username, $password, $dbname);

but I want to get rid of it completely and use only my $db object which is:

$db = new ezSQL_mysqli();

I hope there is a way of using prepared statements with ezSQL, that would make me more comfortable, otherwise I'll have to use both.


Solution

  • I know this is an old question, but there are options for prepared statements from v3.08+.

    When you create your connection you simply use $db->prepareOn();. Here's an example using this code

    // To get SQL calls to use prepare statements
    $db->prepareOn(); // This needs to be called at least once at instance creation
    
    $db->query_prepared('INSERT INTO profile( name, email, phone) VALUES( ?, ?, ? );', [$user, $address, $number]);
    
    $db->query_prepared('SELECT name, email FROM profile WHERE phone = ? OR id != ?', [$number, 5]);
    $result = $db->queryResult(); // the last query that has results are stored in `last_result` protected property
    
    foreach ($result as $row) {
        echo $row->name.' '.$row->email;
    }
    

    More information can be found on the new Wiki