I am trying to select distinct
from mysql database using mysqli prepared statements
.
however, when I run my code, I get no error at all but I cannot get the results from the mysql database either!
this is my mysqli prepared statement
:
$stmt = $db_conx->prepare("SELECT DISTINCT `title`, `url` FROM `$Pages`");
echo $storePages;
$stmt->execute();
$stmt->bind_result($title, $url);
$stmt->store_result();
while($line=$stmt->fetch()){
$tvalue[] = $line;
}
but This code works and i do not want to use it as it is not mysqli prepared statement:
$sql = "SELECT DISTINCT title, url FROM $Pages";
$query = mysqli_query($db_conx, $sql);
while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$tvalue[] = $line;
}
could someone please help me out with this?
Thanks
$stmt->fetch()
does not return the row fetched, it only returns true, false, or null. Please read the documentation.
The results are stored in the variables you told it to use in $stmt->bind_result()
.
$stmt->execute();
$stmt->bind_result($title, $url);
$stmt->store_result();
while($stmt->fetch()) {
$tvalue[] = array('title'=>$title, 'url'=>$url);
}
If you have mysqlnd installed (which should be enabled by default since PHP 5.4), it would be simpler for your case to fetch_all()
. That way you don't need bind_result()
, nor do you need to loop over rows:
$stmt->execute();
$result = $stmt->get_result();
$tvalue = $result->fetch_all();
Always check your return values. Always.
if( ! $stmt = $db_conx->prepare("SELECT DISTINCT `title`, `url` FROM `$Pages`") ) {
die( $db_conx->error );
}
echo $storePages;
if ( ! $stmt->execute() ) {
die( $stmt->error );
}
$stmt->bind_result($title, $url);
$stmt->store_result();
while($line = $stmt->fetch()){
$tvalue[] = $line;
}