MySql query to Azure SQL query failing - php

I am trying to move my web site to Azure, and so to rely on their SQL servers, while before I was using Apache/MySql.
I have some difficulties trying to convert the queries, as they do not work anymore.
The following works if i connect to the MySql db, but it fails if I change the connection to the Azure SQL.
string(178) "SELECT * FROM schemedb.users WHERE CreatedBy=20 ORDER BY dateCreation Desc" string(177) "SELECT IdUsr FROM schemedb.userssettings WHERE User=20 ORDER BY Id Desc"
All connections to the Azure SQL server are correct, as a simpler query before these gets executed fine. But then I got stuck here.
The php error console says
Fatal error: Call to a member function setFetchMode() on a non-object in ...
To understand where the problem was I used var_dump on the queries, to see where they were failing, turns out that if I change them to:
string(178) "SELECT * FROM schemedb.users ORDER BY dateCreation Desc" string(177) "SELECT IdUsr From schemedb.userssettings Order By Id Desc"
These queries are then valid, so I guess that the problem is the where clause then, but I have no clue of what's the problem.
-- EDIT
I've made some test, turns out that if I manually write the query, it works as expected.
What I mean is that the above mentioned query is build through several php custom methods like so:
." WHERE ".$this->buildWhere()." ORDER BY ". etc.
But if I type there WHERE CreatedBy = 20, then it works.
My best guess is that it is a format problem, since in SQL Azure the data type in the query must be of the same type of the one declared in the column.
However, I cant' understand what's wrong, since my custom method is writing the value vithout ', therefore as a number, not a string...

Related

CodeIgniter adds new line characters in my query

I wanted to update a column in my database table, the update should just add a numeric value to the existing one.
But this time around, I'm writing the query with CodeIgniter Query builder, the issue is that when I run the script, CodeIgniter throws an Sql Exception below:
"message": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '11:01:37\nWHERE `user_id` = '26'' at line 1"
As you can see, it added a new line character to the query string.
The PHP code below is the query in CodeIgniter
$userModel->set('reputations', 'reputations+10', false)
->where('user_id', $user_id)
->update();
One thing I noticed is that if I removed the false (the third parameter) which tells CodeIgniter not to escape the column name, there won't be any error, instead '0' will be updated at reputation column.
I don't know what the problem might be, I could have moved on by writing a custom query, but, I wanted to be sure that I'm not doing something wrong.
P.S: custom one will look like this:
UPDATE users
SET reputations = reputations + 10 WHERE user_id = $user_id
Note: in the above error message you might be wondering where the digits in the error came from i.e
'11:01:37 in '11:01:37\nWHERE user_id
It is the value of a column in my table which is also updating along side reputation column.
Thanks amigos.
Could it be your code editor generating the newline?
Anyways, one fast way to avoid the problem is to use codeigniter query method:
$userModel->query("UPDATE `users` SET `reputations` = reputations + 10 WHERE `user_id` = $user_id)
Not the cleanest solution but it makes sure it works! :)
Mattia

Why is SQL Server Express replying with replacement character 65533 in php?

I have a php script running on linux which queries a MSSQL Server Express database running on my dev machine (windows) using the php mssql driver.
I'm able to connect and select a database, and get no errors.
I put the query directly into a query window in MS SQL Server Management Studio and I get the correct data back. However, if I run the same query from my PHP script, the data that comes back has a bunch of "replacement characters", described here: http://www.fileformat.info/info/unicode/char/0fffd/index.htm
It looks like this: �
For instance, when querying for a list of IDs, eg "SELECT DISTINCT PageId FROM Pages", the query window is showing a list of IDs in the following format:
C961277D-D8BE-4337-82CF-003F6E7951E2
However, when I run the same query in PHP, this is the result:
'���K�#�t��#/
I AM getting the same number of results, so the SQL Server seams to be interpreting the query correctly, but in the results, the character length is wrong, none of the characters match, and the format is wrong. Anybody have any idea what is going on?
Thank you for your help.
Relevant code here:
$this->connection = mssql_connect(
$this->configuration['servername'],
$this->configuration['username'],
$this->configuration['password']);
$query = 'SELECT DISTINCT PageId FROM sf_CmsCtrlLinks';
$result = mssql_query($query);
while ($row = mssql_fetch_object($result)){
// Results in "page ID: 7��"�O�5,���"
echo "page ID: " . $row->PageId;
}
SQL Server returns GUIDs to PHP in binary format.
If you want it as a readable format, you can just make MSSQL pass it back to you as a string;
SELECT DISTINCT CONVERT(VARCHAR(38), PageId) as PageId FROM pages
If you don't need them readable, not casting them and just using them as an "opaque" id should work without problems.
I can't test it myself, but mssql_guid_string() may also work to convert them to readable form.

