I use php to fetch data from a mysql database for a web application that I am working on. I have over a hundred php files, each with a number of sql statements. Currently I run the risk of forgetting to update queries in other files when a change is made.
Is there a way to better manage all these sql statements/queries?
I was thinking along the lines of having a file with a list of different functions each querying the database for the relative sql statement. It can be the case that a particular statement is used in multiple files. Instead of having to write out each statement in each file, I can just call the relevant function with the sql statement. Then if I have to update the statement for any reason (i.e. additional fields added to the table, etc) then I can update the query in one place, I dont have to search through multiple files and make an update, and I dont have to worry about missing a file to update.
Im only not entirely sure of whether functions is the best way to go about this because of scope. The config file has the db connection credentials, il have to think about how to pass variables to the function,... its a rabbit hole I dont want to go down yet unless I know im on the right track.
Looking forward to any help and advice as well as any examples or resources to assist
Many thanks.
I have scenario where I have to send different queries to the database to get the result and display it.
Using $mysqli->multi_query in PHP we can send all the queries together to the database and get the results in one shot .Here is the link for it
http://se2.php.net/manual/en/mysqli.multi-query.php
Can I do the same using Yii Command ..? I see QueryAll is returning only for the first query If I combine the queries together separated by ";"
Thanks for checking .
I've made an extension for this.
https://github.com/javijuol/yii-CDbExtendedConnection
It's in a very early stage. Give it a try.
I am building a report using php and mysql, i have multiple queries going on in one go on one page and as you can imagine this is putting a lot of stress on the server, now what i wish to do is get the first query to start and before launching the second query, it checks if the first query has finished and it goes on like this until it reaches the last query. And just to be clear, one query at a time does not put that much stress on the server but several in one go does. If anybody has any idea or has an alternative please let me know.
By default, PHP will not execute next MySQL query or any other code at all before previous query is finished.
I'm troubleshooting a bug and trying to rule out all possible explanations for why I'm witnessing the behavior that I am. I'm executing a number of MySQL queries in PHP (via CodeIgniter's Active Record class) and one explanation for the behavior that I'm seeing is that the queries aren't being executed synchronously, i.e. that PHP isn't waiting for the query to complete before issuing the next one.
I've always coded under the assumptions that if I insert something into a MySQL table via PHP, and then my next line of code executes a select, the results of my insertion will be available in the next statement. Are there any exceptions to this being the case?
Thanks for helping me preserve my sanity...
If you select it on the same server, and are using the same session/connection, and you haven't used INSERT DELAYED it should exist indeed, but loadbalanced MySQL servers / implementations / caching may divert SELECT's to other servers or data locations....
I have never encountered an error like this before. I was using prepared statements when it stared and have tried everything to correct the problem including stripping the form down to it's bare components.
The following code, when executed, is creating a duplicate row.
$sql = "INSERT INTO inventory
VALUES ('','$stocknum','$year','$make','$model','$price','$body','$mile','$engine','$trans',
'$drive','$doors','$fuel','$vin','$des','$featured','$sale','$features','$safety','$airbags','$power',
'$exterior','$interior','','','','','')";
$insert = mysql_query($sql,$connection) or die(mysql_error());
$name = mysql_insert_id();
I can't wrap my head around why it would do this.
I had the same problem in a project while using and orm library to access to the database. Also tried tested with mysql directly
After almost one day testing in multiple browsers and getting diferente results, i've found out that the extensions that i used (Webug) for Chrome caused tha recall to the page. After disabling the extension, it worked
I've tested some extensions that caused that problem... In chrome: Webug.
Hope it helps
The insert statement is possibly getting called twice. Did you add logging to make sure this code is only running once? And did you search to make sure there's no other code to add inventory records anywhere else?
How many columns are in the inventory table? Is the second row an exact duplicate of the first? I don't know PHP's DB interface but I could envision a bug where, if you give it more fields than there are columns, it attempts to create multiple rows.
EDIT: A little research on the MySQL documentation finds:
INSERT statements that use VALUES
syntax can insert multiple rows. To do
this, include multiple lists of column
values, each enclosed within
parentheses and separated by commas.
Example:
INSERT INTO tbl_name (a,b,c)
VALUES(1,2,3),(4,5,6),(7,8,9);
The values list for each row must be
enclosed within parentheses. The
following statement is illegal because
the number of values in the list does
not match the number of column names:
INSERT INTO tbl_name (a,b,c)
VALUES(1,2,3,4,5,6,7,8,9);
Depending on the contents of your variables and how the PHP/MySQL driver handles those variables (direct text substition or ? placeholders) the statement being executed may not look like you expect. Try displaying the value of $sql before you execute it.
When you query the table afterwards, are you only selecting from this table or are you perhaps joining it to another table? If you have more than one child record in the joined table you will get multiple results.
Your code looks correct to me, unless there is something in the rest of the page I'm not seeing.
In the absence of any real clear conclusion on this my observation is that I think Red Element is on the right track when he states 'I think it has something to do with my web host as now every script that adds SQL data is doing the same thing'.
I had the problem outlined above and could not see what on earth caused this until I ran the same code on a different platform and it worked fine. I was originally testing in a localhost WAMP configuration but when I promoted the code to a real server, it worked no problem.
Therefore I suggest that if anyone else has the problem it is worth a try on a different server config.
I'd guess you have a problem with an .htaccess file which is making 2 requests to the same script. Log a message with a timestamp when you do your insert.