Php 如何检索多行数据?
如何检索多行数据?目前我只能检索一行数据,但我想检索多行数据 我想检索用户在数据库中的凭证。例如,user的“name”是mark,我想检索列“name”是mark的每一行Php 如何检索多行数据?,php,mysql,Php,Mysql,如何检索多行数据?目前我只能检索一行数据,但我想检索多行数据 我想检索用户在数据库中的凭证。例如,user的“name”是mark,我想检索列“name”是mark的每一行 // check for post data if (isset($_GET["name"])) { $name = $_GET['name']; $result = mysql_query("SELECT * FROM coupons WHERE name = '$name'"); if (!empty($resu
// check for post data
if (isset($_GET["name"])) {
$name = $_GET['name'];
$result = mysql_query("SELECT * FROM coupons WHERE name = '$name'");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$coupons = array();
$coupons["couponcreated"] = $result["couponcreated"];
$coupons["couponexpires"] = $result["couponexpires"];
$coupons["coupondetails"] = $result["coupondetails"];
// success
$response["success"] = 1;
// user node
$response["coupons"] = array();
array_push($response["coupons"], $coupons);
// echoing JSON response
echo json_encode($response);
此外,您必须保护$name变量并清除其中的任何恶意字符串,这样就没有人可以通过SQL注入攻击窃取您的SQL数据或注入数据
这是一个例子,尽管它并不完美:
$name = htmlspecialchars($name);
使用str_replace或您知道的任何字符串操作函数从$name筛选任何SQL命令。实际上,您可以将$result=mysql\u fetch\u array$result更改为循环,循环完成后,您的数组将完全填充。我只是复制粘贴吗?还是我必须相应地改变?我相应地修改了答案。结果现在将包含一个数组,所以您也需要修改。首先,您应该使用准备好的语句来降低错误的风险。第二第三,当$row=mysql\u fetch\u数组$result{/$row['couponcreated']包含couponcreated列的此行值时,可以循环遍历结果}
$name = htmlspecialchars($name);