PHPMYADMIN returns result, PHP Query doesnt

So, I created a query in PhpMyAdmin to pick one random online member that is part of a certain group. It works fine in PhpMyAdmin and does exactly what I want. However, when I run this query using PHP it does not return anything. I simply get 'NULL' when I use var_dump($result).
$sql= "SELECT
ow_base_user_online.userId,
ow_base_authorization_user_role.roleId
FROM
ow_base_user_online
INNER JOIN ow_base_authorization_user_role ON ow_base_authorization_user_role.userId = ow_base_user_online.userId
WHERE
ow_base_authorization_user_role.roleId = 14
ORDER BY
RAND()
LIMIT 1";
$result = OW::getDbo()->queryForList($sql);
Please, does anyone have any ideas?
Please does anyone have any ideas?
Yes, use error reporting from your mysql query and see what it returns. Check PHP errors too (can get auth warnings to show something daft like bad user/password etc).
Also, for testing, try removing the class you use and stick the DB connection code above that query and query all direct from a test script. If it works fine, then your query is ok, as is the connection and credentials.
From there you can work backwards checking the class you have.

Calling for where tinyint is 0 (used as boolean) in query

I am currently using tinyint to store boolean values in mysql, and am trying to query a database but it is failing. The error I am getting is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given which if im not mistaken just means the query didn't work. Before I post the query let me say I am using practically deprecated php for the query, however it's not going live and I just need it to work real quick. I know this stuff is all being updated so feel free to share any relevant materials (I do need to get caught up as it is) however the solution I am looking for is for my old school query. The query is:
$sql = mysql_query("SELECT * FROM contact ORDER BY id ASC WHERE read='0'");
where read is the tinyint in question.
I have tried WHERE read=0
and WHERE read=false
None of these are working, I do appreciate any help in advance!
You need to structure the query correctly:
"SELECT * FROM contact WHERE read=0 ORDER BY id ASC"
WHERE comes before ORDER BY.
Additionally, "mysql_num_rows() expects parameter 1 to be resource" is happening because you're calling a method on a failed query - not a resource. You could get a proper error on your query itself with something like mysql_query("SELECT... your query") or die(mysql_error()) But officially we all suggest moving to PDO or mysqli
And using their respective error reporting utilities.

MySQL - Simple search for ID and return name

G'day,
I'm not familiar with MySQL and this will probably be an easy question!
I am trying to mod a Joomla plugin and am working with this code that works well for a similar function:
$q="SELECT `".$naming."` AS naming FROM `#__users` WHERE `id`='".$jomsocial_event->creator."' ";
$db->setQuery($q);
$eventcreatorname = $db->loadResult();
$eventcreator = ''.addslashes($eventcreatorname).'';
What I need to do is lookup the field id in the table community_groups and return the matching field name. What I have is (note that $jomsocial_event->contentid contains the group ID):
$q="SELECT `".$naming."` AS naming FROM `#__community_groups` WHERE `id`='".$jomsocial_event->contentid."' ";
$db->setQuery($q);
$eventgroupname = $db->loadResult();
$eventgroup = ''.addslashes($eventcreatorname).'';
It returns nothing as the query is wrong; what should it be for my usage?
I'd work backwards from the database.
i.e. turn on SQL logging and look at what's actually arriving in the database. Tweak as necessary by playing with the resulting SQL until you get what you want (and expect) and then implement that in your code.
Take a look at your generated query in the debugging from Joomla.
Run it against mysql directly and see where it goes wrong.
Also, I'd use the JDatabaseQuery API because you are much less likely to get errors with quoting etc. It looks to me like you are treating id as a string not an integer.

Categories