PHP count row returns error - php

i'm using the following to count the number of rows in my table:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
(count($book_count));
echo $book_count;
and i get the following error:
Notice: Array to string conversion on line 167
(which is the line echo $book_count;)
FIY, the query function is defined and works fine. Never had any issues to insert, select, update and delete.
Am I missing something here?

Try this:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
echo count($book_count);
Also, you need to use print_r($book_count) since your $book_count is not a string.

Suggestion: if you only use that query for getting the count, this may be a bit better:
$book_count = query("SELECT count(*) FROM scifi_book WHERE read = $uid");

your query function seems to return an array, not a string. Instead of echo $follower_count use print_r($follower_count) to see what's inside the query response.

The reason you are seeing that error is because you are echoing Array which is the returned by your query function. echo construct only works on strings, please see the documentation here: http://www.php.net/manual/en/function.echo.php.
Had you used print_r or var_dump then you wouldn't have seen that error. So as #A.S. Roma and Nathaniel Granor suggest use print_r

$book_count = query("SELECT status FROM scifi_book WHERE read =".$uid);
(count($book_count));
echo $book_count;

Related

PHP SQLite - Not returning correct count of match

I am using PHP lite to search for a matching row like this...
$count = $db->exec("SELECT * FROM users WHERE userid = '34534fgr'");
echo $count;
But my count is returning 1 every time, even when the value does not exist.
Am I searching incorrectly?
Am I searching incorrectly?
yup. protip: when debugging, use var_dump instead of echo, it would help you see the issue here, because it would print bool(true); instead of int(1) or string("1"), because PDO::exec() returns a boolean.
here's how to do what you tried to do:
$count = $db->query("SELECT COUNT(*) FROM users WHERE userid = '34534fgr'",PDO::FETCH_NUM)->fetch()[0];

php array to string conversion odbc_exec count

i'm still new towards php but after searching through multiple topics i can't seem to figure this one out.
$query2 = "SELECT COUNT(MAP_CODE2) FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'";
$result2 = odbc_exec($connect,$query2);
echo $result2;
i have a query above that i'd like to use to get the total number of rows within the query that i've set, however for some reason i keep getting hit by the error
Array to string conversion in /var/www/html/xxx.php on line 85
Would highly appreciate if anyone could help out on what i'm doing wrong. Thank you!
Problem is:-
You are trying to echo a result-set object of array type.
Solution (check the code comments):-
$query2 = "SELECT COUNT(MAP_CODE2) AS MYCOUNT FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'"; // given a name to the count
$result2 = odbc_exec($connect,$query2); //prepare and execute query
while (odbc_fetch_row($result2)) { //iterate over result-set object
echo odbc_result($result, "MYCOUNT"), "\n"; // echo count
}
What is happening here is you are echoing an array object type that is also a resource type and php echo only string and other primitive type variables.
so you need to use a loop to access the row or to get row count and access row just use a foreach($results as $result)
to get row count visit this sqlsrv-num-rows . on this official php link you can get more example how to fetch data.

How to get a PDO Fetch( ) to return as string

How can I get a PDO to return the data as a string not an array? I am not sure this is possible so if not is there a way to convert the array to a string after it has been processed?
My code which is returning the string is:
$result = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);
I have tried different fetch types such as FETCH_CLASS FETCH_OBJ
You can do it like this:
echo $stmt->fetchColumn();
Or:
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo $result[0];
$result = $statement->fetchColumn();
This work for me, the only thing I noticed here is that fetchColumn() always refer to first column on your table. You need to adjust / re-position the only column needed to the first-column on your table or make an Sql query i.e "SELECT" query to get the only column needed. Thanks

mysql_fetch_array() always return null

I am try to fetch data from the database
$check_sql = 'SELECT * FROM table;
$check_result = mysql_query($check_sql);
echo $check_result;
$result = mysql_fetch_array($check_result);
when I echo $check_result, it shows 'Resource id 2', which i think it means there exists a return array, but when I use mysql_fetch_array, it will return a null value, and I don't know why...And I found that no matter whether there exists the resules or not, echo $check_result would always shows 'Resource id#2', does this sentence in mysql mean 'no results' ? Could someone help???
In case if you are dealing with multiple rows in your mysql query you need to use code like this:
while ($row = mysql_fetch_array($check_result) )
{
echo $row['ROW_NAME_HERE'];
}
I guess it is why you mentioned mysql_fetch_array function.
mysql_fetch_array() returns an array.
You definitely must take a look at documentation http://php.net/mysql_fetch_array
Try print_r($result);

how to +1 to the MAX record from a field

.i have the following code:
$task_id = mysql_query("SELECT MAX(task_id) from it_task");
echo $task_id;
.what I want to do is to get the highest value from a field named task_id on the table it_task. Instead of getting a result which is an integer I get "Resource id #5". how do i get around this?
mysql_query generates a resource which you then have to read using another mysql_ function. In this case you'll need a 2nd line to read from the query's result:
$result = mysql_query("SELECT MAX(task_id) from it_task");
$task_id = mysql_result($result, 0, 0); // get the first row, first column
echo $task_id;
Any problem here??? In my postgres sql, I can have:
select max(task_id)+1 as max from it_task;
Yes, mysql_query always returns a resource (unless it returns false), which you have to work on using one of the mysql_ functions like mysql_fetch_assoc.

Categories