1

I'm trying to get somes fields of a DB to import in another DB. So, I've connected to my DB with mysqli.

$mysqli = new mysqli("localhost", "root", "password", "db_name");
if ($mysqli->connect_errno) {
    echo "$mysqli->connect_errno";
} else {
    echo "You're IN"."<br />";
}

Now, I select the PK of the table:

$emails = $mysqli->query("SELECT email FROM customers WHERE date_first_order IS NOT NULL AND date_first_download IS NOT NULL");

I know that I've an array at this moment in $emails. So, I use a foreach loop.

foreach($emails as $email) {
        $email = $email["cl_email"];
        $query_name = $mysqli->query("SELECT name FROM customers WHERE email = $email");

        $query_lastname = $mysqli->query("SELECT lastname FROM customers WHERE email = $email");
        echo $email." ".$query_name." ".$query_lastname."<br />";

    }

Here is my problem, with the query_name and query_lastname vars I get an array, an not an String. So, How I can get the field that I'm looking for?

Thanks.

josearcos
  • 15
  • 5

4 Answers4

1

When using MySQLi you should use mysqli_result::fetch_assoc() if you want to get each row as an associative array. http://php.net/manual/en/mysqli-result.fetch-assoc.php

while ($row = $result->fetch_assoc()) {
    printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
Jonathon
  • 15,873
  • 11
  • 73
  • 92
0

I hope this is a string, and you should firstly place it into ''.

$relation= $mysqli->query("SELECT name, lastname FROM customers WHERE email = $email");
-----------------------------------------------------------------------------^------^

And fetch row.

$row = $relation->fetch_row());

Secondly, you can simply return from array the wanted key.

echo $email . " " . $row["name"] . " " . $row["lastname"] . "<br />";
m1k1o
  • 2,344
  • 16
  • 27
0

As mentioned by @jonathon mysqli-result::fetch-assoc() should help you with this. Also, i don't see any reason you have to do these queries. I believe you can get the desired output into single query too :)

$query_name = $mysqli->query("SELECT email,name,lastname FROM customers WHERE date_first_order IS NOT NULL AND date_first_download IS NOT NULL"); while ($row = $query_name->fetch_assoc()) { print $row['email'].$row['name'].$row['lastname']; }

Peeje
  • 445
  • 2
  • 9
0

You could use implode(",",$yourvariable); It changes your array to a string. you may want to separate the results with a comma or you can just put ("",$yourvariable); Hope this helps!:)

Jolene
  • 3
  • 5