how to put where clause variable $didno in following code:

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM misscall WHERE did_no = ?";
$q = $pdo->query($sql);
$mobile1 = array();
while ($mobi1 = $q->fetchColumn()) {
$mobile1[] = $mobi1;
}
$mobile1list = join(",", $mobile1);
Database::disconnect();

thanks in advance

is this what you want:

$sql = "SELECT * FROM misscall WHERE did_no = ". $didno;

or did I not understand your question?

This would also work by including the var within the double quotes (this example is if the data type of did_no is a numeric - no single quotes around the php var):

$sql = "SELECT * FROM misscall WHERE did_no = $didno";

However, if did_no is a string (varchar etc) then you must put single quotes around the php var as in:

$sql = "SELECT * FROM misscall WHERE did_no = '$didno'";

The above examples work only if you are using double quotes around the query statement.

Member Avatar for diafol

If you are using a 'placeholder' use a prepared statement not a query.

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT `mobile` FROM misscall WHERE did_no = ?"; //or whichever field you're after
$stmt = $pdo->prepare($sql);
$stmt->execute(array($didno));
$mobil = $stmt->fetchAll(PDO::FETCH_COLUMN,0);
$mobile1list = implode(",", $mobil);
Database::disconnect();

In order to get unique mobile numbers, I think you can do: $mobil = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE,0) or you can do a SELECT DISTINCT.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.