update query not working in joomla - php

I'm working on a joomla extension and I'm trying to update entries in my joomla extensions database table using the following code in my model:
$this->_db->setQuery(
$this->_db->getQuery(true)
->update('#__my_table')
->set('position=position+1')
);
$dbres = $this->_db->result();
However it doesn't do anything and outputs no error (Development on and error reporting maximum in global config)
I entered the query directly in PHPmyAdmin:
UPDATE cprn7_my_table SET position=position+1
and it works without any problems.
I read about quoting keys and values with $this->_db->quoteName() or so, but I can't find any examples for that with queries like SET position=position+1 but only SET position=$newval so I don't know exactly what to quote and how.
//EDIT: Found the error, it has to be $this->_db->query() and not $this->_db->result()

A few possibilities come to mind. The first is that the --safe-updates setting is enabled, preventing updates (and deletes) whose WHERE clause lacks a primary key column specification.
phpMyAdmin may turn this setting off, while your default MySQL client settings (in my.cnf, under [client]) may be enabling it. You can use the show variables statement to discover the setting's value. Also, see this doc for more info: http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_safe-updates
A second thought: You can quote the column name using backticks, for example:
SET `position`=`position` + 1
However, if this still seems to have no effect, and, if you have an admin-level MySQL account for your server, you can turn the general log on, then run your query, then turn it off again, then examine your results. See this doc for working with the --general-log and related settings: http://dev.mysql.com/doc/refman/5.5/en/query-log.html
BTW, if your MySQL client provider (or Joomla, or any other tier in the mix) is setting --safe-updates using a SET command (i.e., upon connecting), you will also see this as an executed statement in the general log.

Related

PHP & MySQL UPDATE 10 columns

I want to update customer data which means 10 different columns would be updated in one UPDATE SET WHERE statement but I keep getting a warning in NetBeans that says method length 26 ( allowed 20)
Can anyone tell me what the method length refers to and what another way is to update all those columns in one statement ?
This is an utterly ridiculous hint. You can disable it in
Tools->Options->Editor->Hints->PHP.
It won't disturb you again.
Netbeans is notorious for these informational messages which are really a matter of style preference rather than an actual limitation.
I like to create my SQL code directly using a tool like phpMyAdmin to verify the statement works then port it over to my code.
For what is worth, I suggest you download and install mysql workbench, it will serve as sandbox to test queries before anything else. This always works for me in any development situations

different result executing an sql query from phpmyadmin(good) and from PHP script(bad)

