Delete from db multiple rows Drupal - mysql

i have a problem i need to delete from cache table specific rows. I have like 3 rows to delete by one submit.
cache_73_content
cache_73_work
cache_73_header
I tried like this :
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']) . '%';
db_delete('cache')
->condition('cid', $cache_delete)
->execute();
But didn't work. Please need your help

It would make more sense to use the API:
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']);
cache_clear_all($cache_delete, 'cache', TRUE);
If you must use the database directly:
$cache_delete = sprintf('cache_%s', $form_state['values']['node_tid']) . '%';
db_delete('cache')
->condition('cid', $cache_delete, 'LIKE')
->execute();

Related

How to get random row from MYSQL and how to write it in active records?

This is my query
"SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;"
I try to write it in active records like this.
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(0, 3);
$result = $this->db->get();
But it is not work. How to write this in active record?
Use Below code it will work fine -
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(3, 0);
$result = $this->db->get()->result();
// shows last executed query
echo $this->db->last_query();
// shows data fetched
echo "<pre>";
print_r( $result );
echo "</pre>";
You can visit on this link to view how queries are used in Codeigniter.
https://www.codeigniter.com/userguide2/database/active_record.html
CodeIgniter does not mandate that you provide 2 arguments for most Query Builder statements, you can do a whole WHERE by doing ->where('1=1') and its perfectly fine.
I'm surprised how many people don't understand method chaining, but I'll show it in my example it is just nicer...
$result = $this->db->select('*')
->from('package_info')
->order_by('rand()')
->limit(0, 3)
>get();
As per above, if you don't have 2 parameters in your original query, dont feel compelled to add two.
Another thing you can do with basic queries like these, is omit the from('package_info') entirely and stick the table name in the ->get('package_info')
If you cant be bothered with query builder you don't need to use it either. I don't for some things (you cannot use UNION with them for one). In this case just use
$result = $this->db->query("SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;");

Using MD5 and CONCAT in MuSQL WHERE clause

I am creating a reset password procedure. this way:
1- Send reset email to the user (whom forgot his/her password), and this email contain a link to this address:
http://www.example.com/resetpassword.php?userid=1b2798bad6ee465d967cdb71ced504f7
The value of the parameter [userid] is generated by this PHP code:
<?php
md5($row_Recordset2['userID'].date('d-m-Y'));
?>
Note: I did this so that I can get a unique parameter value containing: The MD5 hash of the real user id + the current date (concatenation) , Why? so the link in the email will be available for the current date only. I do not want that link to work in the next day.
2- When the person click on the above link, he/she will be taken to the page [resetpassword.php]
3- In the page [resetpassword.php] page I have this piece of code:
$colname_Recordset1 = "-1";
if (isset($_GET['userid'])) {
$colname_Recordset1 = $_GET['userid'];
}
mysql_select_db($database_aaa_database, $aaa_database);
$query_Recordset1 = sprintf("SELECT * FROM users WHERE md5(CONCAT(userID,
DATE_FORMAT(NOW(),'%d-%m-%Y'))) = %s ", GetSQLValueString($colname_Recordset1,
"text"));
$Recordset1 = mysql_query($query_Recordset1, $aaa_database) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
The SELECT statement return [Query was empty] ... What is the problem and how to solve it?
I have tested this MySQL statement manually:
SELECT userFirstName, md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y'))) from users
And it return many rows, one of the rows is like this:
Sam 1b2798bad6ee465d967cdb71ced504f7
So, the:
md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y')))
works fine and it return the same exact value generated by the PHP code :
<?php
md5($row_Recordset2['userID'].date('d-m-Y'));
?>
PHP give: 1b2798bad6ee465d967cdb71ced504f7
MySQL give: 1b2798bad6ee465d967cdb71ced504f7
But this:
md5(CONCAT(userID, DATE_FORMAT(NOW(),'%d-%m-%Y')))
seems to be NOT WORKING in the WHERE clause:
sprintf("SELECT * FROM users WHERE md5(CONCAT(userID,
DATE_FORMAT(NOW(),'%d-%m-%Y'))) = %s ", GetSQLValueString($colname_Recordset1,
"text"));
By the way, I am using Dreamweaver.
I don't know how to solve this problem. Any help will be highly appreciated.

phpmyadmin (linux) remove backtick in mysql SQL query window

