dongtaidai0492 2014-01-15 13:27
浏览 40
已采纳

从mysql数据库中回显特定的行

I have created a form that ask the user to add: first_name, last_name,location,status

After some time i have received 5 inputs. The mysql table is named users and the table data are like below:

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  

Also the way i am receiving the inputs its via POST method. So:

$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">

I created a query to select all users from UK that are married:

$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);

In other words:

id first_name location status  
== ========== ======== ========  
1   Chris       UK       Married  
5   George      UK       Married

Questions:

1) Does the $row array look like below:

$row= array(
array(1 Chris UK Married),
array(5 George UK Married) 
);

2) How do i echo the content of the database after implementing the filtering WHERE location='UK' AND status='Married'?

i need it to be like this:

Hello Chris! You are from UK and you are married!

Hello George! You are from UK and you are married!

I know that i have to use foreach loop (echo arrays) but i have tried it and it doesnt work.One of the things i tried was something i found in php.net:

Unpacking nested arrays with list()

(PHP 5 >= 5.5.0)

PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.

For example:

<?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
";
}
?> 

When i use the above i am receiving the error below:

Parse error: syntax error, unexpected T_LIST in C:\wamp\www....

Any suggestions?

As i understand, i somehow have to link the ID with the other data..

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dsf12123 2014-01-15 13:37
    关注

    You may use something like this:

    $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!
    ", $row['first_name'], $row['location'],$row['status']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号