Q:

How to get the user ID if the user is exist in the database?

I have a database consists of 2 tables with one to many relationship between them .
The first table "data" has (name , phone , personid) columns.
The second table "links" has (linkid , link , personid) columns.
I want to insert a new link to the links of a person if he already exists in the data table .
$statement = $connect->prepare("SELECT * FROM data WHERE name = :sname AND phone = :sphone");  
$statement->execute(array('sname' => $name , 'sphone' => $phone));  
$count = $statement->rowCount();

if($count > 0){    
$st = $connect->prepare("SELECT personid FROM data WHERE name = :sname AND phone = :sphone");   
$id = $st->execute(array('sname' => $name , 'sphone' => $phone));

I want to get the ID if the user if he exist to use it to insert a new link to his links , So how could I get the ID?

A:

If you are expecting one column only, you can use fetchColumn():
$st = $connect->prepare("SELECT personid FROM data WHERE name = :sname AND phone = :sphone");   
$st->execute(array('sname' => $name , 'sphone' => $phone));
$personID = $st->fetchColumn();

You can also use fetch() if you are expecting a single row return:
$st = $connect->prepare("SELECT personid FROM data WHERE name = :sname AND phone = :sphone");   
$st->execute(array('sname' => $name , 'sphone' => $phone));
$row= $st->fetch(PDO::FETCH_ASSOC);
$personID = $row['personid'];

$personID should hold the data from personid column returned from the query
Note: If you already set the fetch style in your connection, you can omit PDO::FETCH_ASSOC

