0

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;

  }
Salma
  • 17
  • 9
  • what data is in your client table? I mean is it single record? – Haris Aug 08 '17 at 13:46
  • what so you mean please! I didn't understand your question! – Salma Aug 08 '17 at 13:48
  • what do you mean by **result** ? do you want the records from database? If **yes** you have to `$q=mysqli_fetch_assoc($connect,$q)` before the `if` statement. – Haris Aug 08 '17 at 13:51
  • [Little Bobby](http://bobby-tables.com/) says **[your script is at risk for SQL Injection Attacks](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)**. Learn about [Prepared Statements](http://en.wikipedia.org/wiki/Prepared_statement) for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even **[escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)** is not safe! – GrumpyCrouton Aug 08 '17 at 13:52
  • @Anonymous I get error in authentication. – Salma Aug 08 '17 at 14:18
  • @GrumpyCrouton thank you, I will read it – Salma Aug 08 '17 at 14:18
  • @salma If you need help learning how to use [PDO](http://php.net/manual/en/book.pdo.php) for safe and secure queries, then you can check out [this answer that I wrote](https://stackoverflow.com/a/45514591/5827005) that demonstrates a function that I wrote that makes [Prepared Statements](https://www.w3schools.com/php/php_mysql_prepared_statements.asp) **easy**, **clean**, and **secure**. Alternatively you should learn how to use [Prepared Statements for MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – GrumpyCrouton Aug 08 '17 at 14:19
  • @Salma see the answer below. – Haris Aug 08 '17 at 14:21
  • Thank you Anonymous, In fact it's working with mysqli_fetch, you just need to write it as response so I can mark it as right! – Salma Aug 08 '17 at 14:37

1 Answers1

0

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;

  }
Anandhu Nadesh
  • 672
  • 2
  • 11
  • 20