提问者:小点点

mysql查询x=$变量


谢谢

  <?php

 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "db";
global $country;
// Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 }

 $sql = "SELECT meta_value FROM `wp_usermeta` where user_id=$user_id and meta_key='custom_field_6' ";
 $result = $conn->query($sql);



 if ($result->num_rows > 0) {
    // output data of each row
     while($row = $result->fetch_assoc()) {

    $country = $row["meta_value"];
    echo $country;


     }
 } else {
     echo "0 results";
 }
$conn->close();

?>

第二个代码(在这里我想显示此查询的数据(选择emr_value从country_name=$国家))

<?php

 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "db";
global $country;
// Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 }

 $sql = "select emr_value from countries where country_name='$country'";

 $result = $conn->query($sql);


     while($row = $result->fetch_assoc()) {

    $emr_value = $row["emr_value"];
    echo "<h1>EMR : " . $emr_value . "</h1>";   
}
$conn->close();

?>

共1个答案

匿名用户

也许您应该只有一个代码:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT meta_value FROM wp_usermeta WHERE user_id='$user_id' AND meta_key='custom_field_6'";
$result1 = $conn->query($sql);

if ($result1->num_rows > 0) {
    // output data of each row
    while ($row1 = $result1->fetch_assoc()) {
        $country = trim($row1["meta_value"]);
        $sql = "SELECT emr_value FROM countries WHERE country_name='$country'";
        $result2 = $conn->query($sql);

        while ($row2 = $result2->fetch_assoc()) {
            echo "<h1>EMR : " . $row2["emr_value"] . "</h1>"; 
        }
    }
} else {
    echo "0 results";
}

$conn->close();

?>

或者在一个代码中创建两个函数:一个返回$country,另一个返回emr_值。

相关问题