0

Firstly I got the workers name from BIRTHDAYS and then want to get e-mail address from USERS.There is no problem to take workers name's from Table1 but when I try to get the e-mail addresses the db returns me NULL.My DB is mssql.

<?php
include_once("connect.php");
$today = '05.07';
$today1 = $today . "%";
$sql = "SELECT NAME FROM BIRTHDAYS WHERE BIRTH LIKE '$today1' ";
$stmt = sqlsrv_query($conn,$sql);
if($stmt == false){
    echo "failed";
}else{
   $dizi = array();
   while($rows = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
   {
    $dizi[] = array('NAME' =>$rows['NAME']);
    $newarray = json_encode($dizi,JSON_UNESCAPED_UNICODE);
   }
 }

    foreach(json_decode($newarray) as $nameObj) 
{
    $nameArr = (array) $nameObj;
    $names = reset($nameArr);
    mb_convert_case($names, MB_CASE_UPPER, 'UTF-8');
    echo $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
    echo "<br>";
    $stmt2 = sqlsrv_query($conn,$sql2); 
        if($stmt2 == false)
        {
        echo "failed";
        }
        else
        {
        $dizi2 = array();   
        while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
        {   
            $dizi1[] = array('EMAIL' =>$rows['EMAIL']);
            echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
        }   
    }
}   
?>
12
  • 1
    That's good, but what is the actual query that you're executing? When you debug this (for example, you echo that value, so what is that value?) what specifically happens? Commented Jul 3, 2018 at 13:31
  • 1
    Why do you json_encode and then immediately json_decode afterwards in the same piece of code? Seems a bit of pointless overhead...since it's all happening in PHP you may as well just pass PHP variables around. The point of things like JSON and XML is to serialise data into text for transmission to another system or program which cannot directly read the variables in the current program. Commented Jul 3, 2018 at 13:35
  • 1
    Also, is querying on the names really very robust? It's pretty common to have more than one person called "David" for instance, so how are you going to tell them apart? Better to use an ID to match them, or something else which is generally unique to an individual, such as their email address. In other words, in the Birthdays table, don't store the user's name, store their ID, which is also present in the Users table. And create a foreign key relationship between them. That way you can also do this whole thing in one single query using an INNER JOIN. Commented Jul 3, 2018 at 13:36
  • 1
    If you haven't studied database design, entity-relationships and normalisation, now would be a great time to learn. You'd be able to write, for example, SELECT Email From Users INNER JOIN Birthdays on User.ID = Birthday.UserID WHERE Birth LIKE '05.07%' and get the emails all of the users whose birthday matches the LIKE criteria Commented Jul 3, 2018 at 13:38
  • 1
    " it return [email protected] in db site but not in my PHP"...so in that case there must be some difference. This clearly implies that in PHP you are either a) not in fact executing the same query (so please echo the finished SQL of your queries so you can check, as mentioned above), or b) not in fact connecting to the same copy of your database, where one contains different data to the other. Commented Jul 3, 2018 at 13:39

1 Answer 1

1
 while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
        {   
            $dizi1[] = array('EMAIL' =>$rows['EMAIL']);
            echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
        } 

you put in $rows1 and would take it from $rows NULL is correct answer :)

take $rows1['EMAIL'] and it would work

and why foreach =?

you can put the statement in while-loop like this:

  while ($rows = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

    $names = $rows['NAME'];
    $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
    echo "<br>";
    $stmt2 = sqlsrv_query($conn, $sql2);
    if ($stmt2 == false) {
        echo "failed";
    } else {
        $dizi2 = array();
        while ($rows1 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
            $dizi1[]        = array('EMAIL' => $rows1['EMAIL']);
            echo $newarray1 = json_encode($dizi1, JSON_UNESCAPED_UNICODE);
        }
    }

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

1 Comment

@DarthMaul: The takeaway here is to use meaningful variable names. "Rows" doesn't tell you anything about the data, all relational data is in rows. And "Rows1" doesn't distinguish itself from "rows" in any meaningful way. Names are important. Use names that mean something and you'll be able to read your own code.

Your Answer

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