why is this mysql query is not actually updating anything - php

i'm no backend expert but I thought I had a pretty good grasp on PDO queries with mysql but for some reason I can't figure out why this statment doesn't seem to update anything in my database (though the condition is definately met in the db )
anyways here is the statement:
$update = "UPDATE content
SET src_link=:src_link
WHERE content_id=:content_id";
$stmt = db::getInstance()->prepare($update);
$stmt->bindParam(':content_id', $video['content_id']);
$stmt->bindParam(':src_link', $video['src_link']);
$stmt->execute();
some explanation the transaction is initiated earlier in a script, and this code is wrapped in a try catch with no catch being caught. the src_link is a var_char and content_id is a primary key .. however I have printed out the video array and entered code like this which works in phpmyadmin
UPDATE `content` SET `src_link`= 'http://www.youtube.com/watch?v=f3rMo5cgaXQ'
WHERE `content_id`=6939
where these values were taken directly from an attempt output in my php script and seems to work
Anyways, I am kind of lost to why this is not working and have been stuck on this for the last couple hours and without resolving I can't move forward so any help with how I can get the pdo to properly output would be greatly appreciated as I have about a 1000+ updates to put in .
Thanks in advance!

If you've started a transaction, you have to commit the transaction to make the changes permanent.

Related

PHP MySQLi autocommit went off without warning?

Here's a strange case that has never happened to me before, I've been coding with mysqli and php for about 2 years now, very basic stuff, prepared statements, query executes, etc, started with WAMP, then moved to ubuntu, tried LAMP, didn't like it, went on investigating a proper testing enviroment, and I found PuPHPet, which was perfect, have been working with it a few months, and everything is going great.
Never in all this time I had to worry about opening, closing or commiting connections, like I do in JAVA, I read somewhere that php handles connections, it opens and closes them when the scripts runs. I've never really pay much attention, which I should have, I found this very useful post PHP + MySQL transactions examples, which I will start using from now on.
But the reason I'm posting here, is because all of a sudden autocommit is off. I opened my usual puphpet project, tried inserting some new entries, and they did not get inserted. So, I made some test in workbench, inserted some entries manually, they got inserted, but my AI index had incremented, meaning that my app did insert something, or at least it tried, or something happened because the AI counter had increased. Got some help from a couple of DBA and we got to the conclusion that it is the autocommit.
Using this piece of code:
$res = mysqli_query($mysqli, "SELECT ##autocommit");
$row = mysqli_fetch_row($row);
printf("Autocommit: %s\n", $row[0]);
It prints "Autocommit: 0", so it is off, but I didn't change anything, even in production enviroment autocommit is on, or at least my inserts stay there, but I don't know why it changed in my test enviroment, so I'm wondering what could have happened? Anybody got any ideas?

Add comments to mysql queries (php -> mysql-db)

