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

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.

Related

How do I make my table visible to PhpStorm?

I am using PhpStorm; when I execute my code, the browser shows me:
you have created case
test
Table 'test.users' doesn't exist
But the table users does exist? How do I fix this?
My php code looks like this:
<?php
define('dbuser','root');
define('dbpass','');
define('dbserver','localhost');
define('dbname','test');
$conn = mysqli_connect(dbserver,dbuser,dbpass,dbname);
if(!$conn){
die('error connecting to database');
}
echo 'you have created case';
?>
<h1>test</h1>
<?php
$query = "SELECT * FROM users";
$result = $conn->query($query);
if (!$result) die($conn->error);
?>
And in my Microsoft SQL Server I have a database named test with table users with one user in it.
First you need to sort out the basics.
You seem to have your connection to the database but that is just like switching on the computer. Now you have to do the work.
So:
Are you using MySql or SQL Server?
You need to think/read a bit more. https://www.w3schools.com/php/php_mysql_intro.asp is a good place to start (if you are using MySQL!).
Make a cup of coffee and just BROWSE the mySql section (don't try and understand). Keep hitting the NEXT button and then circle back to look at the bits you need to know.
Have another try (experiment lots!)
Re-edit your question with what you have learned if you are still having problems!
And that's it!
This is not really an answer but it should help you get there.
One last very important thing: edit the question to something useful if you can. Stack Overflow can be very harsh on new users but if you make the effort to correct this then I will certainly upvote you to help you get started on Stack Overflow and hopefully a couple of forgiving souls will help you get back to zero.
My rule of thumb: I never post until I have done at least 30 mins research. Annoyingly, then when I then write the question, nine times out of ten I find the answer as I am editing it so never get to ask my "useful" question.
Keep trying. This place is immensely helpful but you do need to make the effort to get the best from it.

why is this mysql query is not actually updating anything

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.

PHP/MYSQL: What's Wrong With My Query/ PHP variable in query

I've been coding with PHP and MySQL for about a year now and have gotten the hang of it pretty well; constructing really complicated queries with joins and calculated fields and all the other joys of MySQL hasn't been a problem for me in months.
BUT there's something syntactically screwy with the following chunk of code that I can't figure out - even though it's impossibly simple, and, even more infuriating, is closely related to other parts of the project that I'm working on (and which works flawlessly).
Here's the problem code I'm trying to run, followed by the bugchecking I've already done to try to isolate the problem.
If anyone has any suggestions, I'd be totally grateful because I'm beginning to lose my mind.
Problem:
I'm really losing my mind over this, so please don't laugh when you see the code:
$query="SELECT count(somefield) FROM db_name WHERE otherfield='".$myvariable."'";
My query finds no results when using a certain variable as part of a field search - even though I know that there are over 900 records in the database that should match.
Bugchecking:
Because I know the value of the variable I'm passing to the query, I've tried hardcoding it into the query and it works fine.
I've run the query in the MySQL console (again, of course, hardcoded instead of with the variable) and it works fine.
To my mind, these two facts eliminate the possibility that there's something syntactically incorrect with the PHP version of the query.
In order to eliminate all possible database connection issues and to make sure the problem isn't related to iterating through the results returned, instead of trying to get the actual results, I've altered my original query to return only the count of the results and have incorporated the standard or die(mysql_error()) statements during the connection sequence. The query is executing, but is finding 0 results, so that eliminates the possibility that it's a connection issue.
I've verified that the field I'm checking is the correct field for the information I'm looking for (like I said, it runs fine if I hardcode the variable into the query... which, of course, will not be an option in the finished code).
I've checked the type of the variable before trying to pass it into the query (figuring that maybe, because it's pulled from a returned xml earlier in the script, that maybe it was showing up as an array or something). It typed as string.
I've verified that the variable is formatted in the way that I expect it to be found in the database; strtoupper, etc.
I've tried using LIKE '%".$myvariable."'"; still no dice.
Anyone have any suggestions for what I can do to figure out what the hell is going wrong? Thanks so much!
It's not a PHP issue so quotes have nothing to do with it.
The query has no error, so you'll need to debug it step by step.
I. SELECT count(*) FROM table_name
II. SELECT count(*) FROM table_name WHERE field='$myvariable'
Where you're dealing with PHP variables in query, echo the query and run it directly in database to omit PHP's side of error.
III. SELECT count(somefield) FROM table_name WHERE field='$myvariable'
Is the $myVariable escaped? If not, escape it using
$escapedVariable=mysql_real_escape_string($myVariable);
and then run
$query="SELECT count(somefield) FROM db_name WHERE otherfield='$escapedVariable'";
Thanks so much to everyone who tried to help, but I figured it out several hours after posting: The first problem was that I forgot to use mysql_real_escape_string($myvariable)... the reasons for how and why I forgot are manifold, but there it is.
So, after plugging that guy back in (which I had ASSUMED had been in this particular module of my code in the first place, but that's where "assuming" gets you, lol), I thought I had the whole thing licked. Three hours later, and still nothing. THEN I realized that it had to be related to XML that was being parsed into the $myvariable... so around and around we went with that one for a few more hours.
FINALLY, I realized that the real culprit was my eyes (which aren't so great).. what looked like a perfectly legit quoted string while reading a debug echo of the query before running turned out to have leading and trailing white space (which, of course, I instantly removed with $myvariable=trim($myvariable, " "), and, that, of course, solved the entire problem...:<
... Yes, I am an idiot, and I'm sorry, but, after working over this UTTERLY INFURIATINGLY stupid line of code for over 48 hours (I'm used to writing things like:
$query="UPDATE db_one.table_one SET item1='".(string)$result_array[$i][1]."', item2='".(string)$result_array[$i][2]."' WHERE thing3=".(string)$result_array[$i][19];
... and other assorted fun nonsense), I had to resort to asking (because I - no pun intended - couldn't "see" the problem... ugh)... SO... I am an idiot, and I'm sorry (but encouraged by the efforts of all of you nice people who tried to help) and am sorry for wasting everyone's time. I need to learn how to handle XML much *more*.
Sorry and thanks again!
I always wrote it like this
$query="SELECT count(somefield) FROM db_name WHERE otherfield='$myvariable' ";
Try removing the double quotes and dot at the variable name
You shouldn't need the parenthesis around the var. Plus look at other changes.
$query="SELECT count(*) FROM table_name WHERE field='$myvariable'";

PHP Update class (OO)

I have written a PHP OO class which will update 4 fields of a certain row in a table. For now the row is decided by a constant (user with name 'jip')
I have corrected the query in a previous post here, so i'm pretty sure the query itself is fine. So, there must be some sort of error within the class itself. Probaply the vars don't reach the query somehow. I have been looking for hours, but can't find the problem. I have linked both files of the class, since i downt know where the error is, the values just don't show up in the database. If anyone would like to check them, (s)he'd make my entire week! SO here is the link and i hope someone is willing to help :)
UpdateForm.php: http://pastebin.com/dUaZPrn6
Update.class.php: http://pastebin.com/6mnL4DzE
Try replacing mysqli_real_escape_string($conn, $variable) with
$conn->real_escape_string($variable);
For example,
$conn->real_escape_string($this->Lengte_update);
You're using the object-oriented style, so you can't use the procedural escape function. See the docs on mysqli::real_escape_string.
Edit:
The query isn't being executed. You assign the query to $query, but you need to call
$conn->query($query);
For anything to happen in the database.

meaning of this sql statement

I just found a code like this in my new project. and i have no idea wht it does.
$q = "SELECT getnewmsg(" . $_GET['messgae'] . " result)";
i searched for getnewmsg in whole file system and i did not get it. Can you please let me know does it actually mean. i have never seen such sql statement.
If you want to know more about the procedure (in case it is a procedure), you may send the following query:
SHOW PROCEDURE STATUS LIKE 'getnewmsg';
For more information, see the MySQL manual for SHOW PROCEDURE STATUS.
"getnewmsg" is not found in a search of the Mysql site, so it is probably a stored procedure that has been added to your particular database. We have no way of telling what it does.
looks like stored function
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

Categories