我创建了一个表单,要求用户添加:first_name,last_name,location,status
一段时间后,我收到了5个输入。mysql表被命名为users,表数据如下:
id first_name location status == ========== ======== ======== 1 Chris UK Married 2 Anton Spain Single 3 Jacob UK Single 4 Mike Greece Married 5 George UK Married
同样,我通过POST方法接收输入的方式。所以:
$firstname=$_POST['FIRST_NAME']; // First Name: <input type="text" name="FIRST_NAME"> $location=$_POST['LOCATION']; // Location: <input type="text" name="LOCATION"> $status=$_POST['STATUS']; // Status: <input type="text" name="STATUS">
我创建了一个查询来选择来自英国的所有已婚用户:
$query = "SELECT * FROM users WHERE location='UK' AND status='Married'"; $result = mysqli_query($dbc,$query); //$dbc is the connection to my database $row = mysqli_fetch_array($results, MYSQLI_BOTH);
换一种说法:
id first_name location status == ========== ======== ======== 1 Chris UK Married 5 George UK Married
问题:
1)$ row数组是否如下图所示:
$row= array( array(1 Chris UK Married), array(5 George UK Married) );
2)在实施过滤后,我如何回显数据库的内容WHERE location =’UK’AND status =’Married’?
我需要它是这样的:
Hello Chris! You are from UK and you are married! Hello George! You are from UK and you are married!
我知道我必须使用foreach循环(echo数组),但是我已经尝试过了,但它不起作用。我尝试的一件事是在php.net中找到的东西:
用list()解开嵌套数组
(PHP 5> = 5.5.0)
PHP 5.5通过提供list()作为值,增加了遍历数组数组并将嵌套的数组解包到循环变量中的功能。
例如:
<?php $array = [ [1, 2], [3, 4], ]; foreach ($array as list($a, $b)) { // $a contains the first element of the nested array, // and $b contains the second element. echo "A: $a; B: $b\n"; } ?>
当我使用以上内容时,我收到以下错误:
Parse error: syntax error, unexpected T_LIST in C:\wamp\www....
有什么建议?
据我了解,我必须以某种方式将ID与其他数据相关联。
提前致谢。
您可以使用如下所示的内容:
$query = "SELECT * FROM users WHERE location='UK' AND status='Married'"; $result = mysqli_query($dbc,$query); while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){ printf("Hello %s! You are from %s and you are %s!\n", $row['first_name'], $row['location'],$row['status']); }