I got a website with a lot functions and calculations and it grows every day. Calculation errors are getting harder to solve as my loggs are really big.
The website is used by employees, so there is a lot of traffic between the website and the mysql database. Is it possible in any way to append comments to the queries so they show up in the log-files?
The standart ones (I guess #..#) get lost after the query is parsed from the mysql-server.
Im just trying to get a better overview about my log-files.
For example it could be something like this:
$query = "UPDATE something SET column = 'input' WHERE ... #ticket-calc#";
$result = mysql_query($query, $db) or die(mysql_error());
I would like to show up 'ticket-calc' for this query in the log-files.
Everything I tried hasnt worked. It looks like the server is saving just the raw-command without anything appended.
I also thought about just comparing a string (the comment) with 'true' what shouldnt change the general query.
[query] ... AND 'ticket-calc'= true
I hope there is a more clean way to get it.
Solved this by using /* comment */. Thanks!
I have same approach to log (in MySQL) where are my queryes in database. I use "--" to solve the problem.
$pdo->exec("-- GETTING USERS");
$stmt->execute();
By the way, is better make all logs in your application, inclusive, log over querys. Aka
file_put_contents('TRY TO EXECUTE QUERY: ' . $stmt->queryString);
$stmt->execute();
As no one of the ppl who helped me out in the comments want to post their solutions as an answer, Ill do so to get this topic closed.
Im using /* .. */ to mark my queries.
Thanks for the help #AlexGreg, #davidkonrad and #davidkonrad.

how to delay PHP script until Mysql insert query is committed

In the website I am developing the Users have to sign Up or register.
when the users submits the form for registration the user should get logged automatically with his UserName...
Now, I insert the data into Mysql database followed by retrieving the same data from database with Select statement but the problem is that Select statement is executed faster then the Insert (or something) and it results in Fatal error... I want the PHP script for retrieving data wait until the Insertion is committed... How can I do that ? thanks
First check whether query has been executed successfully or not.
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Using PHP, requests to a RDBMS such as MySQL are always performed synchronously. Then, the behaviour you describe might come from a not-commited transaction initiated prior your INSERT SQL statement. Indeed, if the transaction is not finished and you perform a SELECT SQL statement on the previously inserted table, you will see nothing new because the RDBMS does not know yet if the inserted data must commited or not.
The first thing you might want to check is whether or not a transaction is begun:
Look in the execution stack if an SQL transaction is begun prior your call to INSERT.
Check if the configuration parameter autocommit is set or not (see https://dev.mysql.com/doc/refman/5.0/en/commit.html).
If you are not performing formal transactions in your code, enabling autocommit might be a solution. Otherwise, simply do not forget to send a COMMIT statement when appropriate.
I am facing a similar problem.
After being unable to fix it properly, i've ended UP adding a sleep(1); after my INSERT/UPDATE query.
It seems that the server may not been working properly, because queries are executed syncronous. Im feeling that my problem is related to memory or filesystem/cache delays at VPS. Data takes around 0.100 to be really updated, when it should be reflected at runtime.
I am not sure where the problem really comes, just telling you "how to delay PHP script".
Regards.

Query to delete comment

So, I almost have my delete function working, I have the page loading correctly without any fatal errors but it's still not doing the trick.
Here is the portion of my while loop:
echo "<a href='http://www.###$#%##%#.org/Profile.php?id=".$prof->id."'>";
echo " delete";
echo "</a>";
And here's the portion of my query which is obviously wrong:
$query = "UPDATE `ProfileComments` SET `status` = 'dead' WHERE `id` = '".$prof->id."' LIMIT 1";
So, how do I get this to delete the comment on the page? And mark the status dead in the database?
From your sql it looks like you are doing a soft delete, which is fine. Your select statement needs to exclude comments that have a status of 'dead'
SELECT columns FROM ProfileComments WHERE status != 'dead' AND id = {$prof->id}
Of course that's a guess without seeing how you populate prof->id when you generate the link.
There's a couple of other problems with your post though:
As others have suggested, you should use parameterized queries otherwise you leave yourself open to sql injections
You shouldn't be doing the delete via a get request (using a naked anchor). Either do the delete using AJAX or via a form. Modifying server data via a get is a bad practice.
Just because you're not seeing fatal errors, your code doesn't have to behave like you want it to.
Examine the content of $prof->id by, inside of the loop, putting it through var_dump and then die, this will let you control what the property holds at the moment you want to print it out.
The same thing applies in your query, var_dump to see what it contains.
Not seeing more of your code (and not knowing your experience level), I'm not sure where you actually need help, so forgive me if my question/advice is obvious and too basic for you:
How is $prof->id getting populated in Profile.php? Are you pulling it off of the request (like so: $id = $_REQUEST["id"])?
And as Michael said, do please use parameterized queries. (If you don't care about children, at least think of the kittens!)
Your SQL statement looks fine, and the html code is fine too. To locate the problem
You need to make sure your SQL is getting the right parameter, the id in this case. just echo it out, if the id is missing or wrong you know $prof->id needs fixing.
also check the link dumped by your php, again does it contain the right id?
Make sure you don't have any errors before you execute the SQL, basically make sure that SQL query statement is called. and make sure your db connection is live.
When mysql statement is wrong it won't throw a fatal error, you need to print the error yourself by mysql_error().

MySQL increment value of attribute - misses sometimes(?)

I have a column called views in my table A. I want to increment the views column like so:
UPDATE A set views = views + 1 WHERE ID = blabla LIMIT 1;
This seems like the way to do it, at least to me.
Or so I thought.
Seems like when I (from PHP) do:
$views = get_viewcount($id);
$views++:
save_viewcount($id, $views); //here we just update views with the $views variable
This always works. But carries the penalty of an extra roundtrip to the DB with the get_viewcount and then incrementing it in PHP and sending it back. It always works.
The SQL statement above "sometimes" works. I know - I too hate the word "sometimes" in programming - but put another way, I cannot say WHEN but at times it doesn't increment it when I do it in SQL directly in one go.
Suggestions?
You need to isolate the bug in your system. It is very unlikely to be a problem with MySQL.
I would suggest running a ton of tests, and looking at database logs, etc. It is most likely the query is simply not getting executed due to some logic in your system, or due to the request dying/ending before it reaches the query.
You must be doing something wrong.
If you tell mysql:
UPDATE foo SET views = views+1 WHERE id = 1337;
it will increment it.
Try it on the command line.
Whatever code you're using to run the sql is failing, not the sql statement itself.
And what's the point of LIMIT=1 on an UPDATE query?
What I recommend doing is to set PHP to echo out the query it's running, the result it's getting back, etc., etc. Everything you possibly can. Look at SQL logs if applicable to see what queries are being run on what tables. Basically you need to see exactly where the fail point it.
When you state that the SQL statement sometimes works, is that on a basis of being called from your code, or being called via a mysql (assuming that is what you are using) prompt? If it's the prior, have you tried running it in a command prompt to see if you get the same result as your code? If not, then you can rule out the database and start looking specifically at your code.
Good luck!
I would look at where and when you do your BEGIN TRANSACTION / COMMIT processing.
It could be you are not checking the SQL return code and missing a "DEADLOCK" warning.

Categories