简体   繁体   中英

how correctly get country and other info from sql for specific id

I am trying to get country and other info from sql for specific id, only " id " displays correctly in both cases.

<?php
    $sql = "SELECT * FROM `list`  ORDER BY category ASC";
    $result = mysql_query($sql);
    $rows = mysql_fetch_assoc($result);      
    $id = $_GET['id'];
    $country = $_GET['country'];

    echo $id;
    echo $country;  
?>

and

<?php
    $sql = "SELECT * FROM `list`  ORDER BY category ASC";
    $result = mysql_query($sql);
    $id = $_GET['id'];
    $country = $_GET['country'];

    if (mysql_num_rows($result) > 0) {
    while($rows = mysql_fetch_assoc($result)) {

    echo $id;
    echo $country;  
    }
    } 
?>

I think you have a column named id and another named country from which you want to collect the data in the country row of a certain record. I have similar ideas to some of the comments and would like to highlight my thoughts in this answer.

  1. $_GET superglobal is used to extract data form a URL parameter. If it is used to get the data from the record, you might want to use mysql_fetch_assoc($result)["country"]. For more information, please take a look at https://www.php.net/manual/en/function.mysql-fetch-assoc.php and https://www.w3schools.com/php/php_superglobals_get.asp

  2. To filter certain records according to some input id, you would want to use the WHERE clause. Link for more information:- https://www.w3schools.com/sql/sql_where.asp The possible code might be:-

<?php
$id = $_GET["id"]
$sql = "SELECT * FROM `list` WHERE id='$id' ORDER BY category ASC";
$result = mysql_query($sql);

if (mysql_num_rows($result) > 0) {
    while($rows = mysql_fetch_assoc($result)) {
        echo $rows['id'];
        echo $rows['country'];  
    }
} 
?>

PS: I have tried my best to answer the asked question provided the less information given. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM