Echo specific rows from a mysql database

10,768

Solution 1

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!\n", $row['first_name'], $row['location'],$row['status']);
}

Solution 2

mysqli_fetch_array returns a single row. You'll need to loop to get all rows. You can get the value from the row and place it in a string like:

while($row = mysqli_fetch_array($results, MYSQLI_BOTH)){
    echo "Hi ".$row['first_name'] . " You are from ".$row['location']." and you are ".$row['status']."!";
}
Share:
10,768
christostsang
Author by

christostsang

Updated on June 05, 2022

Comments

  • christostsang
    christostsang almost 2 years

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

    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.