简单:将php var传递到sql会导致mysqli查询失败

ymzxtsji  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(221)

在阅读了几个小时的stackoverflow并尝试了各种建议之后,我似乎无法使用简单的sql语句。
我正在使用最新的xampp localhost和apacheforphp和mysql。

r7knjye2

r7knjye21#


id |名称|交易|

1 |大卫| 1234v|

在phpmyadmin中,以下非变量sql返回1行。但是,变量sql返回一个非函数资源。
我为$mysqli->query()尝试了各种sql语句和语法的重新安排;
我是不是在php.ini中遗漏了什么?

$text = "here we have a long string of text with transaction ID: 1234V and some other stuff mixed in here.";

//lets cutup the string and only extract the transaction id
$array = explode("transaction ID: ", $text);

if (isset($array[1]))
    $array = explode("and", $array[1]);

$variable = $array[0]; //$array[0] = '1234V ';
$trans = "SELECT * FROM `name` WHERE `transaction` = '$variable';";

if($statement = $mysqli->prepare("$trans")){
    $statement->execute();
    $statement->bind_result($id,$name,$transaction);

    while ($statement->fetch()) {
        printf("%s %s\n",$id,$name,$transaction);
    }

    $statement->close();
}
$mysqli->close();
die();

带有$变量的新代码如下:

$trans = "SELECT * FROM `name` WHERE `transaction` = '$variable';";
mysqli_result 

Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 0 [type] => 0 )

新代码和硬代码打印如下:

$trans = "SELECT * FROM `name` WHERE `transaction` = '1234V ';";
mysqli_result

Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 1 [type] => 0 )
ars1skjm

ars1skjm3#

$trans = "SELECT id, name, transaction FROM `name` WHERE `transaction` = ? ;";

if($statement = $mysqli->prepare($trans)){
    echo 'Let\'s check we are in? '.$passed['variable']."\n";
    $statement->bind_param("s", $passed['variable']);

    $statement->execute();
    $statement->bind_result($id, $name, $transaction);

    while ($statement->fetch()) {
        printf("FETCHED: %s %s %s\n", $id, $name, $transaction);
    }
    echo "Let's check we are out? \n";
    $statement->close();
} else {
    echo $mysqli->error;
}
$mysqli->close();
xytpbqjk

xytpbqjk4#

你能试试这样的吗:

$trans = "SELECT * FROM `name` WHERE `transaction` = ?";

另外,我认为你应该这样做:

$statement = $mysqli->prepare($trans);
$statement->bind_param("s", $passed[variable]);
$statement->execute();
if(...

相关问题