I have a really strange problem, executing a query on a MySQL DB:
from phpmyadmin: the query executes and return good results
from my PHP script: the query executes, no errors, good number of rows, but nearly 50% of the values shown in the table are wrong !
The SQL query used is exactly the same, rather long(250 lines), takes 26 seconds to execute in phpmyadmin, and works good.
On my PHP script the query iss executed with the mysqli_query() the typical way I would say:
if ($res=mysqli_query($mysqli,$q, MYSQLI_USE_RESULT)){
$data = array();
while ($row=mysqli_fetch_assoc($res)){
$data[] = $row;
}
mysqli_free_result($res);
var_dump($data);
}
On the query I replaced backtite: (`) to single quote: (').
Any idea why it's working with phpmyadmin, but not from PHP with mysqli_query() function? Btw phpmyadmin doesn't use the mysqli_query() function?
No errors is thrown, I'm really lost here, if someone has a hint, it would be much appreciated :)
Thx :)
The most likely answer is that something is different between the queries. As you cannot easily trace the difference in 250 lines. Use the MySQL query log to record both queries as MySQL sees them and then compare the differences:
To disable or enable the general query log or change the log file name at runtime, use the global general_log and general_log_file system variables. Set general_log to 0 (or OFF) to disable the log or to 1 (or ON) to enable it. Set general_log_file to specify the name of the log file. If a log file already is open, it is closed and the new file is opened.
Something along the lines of:
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file= 'querylog.log';
.. run queries
SET GLOBAL general_log = 'OFF';
Note that this is a global setting, the server will log queries from all clients whilst the log is enabled. Your MySQL also needs the super privilege to change this setting. Alternatively you can change in the my.cnf config file and restart MySQL.
Once you have the two queries, a comparison should show you where the difference is and guide you to a fix.
YAY after several headaches I solved by chance the mystery, I don't really know why though, but now it's working good.
Here are the small changes I made to make it work:
change the PHP file encoding from UTF8 to ISO-Latin1
retype all non alphha characters to be sure they're ok
In my SQL query I had an accent on a string I checked in a where clause, so I tried this..
The thing is I have many PHP files on the same server, encoded in UTF8 and text with accents that work perfectly.
The only difference here is that the accent is in my SQL query, maybe the server hosting my websites doesn't handle UTF8 characters properly and the system encoding is not well configured ..
Just to be sure, I have a terminal access to server, how can I see the system encoding ?
But problem solved, all good!

Inconsistent Results Set of Simple Mysql Query

I have a large database with a high volume of queries. Recently I noticed that a simple query is returning inconsistent results in the logs in production but cannot be replicated in my dev environment. For example, a query like
SELECT * FROM table WHERE somefield = "value"
would have a result set whose somefield would not equal "value". When I try to replicate the same using the MySql CLI client, I am not able to replicate the issue. The query btw, is handled by php/codeigniter.
Some of the leads I am pursuing right now are
Seeing if the charset of the string passed to codeigniter's where has anything to do with it.
Checking to see if allocating a different user for this query will fix it (no basis whatsoever but worth a try)
Looking for other leads to debug this issue.

Automatically truncate data when inserting

My MySQL Server (running with PHP via PDO on Windows 2008 Server) returns error code 1406 (data too long for field) when inserting strings longer than allowed in a column. The thing is I read somewhere that what MySQL usually truncates the data when it is not in strict mode. I changed the sql_mode in my.ini so that even at startup it doesn't enter in strict mode (it is currently ""), but it is still giving me the error and rolls back, so the data is lost (truncating is the desired behaviour of the site).
I entered to the command line and made an insert with a long string in a shorter varchar field and it does truncate the data and save, but it is the site that doesn't. When I changed the mode back to strict, it didn't truncate in the command line (only the error).
Also, I made the site output the current sql mode, both global and session (##GLOBAL.sql_mode and ##SESSION.sql_mode) and they both output "" but just don't work as desired.
Does anyone know what is causing this and/or how to change it?
My suspicion is that it may have to do with PDO enabled with PDO::ATTR_ERRMODE = PDO::ERRMODE_EXCEPTION, but I have read and can't find anything helpful about that (I don't really think this is definitely the explanation, but I am just putting this out there so that you know as much as possible about the problem).
Thank you very much
You should not really do that - please don't let bad data into your database, and sanitize it in your scripts before you insert.
If you don't know actual width of your VARCHAR columns or don't want to hard-code it in your scripts, you can read it from database by querying INFORMATION_SCHEMA table COLUMNS using query like this:
SELECT
column_name,
data_type,
character_maximum_length,
ordinal_position
FROM information_schema.columns
WHERE table_name = 'mytable'
(you may want to limit this to only data_type = 'varchar').
Having this information and specifically character_maximum_length, you can use PHP substr to trim your input strings to desired length.
Advantage of this approach is that it does not alter any server configuration, and should work for any other databases, not only MySQL.
But if you insist on doing it unsafe way, you can try to temporarily disable strict mode by executing this:
SET sql_mode = '';
After that MySQL should silently trim strings. Read more here.
Probably the mysql server is in "not forgiving" mode, aka. traditional (most times). What you read is true, if mysql server operates in forgiving mode. If you are trying to insert too long string, it can be really important information, therefore mysql will issue a error. You have a few alternatives:
1: Use insert ignore (which will transform all errors into warnings and will proceed to truncating the data)
2: For the current session, set the sql_mode to ''
With either of these your problem should go away.
PS: I have read the error you are getting, but i still think the server is operating in traditional mode (so it's no mistake that i recommended sql_mode set to empty).
PS2: After changing the my.cnf, did you restart the mysql server?

MySQL select all missing a record

I have added a new record to a table using phpMyAdmin
I then run select * within phpMyAdmin and it works listing all records.
However if run the same select * from a PHP page the new record is missing.
I am using PHP 5 and am not sure what is going on here. It used to work in PHP 4.
.php files run as PHP 5 Apache module mode.
Here are a few things you can investigate:
You could be connecting to a different database from the one you think you are (do you have test and production databases?).
You may have an error or typo in your SELECT statement. Copy and paste exactly the query that works into your code. Do not manually edit it afterwards.
You might select the row correctly but display it incorrectly so that it appears not to be there. Try checking how many rows there are in your result set.
Try put sql query to variable and then dump it to log or use var_dump. Then put query to phpMyAdmin and check errors.
Thanks for the answers but found out the answer now.
I had bookmarked a link to an older phpMyAdmin server which looked almost identical and allowed me to log in etc. My host should have forwarded me to the latest server.
I am now using the correct server. Hope this helps someone else.

Categories