0

How i can send $country to second code (select emr_value from countries where country_name='$country') i don't know why $country = "" or null please help

thanks

  <?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();

?>

Second Code (Here i want to display data for this query (select emr_value from countries where country_name='$country'))

<?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();

?>
5
  • include the first file into second one and use the variable. and it's not needed to specify the connections and global variable there. It should work. Commented Sep 8, 2015 at 8:20
  • Why have 2 files instead of having 1? Commented Sep 8, 2015 at 8:25
  • study use one php file's variable in another Commented Sep 8, 2015 at 8:27
  • its a one page, how i can solved it, and i see this problem when echo sql = select emr_value from countries where country_name=' Jordan' ,, the problem is a space before variable here - ' Jordan' Commented Sep 8, 2015 at 8:27
  • use ...where country_name =trim($country) It'll take the query as: ... where country_name = 'Jordan' without the leading or trailing spaces within the variable value. And if it's all in same file then no need of include Commented Sep 8, 2015 at 8:31

1 Answer 1

0

Maybe you should have only one code:

<?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();

?>

Or create two functions in one code: one that returns $country and one that returns emr_value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.