When I run the following code the echo returns resource id #10 instead of number, in my case 6. Would appreciate any help!
$result = mysql_query("SELECT COUNT(`category_id`) FROM `products_has_product_category`
WHERE `category_id`=1");
echo $result;
use function mysql_fetch_object or mysql_fetch_row to convert query result to data
$result = mysql_query("SELECT COUNT(`category_id`) as count FROM `products_has_product_category`
WHERE `category_id`=1");
$row = mysql_fetch_object($result);
$count = $row->count;
echo $count;
see mysql_fetch_object
Related
I want to show the result of this SQL query execution in a view:
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
$people = print_r($results);
<?= $people ?>
But it always shows: 1
The results should be a table with 3 columns and 10 rows. So I don't know why it shows 1. I tried with different tables but always shows the same.
depend of your need you
QueryALL return All rows of the query result.
Each array element is an array representing a row of data
so you could simple iterate over you $result
foreach($result as $key => $row){
echo $row['column1'] . '<br />' ;
echo $row['column2'] . '<br />' ;
....
}
otherwise you can use the queru for build a sqlDataProvider and the manage the models show with widget (eg: gridView)
wrong usage of print_r. see this link. did you mean:
$people = print_r($results, true);
or just use var_dump to avoid confusion.
$results already contains your rows.
This way you'll see this array $results.
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
echo "<pre>";
print_r($results);
exit;
Which code do I need to retrieve a single value i from a column from table in SQL?
user_comment_count
This is column name in table
table is :
zmar_hreviews_list_total
This is code I use with error:
<?php
$insert1 ="/// ";
$string = "ars <pre>{$insert1}</pre>";
$query = 'SELECT user_comment_count FROM zmar_hreviews_list_total WHERE contentid = '.$item->getId();
$db->setQuery( $query );
$result = $db->loadResult();
if($result) {
$result = str_replace('*','',$result);
print_r($insert1); print_r($result);
}
?>
try:
select user_comment_count from zmar_hreviews_list_total
i am new in pdo. want to echo number of times post viewed. each time a user view it will add +1 autometically. i created a table views that type int length 16. my code is below
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, views FROM blog_posts ORDER BY postID ASC');
while($row = $stmt->fetch()){
echo '<div>';
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo $row['views']; echo "times";
echo '<p>Read More</p>';
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
You just prepare update statement not execute so execute the prepare statement .
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
$stmt->execute(array(':postID'=>$row['postID']));
Here I want to echo out number of rows the following code shows syntax error
$check=$db->prepare("SELECT count (*) FROM follow WHERE uid_fk=:user_id");
$check->execute(array(':user_id'=>$user_id));
echo $check->rowCount();
Try this:
$sql = "SELECT count(*) follow WHERE uid_fk=:user_id";
$result = $con->prepare($sql);
$result->execute();
$rowCount = $result->fetchColumn();
Below is my code:
<?php
$url = $_GET['url'];
$wordlist = array("Www.", "Http://", "Http://www.");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$url = preg_replace($wordlist, '', $url);
?>
<?php
$oDB = new PDO('mysql:dbname=mcnsaoia_onsafe;host=localhost;charset=utf8', 'mcnsaoia_xx', 'PASSWORD');
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja");
$hStmt->execute(array('hjemmside' => $url));
if( $row = $hStmt->fetch() ){
echo "EXIST";
}else{
echo "NOT EXIST";
}
?>
My problem is that it says NOT EXIST, because I know that there is a row which should be found with the following query:
SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja
So why does it say not exist? I have absolutely no idea :(
Instead of
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt=ja");
try
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt='ja'");
The left is most likely a column and the right side is a string? I don't speak your language, but this is the first thing coming to my mind.
You should surround with quotes your not integer variable in your query
AND godkendt='ja'
Or maybe let pdo deal with it
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt = :ja");
$hStmt->execute(array(':hjemmside' => $url, ':ja' => 'ja'));
//^ i added : for placeholder here, you missed it
I would also rather check if rows are returned
if($hStmt->$eowVount() > 0){
$row = $hStmt->fetch()
echo "EXIST";
}else{
echo "NOT EXIST";
}