简体   繁体   中英

how to put a result of query in variable with PHP

I am working with PHP and AngularJS, and I have a login.php file, Where it happens the verification of the user, and in this file, I store some information(email,token,..) in a vairable $result to use them in AngularJS. The problem that I have is that I need to store the result of a query in this variable to recuperate in AngularJS as I did for other variable. when I try this code, I recuperate the $cc variable where I stored the query result but, but it doesn't bear the query result , I get this :

cc: {current_field: "", field_count: "", lengths: "", num_rows: "", type: ""}

But in this variable I want to have the result of query. How can I do please?

login.php

<?php  

$data = json_decode(file_get_contents("php://input"));

 $connect = mysqli_connect("localhost", "root", "", "test");  

 if(count($data) > 0)  

 { 

$Email=mysqli_real_escape_string($connect, $data->Email);
$mdp=mysqli_real_escape_string($connect, $data->mdp);

$query = 'SELECT * FROM client  ';

$q = mysqli_query($connect , $query);


if(mysqli_num_rows($q) > 0 )
  {    

       $token = md5($Email.time()."51395+81519851");
       $query2 = "UPDATE client SET token = '".$token."' WHERE EmailClient = '".$Email."'";
       mysqli_query($connect , $query2);
       $_SESSION["logged_in"] = true; 
       $_SESSION["token"] = "51395+81519851"; 
       $_SESSION["Email"] = $Email; 
;
       $result['email'] =$Email;
       $result['role'] = 'client';
       $result['token'] = $token;
       $result["cc"] = $q ;


       $resultstring=json_encode($result);
       $resultstring=str_replace("null", '""', $resultstring);
       echo $resultstring;
       exit;

  }

What you are getting is an object. The query result is an object and it is supposed to be like this. if you want to extract the rows from the object you need to use

$res=mysqli_fetch_assoc($q);

then store $res to $result['cc'] ie;

$res=mysqli_fetch_assoc($q);
$result['cc']=$res;

Note that this will display only one row from your db. If u want to display all the rows, put the fetch_assoc in a while loop. like this

$query = 'SELECT * FROM client  ';

    $s=""; 

$q = mysqli_query($connect , $query);


if(mysqli_num_rows($q) > 0 )
  {    

       $token = md5($Email.time()."51395+81519851");
       $query2 = "UPDATE client SET token = '".$token."' WHERE EmailClient = '".$Email."'";
       mysqli_query($connect , $query2);
       $_SESSION["logged_in"] = true; 
       $_SESSION["token"] = "51395+81519851"; 
       $_SESSION["Email"] = $Email; 
;
       $result['email'] =$Email;
       $result['role'] = 'client';
       $result['token'] = $token;


    while($res=mysqli_fetch_assoc($q))
    {
    $s=$s.print_r($res);
    }
       $result["cc"] = $s ;


       $resultstring=json_encode($result);
       $resultstring=str_replace("null", '""', $resultstring);
       echo $resultstring;
       exit;

  }

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