Retrieve articles from array of category ids [duplicate] - php

This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query [duplicate]
(8 answers)
Closed 11 months ago.
I have an array of category ids and want to retrieve articles from my mysql database with these category ids. What is the best method for this?

mysql_query('SELECT * FROM articles WHERE category_id IN (\''.implode('\'',array_map('mysql_real_escape_string',$categories)).'\')');
Specify how articles are joined to categories if this is not how your db/table setup.

Look here:
I have an array of integers, how do I use each one in a mysql query (in php)?
for a safe, parameter-based approach and code sample.

Related

Using IN() condition in PDO MYSQL [duplicate]

This question already has answers here:
Can I bind an array to an IN() condition in a PDO query?
(23 answers)
Closed 7 years ago.
I'm trying to search a value in a mysql table field. using prepared statements.
services: 2,4,5
passed value: 2
$query = "SELECT *
FROM table
WHERE services IN(':array')"
so basically I'm trying to return all rows where 2 is inside the services field. The field itself will have numbers separated by commas. So the services field can look like this: 2,3,6,7,1 or just something like this: 4. Any help is greatly appreciated.
$query = "SELECT *
FROM table
WHERE FIND_IN_SET(':array', services )"
:array is 2 what u want pass.

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.

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

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 :-)

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!

PDO prepared query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query back from PDO prepared statement
Exists any method to show, the query executed sql query in PDO Statement object?
Ex:
$sql="SELECT * FROM table WHERE id=?";
$res=$con->prepare($sql);
$res->execute(array(1));
I like to view a query similar this: "SELECT * FROM table WHERE id=1"
$res->queryString should contain what you need. You can check out the documentation here.

Categories