I have a problem here in NotORM code.
This code are working well:
$select = $db->pspaym->select("COUNT(*)")->where("F4","$textdate")->fetch();
$count = count($select);
But this code here does not working:
$select = $db->pssale->select("COUNT(*)")->where("F8","$textdate")->fetch();
$count = count($select);
This code have an error message said:
"Trying to get property of non-object"
cannot resolve this problem.
all variables are not null.
thanks.
if you want to count lines in a table, simply use:
$select = $db->pspaym->where("F4","$textdate");
$count = count($select);
The problem comes from your combination of fetch() and count() methods. Because of the ending fetch call, $select doesn't contain a Table object, but a Record object.
So count($select) will count the number of columns in your record. Normally, it should always returns 1 (because one field in the returned record).
For your information, if you want to be uselessly explicit, you may do something like this.
$record = $db->pspaym("F4","$textdate")->select("COUNT(*) AS c")->fetch();
$count = $record['c'];
But, that's the long way.
Related
I'm new to PHP and i want to know how i can subtract a specific amount from the results from counting the total amount of rows in a table. In this case i'd like to minus the value 3 from whatever the value of the total rows is. But i keep getting an error. Below is my code.
$cartwork = $con->query("SELECT count(*) FROM table");
$vs = '3';
$camount = $cartwork - $vs;
echo "$camount";
When the code runs i get the error "Object of class mysqli_result could not be converted to int" what can i do to fix this and get it to work properly.
The query returns a result set. You need to parse through the result set(s) in order to access the values returned. That's basically what the error states.
Please see here for documentation on the PHP function for fetching rows:
http://php.net/manual/en/function.mysql-fetch-row.php
So basically you would need
$row=$cartwork->mysql_fetch_row();
$cartWork_value = $row[0];
$vs = '3';
$camount = $cartwork_Value - $vs;
echo "$camount";
Note - this assumes that you get back exactly one result row (which should be the case with your query).
You can simply change your query to:
$cartwork = $con->query("SELECT count(*)-3 FROM table");
It doesn't smell particularly good though.
$query = $db->prepare("SELECT COUNT(id_u) FROM account");
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$count = $result['count(id_u)'];
echo $count;
Not sure what I'm doing wrong here. I'm trying to get it to count the amount of registered users we have by counting the amount of id_u's (user id).
Like I said in the comments, It might be similar to other posts but those don't explain how to fix my situation since they are not quite the same.
EDIT:
After testing the issue you are experiencing; I managed to solve why your solution is not working. It appears that it is due to you calling COUNT(id_u) in the SQL query, then in the echo statement you are trying to get count(id_u) which doesn't exist in the array. Try
$count = $result;
$count = $result['COUNT(id_u)'];
To detect where the issue is stemming from I'd suggest you add in a print_r($result);. This will give you an idea what is returning from your SQL query in an array format. Once you see what data is returned and available, you can then determine how you echo/print the result.
My opinion on how this should be done:
The use of prepared statements in this is not necessary in my opinion for counting rows.
What I would do is:
$rowCount = $db->query('SELECT COUNT(*) FROM account')->fetchColumn();
echo $rowCount;
$count = count($result['id_u']);
I have a PHP class where I am initializing an array retrieved from database in the constructor:
class TableClass{
public function __construct($con, $id){
$this->con = $con;
$query = mysqli_query($this->con, "SELECT * FROM table1 WHERE id='$id'");
$this->table_result= mysqli_fetch_array($query);
}
}
I have different functions that I want to use the array without having to fetch again. I just want to loop through the results and make some calculations.
I tried the following syntax:
public function getNumberOfComments(){
for ($i = 0; $i < count($this->table_result); $i++) {
$comment= $this->table_result[$i]['comment'];
}
}
I am getting an error:
Illegal string offset "comment"
What is the correct syntax?
You are fetching one row from the result set:
$this->table_result= mysqli_fetch_array($query);
Assuming that the id is the primary key, you will have 0 or 1 rows in your result set, so if a row is found, you can access the fields directly without using a loop:
$comment= $this->table_result['comment'];
Illegal string offset "comment" is telling you that the value of $this->table_result[$i] is not an array, and therefore does not contain an element named comment.
The problem is possibly being caused by the code in the constructor failing to work properly; you don't have any error checking in there, so if it fails, it won't tell you about it.
Add some error checking in the constructor, find out why it's failing, and fix it.
For an application I'm trying to count the total of friends. I want to do this with a function but it isn't returning anything. It tells me this:
Warning: mysqli_query() expects at least 2 parameters, 1 given
But I only need one parameter. I think I'm totally wrong.
This is the function:
public function GetTotalOfFriends($user_id){
$db = new Db();
$select = "SELECT COUNT FROM friendship WHERE (friendship_recipient_id ='" . $user_id ."' OR friendship_applicant_id = '" . $user_id . "') AND friendship_status = 'accepted'";
$result = $db->conn->query($select);
$row = mysqli_query($result);
$total = $row[0];
echo $total;
I'm trying to print it out in this way:
$friend = new Friendship;
$numberoffriends = $friend->GetTotalOfFriends($user_id);
<?php echo $numberoffriends; ?>
You are mixing up a couple of things. The line $result = $db->conn->query($select); already seems to execute a query, but then you try to bypass your database wrapper by passing that query result again to mysqli_query.
Apart from that, I think your query itself is also wrong. COUNT needs a parameter, indicating a field or value to count. Quite often COUNT(*) is used, but COUNT('x') might be more efficient in some cases. You can also use a specific field name, and COUNT will count the non-null values for you.
The result you got is a mysql_result object, which you need to use to get to the actual data of the query result.
The documentation of this object is here and I suggest that you read it thoroughly.
One possible way to do this is using this:
$resultArray = $result->fetch_row();
This will result in the first (and only) row of your query. It is represented as an array, with one value (since your query returns only one column). You can fetch that value like this:
return $resultArray[0];
You could also use any of the other fetch methods if you want your data in a different fashion.
Let me preface by saying I know nothing about doctrine, but at my new position we use it all over the place (not sure why...). Either way, here's the php and mySQL statement I'm trying to turn into a Doctrine statement:
$find_vac = mysql_query("SELECT Vacancies FROM States WHERE Abbreviation = '".$state."'");
I think the part that's tripping me up is where the Abbreviation is a variable. Any help would be greatly appreciated!!!
UPDATE:
$res = Doctrine_Query::create()
->select('Vacancies')
->from('States')
->where('Abbreviation = ?', $state)
->execute();
$vacancies = $res[0]->getVacancies();
The above returns an error.
echo $res['Vacancies']."<br />";
This returns the number 4 no matter which state is selected (and even then all states range from 0-3 for the number of vacancies).
Something like this should do it. The variables can be inserted into the query in the same way as prepared statements.
$res = Doctrine_Query::create()
->select('Vacancies')
->from('States')
->where('Abbreviation = ?', $state)
->execute();
EDIT: This will give you an array of States in array form that match the search criteria. If you just want to get the value of the first one's vacancies, you can get it like this:
$vacancies = $res[0]['Vacancies'];
Or course, you'll also want to check that $res[0] exists and is itself an array in case a bogus or nonexistent $state is used.