MySql column value by searching of another value in particular record


#1

Hello,

I’m trying to get username with search by inputted string of name or ID value from particular record columns in MySQL database table with ID, name and username. If I search for Leila in name column, I want get username column leila77 or ID 3:

ID | name  | username
------------------------
1  | Sally | sally123
2  | Jack  | jack120
3  | Leila | leila77
4  | Aaron | aaron800 
5  | Sara  | sara444

TS:

check() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let options = new RequestOptions({ headers: headers });

    let postParams = '&name=' + this.name;

    this.http.post("http://site.info/php/check.php", JSON.stringify(postParams), options)
      .subscribe(data => {
        console.log(data['_body']);
      }, error => {
        console.log(error);
      });
  }

For example I can check if value of inputted name exist in database table column in check.php this way:

    <?php
    require 'connection.php';

    $name = $_POST['name'];

    $sql = "SELECT * from nameTab where name='$name'";

    if (mysqli_query($con, $sql)==1)
    {
        echo 'Name exist';
    }
    else
    {
        echo 'Name does not exist';
    }
    ?>

but to get other columns of same record with this way:


<?php
require 'connection.php';

$name = $_POST['name'];

$sql = "SELECT * from nameTab where name='$name'";

$result = mysqli_query($con, $sql);

while($row = $result->fetch_assoc()) 
{
   echo $row["username"];
}
?>

not sure how to get this value, console does not outputs echo, seems like my .ts front end is not proper, advice would be helpful:


#2

Do a console.log on data to see what is returned (or take a look at the network request)


#3

@lado If you are using JSON.stringify(postParams), then postParams should be a proper object to be stringified, like:

postParams = { name: this.name }

Try to do this way and show an echo of name in the backend:

$name = $_POST['name'];
echo 'Name: ' . $name;

#4

Hello @lucasbasquerotto

Thank you for answer

If I get you right, my back end should look like this:

check.php:

<?php
require 'connection.php';

$name = $_POST['name'];

$sql = "SELECT * from nameTab where name='$name'";

$result = mysqli_query($con, $sql);

while($row = $result->fetch_assoc()) {
    echo "username: " . $row["username"] . "with id: " . $row["ID"];
}
?>

and Type Script front end:


  check() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let options = new RequestOptions({ headers: headers });

   let postParams = '&name=' + { name: this.name};

    this.http.post("http://site.info/php/check.php", JSON.stringify(postParams), options)
      .subscribe(data => {
        console.log(data['_body']);
      }, error => {
        console.log(error);
      });
  }

Not sure what is wrong here, my console, same as with code above:


#5

Hello @Sujan12

I have added edit to @lucasbasquerotto answer above, check it please


#6

@lado No. Your backend should be like:

<?php
require 'connection.php';

$name = $_POST['name'];
echo 'Name: ' . $name; // add this line so you can see what you receive as name on your back-end

$sql = "SELECT * from nameTab where name='$name'";

$result = mysqli_query($con, $sql);

while($row = $result->fetch_assoc()) {
    echo "username: " . $row["username"] . "with id: " . $row["ID"];
}
?>

Instead of $_POST['name'] you could try:

// (remove it) $name = $_POST['name'];
$request = json_decode(file_get_contents("php://input"));
echo 'JSON: ' . $request;

$name = $request->name;
echo 'Name: ' . $name;

(Because you are sending a JSON)

Your front-end should be like:

  check() {
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let options = new RequestOptions({ headers: headers });

   let postParams = { name: this.name };

    this.http.post("http://site.info/php/check.php", JSON.stringify(postParams), options)
      .subscribe(data => {
        console.log('data', data); // first see what you receive as data
      }, error => {
        console.log(error);
      });
  }

If you don’t receive the name (echo 'Name: ' . $name; outputs nothing as the name), try without the stringify:

this.http.post("http://site.info/php/check.php", postParams, options)

Or try with FormData:

let body = new FormData();
body.append('name', this.name);
this.http.post("http://site.info/php/check.php", body , options)

I’m not a http expert though, but you can search examples in the web, like:


#7

@lucasbasquerotto

Thank you for answer