I have used this code to select data from table tbl_users but it is showing 1. what does it mean..
require_once('config.php');
$dbCon = getConnection();
$sql = "SELECT * FROM tbl_users";
$stmt = $dbCon->prepare($sql);
print($stmt->execute());
can Anyone Help...????
If the query runs successfully, $stmt->execute()
returns true
, which will print as 1
.
To return data:
//Returns first row as array
$row = $stmt->fetch();
//Returns first row as key => value array
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//Returns all rows as key=>value arrays
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
You can use this to print data like so:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Print all data
$print_r($rows);
//Print data row-by-row
foreach($rows as $row){
print_r($row);
}
At this moment you're printing the response of the query, not the results.
To print the results you should do something like this:
foreach ($stmt as $row) {
print $row['name'] . "\t";
print $row['first_name'] . "\t";
print $row['birth_date'] . "
";
}
On successful execution of query
execute()
return True
.
$sql = "SELECT * FROM tbl_users";
$stmt = $dbCon->prepare($sql);
$result = $stmt->fetchAll();
print_r($result);