prepare" Can I export a query formatted mysqli::prepareand ::bind_param? Example:

Export "query" from "mysqli-> prepare"

Can I export a query formatted mysqli::prepareand ::bind_param?

Example:

<?php
$mysqli = new mysqli('host', 'user', 'pass', 'table');
if(mysqli_connect_errno()){
    printf('Connect failed: %s\n', mysqli_connect_error());
    exit;
}

$data=7290;

if ($stmt = $mysqli->prepare('SELECT `id`,`info` FROM `propertys` WHERE id>?')){
    $stmt->bind_param('i',$data);
    $stmt->execute();
    $stmt->bind_result($id,$info);
    while($q=$stmt->fetch()){
        echo $id,': ',$info,'<br>';
    }
    $stmt->close();
}
$mysqli->close();
?>

I would like to export the functions QUERYperformed mysql::prepareand bind_param therefore (this is an imaginary example):

if ($stmt = $mysqli->prepare('SELECT `id`,`info` FROM `propertys` WHERE id>?')){
    $stmt->bind_param('i',$data);
    $stmt->execute();
    echo $stmt->exportQuery();//Function does not exist, just for example

The function ::exportQuerywill print as follows:

SELECT `id`,`info` FROM `propertys` WHERE id>7290

is there any solution?

Thank.

+5
source share
3 answers

I know that this would be useful for debugging, but thatโ€™s not how prepared statements work. Parameters are not combined with a prepared statement on the client side. PHP should never have access to the query string in combination with its parameters.

SQL (), , (). MySQL SQL , (). . CLI mysql, PHP, .

081016 16:51:28 2 Query       prepare s1 from 'select * from foo where i = ?'
                2 Prepare     [2] select * from foo where i = ?
081016 16:51:39 2 Query       set @a =1
081016 16:51:47 2 Query       execute s1 using @a
                2 Execute     [2] select * from foo where i = 1

:

@Baily , MySQL , . PHP.

, , MySQL, PHP API:

SET GLOBAL general_log = ON;

, , .

SET GLOBAL general_log = OFF;

PS: MySQL 5.1 . mysqld .

+6

, , .

, - .

, , .

//Assume you're using $_GET to get the id
$data = mysql_real_escape_string($_GET['yourID']);

$yourStatement = 'SELECT `id`,`info` FROM `propertys` WHERE id>';
$savedStatement = $yourStatement.$data;

echo $savedStatement;
//Will return 'SELECT `id`,`info` FROM `propertys` WHERE id>4'

if ($stmt = $mysqli->prepare($yourStatement.'?')){
$stmt->bind_param('i',$data);
$stmt->execute();
  }
0

- :

if ($stmt = $mysqli->prepare('SELECT `id`,`info` FROM `propertys` WHERE id>?')){
    $stmt->bind_param('i',$data);
    if($stmt->execute()){
        echo 'SELECT `id`,`info` FROM `propertys` WHERE id>'.$data;
    };
}

, , :

, MySQL?

-1

All Articles