I have this query:
$butacas= $this->pdo->prepare('SELECT COUNT( * ) FROM `usuarios` WHERE `sala` LIKE :nombreSala');
$butacas->bindValue(':nombreSala', $nombreSala);
$butacas->execute();
echo $butacas->rowCount();
This query results in an integer: 1
if I replace :nombreSala with the value that actually exists in the database, like:
SELECT COUNT( * ) FROM usuarios WHERE sala LIKE 'salaChica'
It still results in 1.
Now, when checking the database with phpMyadmin I realise that with that value (salaChica) there are 2 items instead of one! (and consulting from phpMyadmin does result in 2).
Why it is not accurate? I've read this post about that function not being always accurate with SELECT, but is there a simple alternative?
You're confusing the number of rows returned (1) with the value returned (2).
Your query "SELECT count(*) WHERE whatever" will always return 1 row (unless you do a group by). that row contains a single column - the value of that column will be the count - 2 in your case.
Try
echo $butacas->fetchColumn();
instead of
echo $butacas->rowCount();
Related
i query to check if a point(input) is intersect with polygons in php:
$sql1="SELECT ST_intersects((ST_SetSRID( ST_Point($startlng, $startlat),4326))
, zona_bahaya.geom) as intersek
FROM zona_bahaya";
$query1 = pg_query($conn,$sql1);
$check_location = pg_fetch_array($query1);
if (in_array('t',$check_location)) {
dosemthing1;}
else {dosomething2;}
it's work peroperly before i update the data
after data updated, it's only show the first row when i check the pg_fetch_array result. here is the result {"0":"f","intersek":"f"} .
i try to check from pgadmin and it's can show 8 result (1 true(intersected) and 7 false(not intersect)) using updated data with this query:
SELECT ST_intersects((ST_SetSRID( ST_Point(110.18898065505843, -7.9634510320131175),4326))
, zona_bahaya.geom) as intersek
FROM zona_bahaya;
to solve it, i order the query result descended so the 'true' gonna be the first like this:
order by intersek desc
anybody can help me to findout way it just only show the first row???
here some geom from STAsText(zonabahaya.geom) not all of them : MULTIPOLYGON(((110.790892426072 -8.19307615541514,110.791999687385 -8.19318330973567,110.794393723931 -8.1927980624753,110.794586347561 -8.19205508561603,110.795329324421 -8.19120203811094,110.796540101525 -8.19023891996003,110.797503219676 -8.18933083713203,110.798576408472 -8.18919324882476,110.79929186767 -8.18957849608512,110.800337538805 -8.19059664955894,110.800585197758 -8.19150473238694,110.80022746816 -8.19238529755349,110.799787185576 -8.19290813312112,110.799589319279 -8.19300706626968,110.798788231202 -8.19299429992581,110.798537293576 -8.19311976873883,110.79850269889 -8.1933090511224,110.798620939451 -8.19433728092441)))
In order to filter only the records that intersect you have to use ST_Intersects in the WHERE clause:
SELECT *
FROM zona_bahaya
WHERE ST_Intersects(ST_SetSRID(ST_Point(110.18, -7.96),4326),zona_bahaya.geom);
Since you're dealing with points and polygons, perhaps you should take a look also at ST_Contains.
In case you want to fetch only the first row you must set a limit in your query - either using LIMIT 1 or FETCH FIRST ROW ONLY -, but it would only make sense combined with a ORDER BY, e.g.
SELECT *
FROM zona_bahaya
JOIN points ON ST_Intersects(points.geom,zona_bahaya.geom)
ORDER BY gid
FETCH FIRST ROW ONLY;
Demo: db<>fiddle
I have a table with an A.I column called 'id'. This column is UNSIGNED and has 6 entries. So id: 6 is the MAX value in the column. Using PHP to call forth the max id in the table, it always prints out the least value.
This is what i'm doing:
$MAX_ID = $db->query("SELECT MAX(id) FROM table");
echo "Hello" + $MAX_ID;
I've tried all the ways of doing this, like ORDER BY and id=("SELECT FROM MAX(id)"), but 1 is still being returned. I am using PHPMyAdmin, and when I do the SQL query there, the right value is being returned. What am I doing wrong?
No, what you're getting is just a MySQLi result object ($MAX_ID).
Execute the query → Fetch the rows.
$query = $db->query("SELECT MAX(id) FROM Entertainment"); // execute
$max_id = $query->fetch_array(); // fetch
echo $max_id[0];
Additional Note: Use . for concatenation, not +:
echo 'max: ' . $max_id[0];
Take a look at the mysqli documentation. The value being returned is a result object, not the direct result data from the query: You'll need to use mysqli's accessor methods to extract actual result rows.
I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.
I'm wondering why my MySQL COUNT(*) query always results in ->num_rows to be equal 1.
$result = $db->query("SELECT COUNT( * ) FROM u11_users");
print $result->num_rows; // prints 1
Whereas fetching "real data" from the database works fine.
$result = $db->query("SELECT * FROM u11_users");
print $result->num_rows; // prints the correct number of elements in the table
What could be the reason for this?
Because Count(*) returns just one line with the number of rows.
Example:
Using Count(*) the result's something like the following.
array('COUNT(*)' => 20);
echo $result['COUNT(*)']; // 20
Reference
It should return one row*. To get the count you need to:
$result = $db->query("SELECT COUNT(*) AS C FROM u11_users");
$row = $result->fetch_assoc();
print $row["C"];
* since you are using an aggregate function and not using GROUP BY
that's why COUNT exists, it always returns one row with number of selected rows
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html
Count() is an aggregate function which means it returns just one row that contains the actual answer. You'd see the same type of thing if you used a function like max(id); if the maximum value in a column was 142, then you wouldn't expect to see 142 records but rather a single record with the value 142. Likewise, if the number of rows is 400 and you ask for the count(*), you will not get 400 rows but rather a single row with the answer: 400.
So, to get the count, you'd run your first query, and just access the value in the first (and only) row.
By the way, you should go with this count(*) approach rather than querying for all the data and taking $result->num_rows; because querying for all rows will take far longer since you're pulling back a bunch of data you do not need.
Can anyone explain me why the last query returns always 1 row. it should return more than 1 because there're a lot of records in the database!
Sorry for my bad english
$query=mysql_query("SELECT book_id FROM ".DB_PREF."books_cats_list WHERE cat_id='".$cat."'");
if($row=mysql_num_rows($query))
{
//fetching all books from $cat category
for($i=0; $fetch=mysql_fetch_assoc($query); $i++)
{
$records[$i]=$fetch['book_id'];
}
//Joining all records in a string for next query
$records=implode(",",$records);
//returning num rows if there're book_id records in $records array
$query=mysql_query("SELECT * FROM ".DB_PREF."books WHERE book_id IN ('".$records."')");
$rows=mysql_num_rows($query);
echo $rows;
Your query is going to look like this:
SELECT * FROM books WHERE book_id IN ('2,3,4,5')
Note how inside the IN, it's all one string. This one string will be converted to an int. This happens by stopping at the 1st non-number character. So, the query becomes:
SELECT * FROM books WHERE book_id IN (2)
Try to remove the single quotes inside the IN.
NOTE: If your values aren't ints, try changing the the implode to: implode("','",$records), and keep the quotes inside the IN.
At a quick glance I suggest the for loop:
for($i=0; $fetch=mysql_fetch_assoc($query); $i++)
Should be a while loop:
while($fetch=mysql_fetch_assoc($query))
I expect it is only doing one record with that for loop code.
I suggest however you do a left join select
SELECT * FROM books_cats_list as cat
left join books as book on cat.book_id = book.book_id
WHERE cat.cat_id='$cat'
This will be far more optimal in terms of database performance I expect.