Raw Delete Query - mysql

I'm about to pull my hair out... why wont this run?
Mage::getSingleton('core/resource')->getConnection('core_write')->query('DELETE FROM catalog_product_super_attribute WHERE product_id = 46');
When I run that query via command line, or phpmyadmin even, it executes just fine deleting all the rows HOWEVER when I try to run it using code it doesn't work. I've even tried just bypassing Magento hoping it was something with them BUT when I tried to delete using straight mysql or mysqli in php it wouldn't work either.
Any thoughts or suggestions would be AWESOME.

$transaction = Mage::getSingleton('core/resource')->getConnection('core_write');
try {
$transaction->beginTransaction();
$transaction->query('DELETE FROM catalog_product_super_attribute WHERE product_id = 46');
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack(); // if anything goes wrong, this will undo all changes you made to your database
}

Related

Codeigniter : lock wait timeout exceeded, try restarting transaction

My Codeigniter Model Code is below
$last_qt_pk_id = "";
$i = 0;
foreach($query->result() as $row){
$data [] = $row;
$rq_pk_id = $row->id;
$question_type_pk_id = $row->question_type_pk_id;
if($question_type_pk_id != $last_qt_pk_id){
$i =1;
} else {
++$i;
}
$up_data = array('receive_serial_ato_qt'=>$i);
$this->db->where('id',$rq_pk_id);
$this->db->update('qms_received_question_info',$up_data);
$last_qt_pk_id = $question_type_pk_id;
}
My Above Code Firstly it do update, then the question list display in view.
My Code is working nicely but sometimes the problem/error is showing.
My Error is given below by image
I cannot understand that why is showing the error sometimes. If i restart xampp then it has ok for some time but after some time then the error is showing again. I have tried with wamp and xampp but it is showing same problem.
Please, any help?
This error is entirely MySQL not doing as it should. The best solution is to forget about MySQL and migrate to other SQL like PostgreSQL, but lacking this option, maybe the following will help you:
Performance blog post
SQL Server - fix
also consider increasing the "innodb_lock_wait_timeout" value, and stop using active records when working with transactions.
Personally i don't use active records as it takes a lot of memory and slows the performance time

How Do I Figure Out What The Query Is In Eloquent?

Thinking about moving to use Laravel with Eloquent, but I know given our site eventually we'll need to ask, "So what raw SQL query is Eloquent actually generating?" so we can troubleshoot that query to see what went wrong... and I don't see that info in the docs.
So how would we pull up the query that Eloquent generated?
Fastest way is to add this to your route, controller action or even app/start/global.php:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
});
It log it, so you can then
php artisan tail
To see your queries.
If you need to log just one query direct to the page it's almost the same, you just put it in the place the query is executed:
DB::listen(function($sql, $bindings, $time) {
var_dump($sql);
var_dump($bindings);
});
Posts::where('title', 'My First Post')->get();
die;
And it will echo it in the page.
You can also use the Clockwork Chrome extension (https://chrome.google.com/webstore/detail/clockwork/dmggabnehkmmfmdffgajcflpdjlnoemp?hl=en), this is the PHP package to be installed on Laravel: https://github.com/itsgoingd/clockwork.
Clockwork gives you a really nice profiling view of your application.

Laravel 4 view sql query

When working on Laravel, we do query like this:
$user = DB::table('users')->where('name', 'John')->first();
How can I view the generated sql query? This is something very important for debugging during the development.
Thank you.
According to this answer, you should be able to use this to get the last executed query :
$queries = DB::getQueryLog(); // gets a log of all executed queries
$last_query = end($queries); // gets the last one
You can also add this snippet:
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
It will output all queries being executed in your request.
Not a direct answer as other people have answered this but take a look at this composer package, it is very helpful and displays all of your queries and more.
https://github.com/barryvdh/laravel-debugbar

Codeignighter Record wont insert

Using CI for the first time and i'm smashing my head with this seemingly simple issue. My query wont insert the record.
In an attempt to debug a possible problem, the insert code has been simplified but i'm still getting no joy.
Essentially, i'm using;
$data = array('post_post' => $this->input->post('ask_question'));
$this->db->insert('posts', $data);
I'm getting no errors (although that possibly due to disabling them in config/database.php due to another CI related trauma :-$ )
Ive used
echo print $this->db->last_query();
to get the generated query, shown as below:
INSERT INTO `posts` (`post_post`) VALUES ('some text')
I have pasted this query into phpMyAdmin, it inserts no problem. Ive even tried using $this->db->query() to run the outputted query above 'manually' but again, the record will not insert.
The scheme of the DB table 'posts' is simply two columns, post_id & post_post.
Please, any pointers on whats going on here would be greatly appreciated...thanks
OK..Solved, after much a messing with CI.
Got it to work by setting persistant connection to false.
$db['default']['pconnect'] = FALSE;
sigh
Things generally look ok, everything you have said suggests that it should work. My first instinct would be to check that what you're inserting is compatible with your SQL field.
Just a cool CI feature; I'd suggest you take a look at the CI Database Transaction class. Transactions allow you to wrap your query/queries inside a transaction, which can be rolled back on failure, and can also make error handling easier:
$this->db->trans_start();
$this->db->query('INSERT INTO posts ...etc ');
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
// generate an error... or use the log_message() function to log your error
}
Alternatively, one thing you can do is put your Insert SQL statement into $this->db->query(your_query_here), instead of calling insert. There is a CI Query feature called Query Binding which will also auto-escape your passed data array.
Let me know how it goes, and hope this helps!

Dealing with duplicate keys in codeigniter mysql insert

What I want to do is something like this:
function registerUser($username, $password){
$this->db->insert('tblUsers', array('username'=>$username, 'password'=>md5($password)));
if($this->db->_error_number()==1062){
return "DUPLICATE";
}
return true;
}
However, at the moment if there is a duplicate key then its not letting me get to the _error_number() bit. Its displaying an error like this:
How do I stop codeigniter from bailing with an error and passing the error number to me to deal with appropriately?
Thanks
You can access MySQL error messages in Codeigniter using:
$this->db->_error_message();
Apparently DB_DEBUG needs to be set to false in the database config file:
Ensure DB_DEBUG is set to FALSE in the database config file, or
execution will halt when a mysql error occurs (it does not get thrown,
it justs exits from the php interpreter)
Link: http://codeigniter.com/forums/viewthread/79950/#413830
Suggestion:
$orig_db_debug = $this->db->db_debug;
$this->db->db_debug = FALSE;
RUN QUERY HERE
$this->db->db_debug = $orig_db_debug;