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();
Related
I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.
I know that I have to use this statement:
LAST_INSERT_ID()
That statement works with a query like this:
$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";
But if I want to get the ID using this statement:
$ID = LAST_INSERT_ID();
I get this error:
Fatal error: Call to undefined function LAST_INSERT_ID()
What am I doing wrong?
That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().
Like:
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:
$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();
lastInsertId() only work after the INSERT query.
Correct:
$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass)
VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();
Incorrect:
$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
Like this $lid = $conn->lastInsertId();
Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php
I need to make a query for a drupal site. Initially I just needed to COUNT the number of nodes for a content type:
$query = "SELECT COUNT(*) amount FROM {node} n WHERE n.type ='A'";
$result = db_query($query)->fetch();
return $result->amount;
Now I need that but only for nodes that have the field_b equal to 'X'. How can I do this?
I tried EntityFieldQuery without sucess:
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'A')
->fieldCondition('field_b', 'value', 'X');
$results = $query->execute();
return $results->amount;
Any help?
You can set the query to be a count query only by using :
$count = $query->count()->execute();
Please, i have this query here:
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['id'];
The issue is i get this error here:
Undefined index: id where it's: `$count = $row['id'];`
Help please! It won't work without this piece of code here.
I'm using odbc_connect..
Thanks in advance..
your select function will only return 1 column: "count". you are trying to get to column "id"
here is your fixed code
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['count'];
you do not have any field in your SELECT called 'id'. Try: $count = $row['count'];
i have one one issue in fetch image and text from database by module what to do for this issue and i add my table name and field name #__home_service_item this is my table name in that table two field one is image and image_name than i have one error for that question i display my error
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Joomla_2.5.8-Stable-Full_Package\modules\mod_home\tmpl\default.php on line 40
please give me any clue for that problem i also add my code
<?php
defined('_JEXEC') or die('Restricted access');
$items = $params->get('items', 1);
$db =& JFactory::getDBO();
$query = "SELECT id
FROM #__home_service_item
WHERE published = '1'
ORDER BY id DESC";
$db->setQuery( $query, 0 , $items );
$rows = $db->loadObjectList();
foreach($rows as $row)
{
echo 'ID: '.$row->id.' </br>';
}
?>
please give one clue
do print_r($rows) and see if any records are returning from the database. I think that you have a problem with your query. If there are no results returning try enclosing your foreach statement with in a try catch or ignore warnings.
Also try to set $db->setQuery($query); instead of $db->setQuery( $query, 0 , $items );
If you just need one row result use $db->loadResult();
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