I would like to know if there is a away to remove the backtick in the default SQL query window for all queries in phpymyadmin (on Linux)
I'm thinking there might be a phpmyadmin config file responsible for this setting.
For example:
In order to build a query based on the current table invoices, I would first click on the SQL tab in phpmyadmin window, and this is what appears:
SELECT * FROM invoices WHERE 1
If I then select a table column on the right hand side to add to the query, eg. invoice_number, the query may then read:
SELECT * FROM invoices WHERE invoice_number
What I want is:
1) for the default SQL (ie when I first press SQL tab) to read:
SELECT * FROM invoices WHERE 1
and
2) as I build the query with selected columns from the right, it would then read:
SELECT * FROM invoices WHERE invoice_number
and so on and so forth.
There surely must be a config setting somewhere in the phpmyadmin environment that I can adjust so that no backtick or quote_backtick ever appears in the SQL I build?
Any ideas appreciated.
At this point it is easier to modify the backquote function in
libraries/Util.class.php:881
Replace $do_it default value from true to false
public static function backquote($a_name, $do_it = true)
to
public static function backquote($a_name, $do_it = false)
The actual source code shows that there is no such configuration entry (backquote is hardcoded):
A possible solution is to edit these lines in sql_query_form.lib.php:
line1
$query = PMA_Util::expandUserString(
$GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
);
replace to:
$query = PMA_Util::expandUserString(
$GLOBALS['cfg']['DefaultQueryDatabase']
);
line2
$query = PMA_Util::expandUserString(
$GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
);
replace to:
$query = PMA_Util::expandUserString(
$GLOBALS['cfg']['DefaultQueryTable']
);
line3
$html .= '<option value="'
. PMA_Util::backquote(htmlspecialchars($field['Field'])) . '"';
replace to:
$html .= '<option value="'
. htmlspecialchars($field['Field']) . '"';
(It seemed to be working for phpmyadmin 4.0.10.)

How to fetch a record from a column or field?

I have a table with a column named balance.
if(mysqli_num_rows($get_bank_check_res) > 0){
$display_block = "<p>your autho code is:</p>";
$account_check = mysql_fetch_array($get_bank_check_res);
$balance= $account_check > $grand_total_safe ? (balance - $grand_total_safe) : 0;
$display_block .= "<p>your balance is: '".$balance."' </p>";
I received the warning : Undefined variable balance. Trying mysql_fetch_assoc() didn't work either.
You get a row back with mysql_fetch_array, it doesn't automagically create new variables for you. Ie your column is located here. Also, since you are using the MySQLi extension instead of mysql, it look like this:
$row = $get_bank_check_res->fetch_assoc();
$balance = $row["balance"];
then you can do you whatever math your doing using the values found inside your $row array.

PHP Array Duplicates

my first post here and hoping someone can help. I am querying a table in a mySQL DB, and obviously getting the results. However, the table is used to store multiple entry by one user for the purpose of user contacts.
What I would like to do is display each user individually, and count the number of contacts each user has. I had a look at the post "How to detect duplicate posts in PHP array, which helped a bit, but I am still stuck.
Please see my code for the query below, I have left out the array duplicate part as it is a pretty mess at the moment.
<?php
$result = mysql_query("SELECT * FROM vines");
while($row = mysql_fetch_array($result)) {
$results=$row['vinename'];
echo $results;
echo "<br />";
}
?>
This result returns the below, obviously these are records from the vinename coloumn.
Marks Vine<br />
Marks Vine<br />
Marks Vine<br />
Tasch Vine<br />
Tasch Vine<br />
Regards
Mark Loxton
Hi there, my first post here and hoping someone can help. I am querying a table in a mySQL DB, and obviously getting the results. However, the table is used to store multiple entry by one user for the purpose of user contacts.
You can do this in the query itself a lot more easily than in the PHP code afterwards.
SELECT name, COUNT(id) AS count FROM vines GROUP BY name
Just change the SQL Query to
SELECT vinename, COUNT(vinename) as counter FROM vines GROUP BY vinename
and then do
echo $row['vinename']." #".$row['counter']."<br />";
I would run two types queries...
1) Select each UNIQUE user from vines.
2) For each user in that set, run a second COUNT query against that user's id in the table "vines".
I hope that helps.
You can create a separate array to store records you've already output there.
<?php
$result = mysql_query("SELECT * FROM vines");
$duplicates = array(); ## store duplcated names here
while($row = mysql_fetch_array($result)) {
$results = $row['vinename'];
if (!array_key_exists($results, $duplicates)) {
echo $results;
echo "<br />";
$duplicates[$results] = 1; ## mark that we've already output this records
}
}
?>
You can try, change your query to use count and group of SQL.
Somoe thing like
$result = mysql_query("SELECT count(*) as total,name FROM vines GROUP by name");
firstly thank you everyone for such awesome input. I seriously did not expect such a quick response. I am seriously grateful.
I used the recommendation from Jitter. I have pretty much been going through so many variations of the above code today, but just needed that missing piece.
Thanks, everyone. Below is what the final code looks like for anyone else who has the same problem in the future.
<?php
$result = mysql_query("SELECT vinename, COUNT(vinename) as counter FROM vines GROUP BY vinename ORDER BY counter DESC LIMIT 0, 3");
while($vinerow = mysql_fetch_array($result))
echo $vinerow['vinename']." has ".$vinerow['counter']." tomatos."."<br />";
?>
change your query to:
SELECT distinct * FROM vines