Kelbe
Kelbe

Reputation: 161

MySQL Query not returning a row value in PHP

I don't know why this query won't return a value because when I copy the "echoed" portion into phpmyadmin I do get a record returning:

echo $_GET["cname"];

// Query template
$sql =  'SELECT C.cid FROM `Contact` C WHERE C.email="'.$_GET["cname"].'"';
echo $sql;

// Prepare statement
$stmt = $conn->prepare($sql);

$stmt->execute();
$stmt->bind_result( $res_cid);

echo $res_cid;

$res_cid is apparently 0, but I don't know why because when I paste that query manually into phpmyadmin I do get a value... So why doesn't it return anything?

Upvotes: 1

Views: 41

Answers (1)

Dekel
Dekel

Reputation: 62536

As already mentioned in the comments - you should make sure your code is secured. You better use the bindparam for that.

As for your question - after you execute your query and bind_result you should also fetch to get the actual value from the database, based on your query:

// Prepare statement
$stmt = $conn->prepare($sql);

$stmt->execute();
$stmt->bind_result( $res_cid);

// Fetch to get the actual result
$stmt->fetch();
echo $res_cid;

Upvotes: 1

Related Questions