不知道如何从mysql中回显出Selected数据

时间:2016-05-14 04:58:49

标签: php mysql mysqli prepared-statement sanitization

>>> contains("lit", "litter")
True
>>> contains("thot", "thurtle")
False
>>> contains("ratchet", "ramtbunchiousest")
True
>>> contains("shade", "hadsazie")
False

我希望它回显$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ?"); $stmt->bind_param("s", $query); $stmt->execute(); $stmt->store_result(); if ($stmt->affected_rows > 0) { echo "Exists"; } ,而不是回显Exists。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

首先,如果您想要只有一行,请将LIMIT 1添加到SELECT查询中,如下所示:

$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");

因此有两种方法可以显示nameData

方式(1):

首先将变量$nameData绑定到预准备语句,然后将结果提取到此绑定变量中。

$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");
$stmt->bind_param("s", $query);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows){
    $stmt->bind_result($nameData);
    $stmt->fetch();
    echo $nameData;
}else{
    echo "No result found";
}

方式(2):

首先使用get_result()方法从预准备语句中获取结果集,然后使用fetch_array从结果集中获取结果行。

$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");
$stmt->bind_param("s", $query);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows){
    $row = $result->fetch_array()
    echo $row['nameData'];
}else{
    echo "No result found";
}

答案 1 :(得分:0)

我认为你可以在代码下面我希望你的查询工作正常,它可以正常返回结果然后你可以使用下面的代码。

$stmt->bind_result($nameData);
    if ($stmt->fetch()) {
                printf ("%s\n", $nameData);
            }

答案 2 :(得分:0)

请注意,<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div id="tes"> </div> <!-- Modal Currency--> <div class="modal fade" id="priceModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> <!-- /.modal-content --> </div> <!-- /.modal-dialog --> </div>在这里不会做任何有用的事情 但是,您也不需要affected_rows(因此也需要store_result)

num_rows

考虑到所有麻烦,即使没有无用的功能,您可能会发现PDO更好的方法:

$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ?");
$stmt->bind_param("s", $query);
$stmt->execute();
$stmt->bind_result($nameData);
$stmt->fetch();
echo $nameData;