Create limit posting for users - php

I want to make users only can create maximum 10 post per day.
Here is my controller:
$id_user=$this->session->userdata('id');
$sql ="SELECT COUNT(id_post) as max_post FROM post
WHERE post.id_user=$id_user
AND DATE(post.time)=DATE(NOW())";
$result = $this->db->query($sql);
if ($result > 10) //line number 17
{
//redirect to home
} elseif($result <= 10 ) //line number 20
{
//then do post
Then is getting error like this:
A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Line Number: 17
A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Line Number: 20
Any answer?
Many thanks...

if ($result > 10)
$result Is the resource itself, you have to fetch data from it. $result itself is not what you are looking for. Fetch the required field from result.
For example
$row = $result->row();
if ($row->max_post > 10)

if(mysql_num_rows($result)>10)
{
}
try using this

Related

Variable is an array but not countable?

I am upgrading our Codeigniter (v. 3.1.11) site from php 5.6 to php 7.2 (currently running on localhost on my Mac). Slowly I am finding all the instances of count() usage and correcting them. It is my understanding that an array is countable, but I seem to not be able to count arrays returned by Codeigniter's result_array() function after a database call....
the following section of my controller
$reviews = $this->reviews_model->review_details($productname);
echo "Variable is type: ".gettype($reviews);
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
return $myreview;
calls this function in my model (note that I am echoing out the variable type just to be sure!)
function review_details($pagename) {
$r = false;
$sql = "select Reviews.*, ReviewItemLink.Item as Product, ReviewItemLink.* from Reviews LEFT JOIN ReviewItemLink ON Reviews.ReviewItemID=ReviewItemLink.ReviewItemID where pagename=? AND ReviewActive = 1 ORDER BY Date DESC";
$query = $this->db->query($sql, $pagename);
if ($query->num_rows() > 0):
$r = $query->result_array();
endif;
return $r;
}
And even though the variable is an array
Variable is type: array
I still get the by now, oh-so-familiar warning message:
Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: controllers/Software.php
Line Number: 1005
Backtrace:
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 1005
Function: _error_handler
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 75
Function: _get_my_review
File: /Users/juner/Sites/html/index.php
Line: 324
Function: require_once
Are there types of arrays that are NOT countable? Any suggestions/ideas would be most helpful!
This part is incorrect (typo?):
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
You are passing a boolean to the count function. count($reviews >=1) turns to count(true); which is why you got your warning.
if (count($reviews >=1)) { should be if (count($reviews) >=1 ) {

Codeigniter, Database Errror, Severity: 4096 Message: Object of class CI_DB_mysql_result could not be converted to string

I am trying to get a value of an attribute from database named "post_id to use somewhere else.
Here is my model code.
$copy_query = "SELECT post_id FROM posts order by post_id DESC limit 1"; //query for selecting last post's post_id
$result = $this->db->query($copy_query); //adding that post_id to the $result variable
$sub_data = array(
'study_education_level' => $this->input->post('sub_education_level'),
'tourism_country' => $this->input->post('sub_tourism_country'),
'tourism_place_name' => $this->input->post('sub_tourism_placeName'),
'post_id' => $result
);
$this->db->insert('subcategories',$sub_data);
But whenever I run the code it gives me two ERROR.
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: mysql/mysql_driver.php
Line Number: 553
And
2.
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
INSERT INTO `subcategories` (`study_education_level`, `tourism_country`, `tourism_place_name`, `post_id`) VALUES ('Higher Study', '', '', )
Filename: C:\xampp\htdocs\learn\system\database\DB_driver.php
Line Number: 331
Thank you in advanced!
Btw Ive solve the problem :)
$result = $this->db->query($copy_query); //adding that post_id to the variable
if ($result->num_rows() > 0)
{
foreach ($result->result() as $row)
{
$post_id_plz = $row->post_id;
}
}

SQL call keeps on returning error

The SQL call below keeps returning:
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/content/76/10930776/html/apprentice/report.php on line 86
$sql = "select sum(".$column.") as totalmeetings,username from data where datediff(max(dateinput),min(dateinput)) <= ".$daysdifference." group by username ";
echo $sql;
$result = $mysqli->query($sql);
while($pcresult = $result->fetch_assoc())//line 86
{
}
What am i doing wrong?
The SQL call prints out like this:
select sum(prospects1stmeeting) as totalmeetings,username from data where datediff(max(dateinput),min(dateinput)) <= 500 group by username
try this query
select sum(prospects1stmeeting) as totalmeetings,username from data
group by username
having datediff(max(dateinput),min(dateinput)) <= 500
Look at how you're trying to access the results:
$result = $mysqli->query($sql);
while($pcresult = $result_percentile->fetch_assoc())//line 86
{
}
$result and $results_percentile are not the same thing. You should try:
$result = $mysqli->query($sql);
while($pcresult = $result->fetch_assoc())//line 86
{
}
unless you're not showing your whole code.
EDIT
But since you edited your question to no longer reflect this answer, the answer is still that you're trying to access a non object. That means $result does not produce data that can be read as an associative array.

Two PDO errors when executing rowCount and second foreach loop

Been struggling at this for a couple of hours now. Can anybody point out where I'm going wrong.
The first error I get is:
Fatal error: Call to undefined method PDO::rowCount() in
pm_wall.php on line 8
Call Stack: 0.0000 643680 1. {main}()
index.php:0 0.0015 662904 2.
include('pm_wall.php')
index.php:117
The second error I get is:
Warning: Invalid argument supplied for foreach() in pm_wall.php on line 19
Call Stack:
0.0000 643296 1. {main}() index.php:0
0.0015 649864 2. include(index.php:117
How do I go about resolving this? Here is my code below:
$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($pdo->rowCount() > 0 && !empty($result)) // first error occurs here
{
foreach ($result as $row)
{
$user_id = $row['user_id'];
echo '<div id="content">
<div class="stbody">
<div class="stimg">';
// set profile picture
$sql = "SELECT img FROM pm_user WHERE id=$user_id";
$result = $pdo->query($sql);
foreach ($result as $row) // second error occurs here
{
echo $row['img'];
}
echo '<div id="stexpandbox"><div></div>';
echo '</div></div></div></div>';
}
}
else
{
echo 'Wanna be starting soemthing?';
}
rowCount() has to be called on your PDO statement, not on your PDO instance !
$result->rowCount(); Reading errors might help...
Something like this maybe:
$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($result->rowCount() > 0 && !empty($result)) // first error occurs here
{
while( $row = $result->fetch(PDO::FETCH_ASSOC) )
Take a little time(a day or two), and read about PDO and its usage over on php.net.
Use this option in PDO to get rows that are affected and matched:
PDO::MYSQL_ATTR_FOUND_ROWS => TRUE
If the row is not found while update for instance - it will return 0. Hope this helps.

mysql_fetch_row() expects parameter 1 to be resource, object given.? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
What is the cause of error in my code and how I can to fix?(i use of codeigniter)
$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) ) // Line 27
{
$units = unserialize( $row[0] );
}
echo json_encode(var_dump($units));
Error:
A PHP Error was encountered Severity: Warning Message:
mysql_fetch_row() expects parameter 1 to be resource, object given
Line Number: 27
Output: array(0) { } null
UPDATE:
Error:
A PHP Error was encountered Severity: Notice Message:
unserialize() [function.unserialize]: Error at offset 0 of 3 bytes
Line Number: 29
Output: bool(false) null
See my database: http://i.stack.imgur.com/IORSM.jpg
$query_hotel_search = mysql_query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
if(mysql_num_rows($query_hotel_search)==0){
return '0';
}else{
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) )
{
$units = unserialize( $row->units[0] ); // Line 29
}
echo json_encode(var_dump($units));
}
http://de2.php.net/manual/en/function.mysql-fetch-row.php
mysql_fetch_row() takes not the query itself but rather the result from the mysql_query command (which is a resource).
Look at the example at the PHP Documentation (linked at the top) and you will get the functionality of mysql_fetch_row.
Maybe the assignment of $query_hotel_search can clarify the situation.
You are using a codeigniter object as mysql resource. Instead you should use object iterator provided by codeigniter to loop through the results.
foreach($query_hotel_search->result() as $row)
{
// $row as object
}
foreach($query_hotel_search->result_array() as $row)
{
// $row as array
}
For the second part (UPDATE), mysql_fetch_row() return an enumerated array. so you should use:
$units = unserialize($row[12]);

Categories