COUNT values in a column and store count in variable? [duplicate] - php

This question already has answers here:
How can I store the result of an SQL COUNT statement in a PHP variable
(4 answers)
Closed 8 years ago.
I have tried a few things and googled but can't get something that works. I want to use an SQL query to count how many rows in a table has "Rock" as it's value inside the "Genre" column. And then I need to store the count number inside a variable so that I can echo it later in my page. Any help? I'm new to php...

A query like this
$lo_rec = mysql_query("SELECT COUNT(`genre`) as `total`
FROM `tablename`
WHERE `genre` = 'ROCK'");
$result = mysql_fetch_object($lo_rec);
Then when you grab the results, assign it
$li_count = $result->total;
echo $li_count;
And before anyone says so, I know to use mysqli or PDO :-)

Related

Mysql query to get the comma separated value (1,10,15) stored in a column with WHERE Condtion that also has a comma separated Value (15,1,100) [duplicate]

This question already has answers here:
SQL query to match a comma-separated string against a comma-separated string?
(3 answers)
Closed 5 years ago.
My Table Look like Below, Please refer Screenshot.Table Example Structure
I want a query to get the "id" when I pass the comma separated values (ex:25,10,3) in WHERE Condition of user_id column.
EX: Select id from table where user_id IN('25,10,3')
When I execute the above query I should get ID's 1 and 2.
I tired with "WHERE IN" and "find_in_set()". But nothing worked out...
Please help me to get this done...
Balachander,
find_in_set will work (see SQLFiddle), but you should reconsider using proper database structure.
Finally I have Find a Solution for this....
select * from test where user_id REGEXP '(^|,)(25|10|3)(,|$)'
Refer SQLSQLFiddle

How can I fetch with PDO in order to have names of the columns in the first array position? [duplicate]

This question already has answers here:
php pdo: get the columns name of a table
(14 answers)
Closed 7 years ago.
How would you fetch a mysql query with PDO (Or after PDO) in order to have this result? I mean the "titles of the columns" in the first position of the array and the values in the next positions? Can somebody helping me with an example?
[
["ColumnTitle","Column2Title"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"],
["RecordValue1","RecordValue2"]
]
The mysql query returns:
ColumnTitle | Column2Title
----------------------------
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
RecordValue1 RecordValue2
I don't think this question was duplicate, this was a specific case.. :(
PDO doesn't offer such a format out of the box (because no developer would like it this way), but you can make it with minor tweaking
$data = $pdo->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);
array_unshift($data, array_keys($data));
But in your place I wouldn't add column names to the data array at all.

Getting all effected rown in MySQL UPDATE [duplicate]

This question already has answers here:
UPDATE/DELETE in mysql and get the list of affected row ids?
(3 answers)
Closed 9 years ago.
Title pretty much says it. I am using the deprecated mysql_ functions. I've got a query like:
UPDATE `table`
SET `row` = 'newtext'
WHERE `row` = 'oldtext'
Is there an easy way to get all effected row's primary key? Some glorious combination of mysql_insert_id and mysql_affected_rows? How can I do this without looping and doing each update one row at a time?
As a solution to your problem please refer the following sample code snippet
UPDATE `table` SET `row` = 'newtext' WHERE `row` = 'oldtext'
select id from table where row='oldtext'

SQL+PHP: how can i return all PHP array existed values from SQL table? [duplicate]

This question already has answers here:
Passing an array to a query using a WHERE clause
(17 answers)
Closed 10 years ago.
Hello All
I have a simple php array, For Example:
$numbers=array("12345","65432","98765");
And SQL Table named: "phones" with the column "TNumbers" and the rows: 654654, 12345, 87878.
Now, What will be the fastest way to return all the values from the array that exist in the SQL server table? (So it should return only this: ("12345"))
Thank you Very Much!!
After some more search i have found the answer, Sorry for that:
$ids = join(',',$numbers);
$sql = "SELECT * FROM phones WHERE TNumbers IN ($ids)";
Thank you anyway!

Can I use get the just insert table row id, from mysql_query? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I use mysql_query() in PHP to insert a new record in my Database.
It will auto generate a row, with a user Id, can I use the result to get that Id?
mysql_insert_id()
Have you considered upgrading to PDO?

Categories