有人可以用下面的代码让我满意吗,我在这方面没有经验,我的 SQL 类是“很久以前,在一个遥远的星系......”我知道连接字符串是有效的,因为我使用过它与此应用程序的其他功能。在大多数情况下,我什至使用下面的代码从另一个函数中的另一个表中检索 * 行,只是我没有使用 WHERE 子句。
首先,我可以使用函数将 IP 地址存储在表中,并且运行良好。现在我想检查该表中是否存在给定的一个。部分代码如下。
似乎总是返回 0 行。我已将测试数据放入表中并对 $ipA 进行硬编码,但仍然返回 0 行。如果可能的话请提供帮助,并感谢您所付出的努力。
function checkDB($ipA) {
require_once('connection.inc.php');
$resultAns = "";
//create db connection
$conn = dbConnect();
//init prepared stmt
$stmt = $conn->stmt_init();
//Set sql query for ipAddress search
//prepare the SQL query
$sql = 'SELECT * FROM ipAddress WHERE ipA = ?';
//submit the query and capture the result
if ($stmt->prepare($sql)) {
$stmt->bind_param('s', $ipA);
$stmt = $stmt->execute();
//if qry triggers error affeted_rows value becomes -1 &
//php treats -1 as true; so test for greater than 0
$numRows = $stmt->num_rows; //not to sure about the syntax here
}
// I want to know if the query brought back something or not, I don't what
// to know exactly what, only that it found a match or did not find a match.
// echos are for testing purposes to show me where I am landing.
if ($numRows == 0) {
echo '<script type="text/javascript">window.alert("numRows = 0")</script>';
$resultAns = 0;
} elseif ($numRows == 1) {
echo '<script type="text/javascript">window.alert("numRows = 1")</script>';
$resultAns = 1;
}
return $resultAns;
}
最佳答案
尝试在执行后存储结果
$stmt->store_result();
关于php - 在 PHP 和 MySQL 中编写 SelectWhere 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313155/