Migrating from MySQL to MS SQL - php

I have a PHP (CODEIGNITER) application which is generally deployed on Apache/MySQL combination. I recently deployed it on IIS8 and MS SQL 11.0.2100.60
I migrated the tables and data by using ODBC connection to migrate to Access database and then again another ODBC connection to migrate to MS SQL. I modified the configurations of my PHP application (PHP.ini, database.php, db_driver.php) to make sure it connects properly and works on IIS.
I am having problem with SQL Syntax now. When I try to run the application it does not give me database connection error (which it was giving earlier) but when I try to log in to the application (it has user authentication) - I get the following error:
Error Number: 42000
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '`'.
SELECT * from ctbl_events WHERE 2017-01-11 <= startdate AND `enddate` >= 2017-01-11 and status=0 ORDER BY `ctbl_events`.`id`
Filename: C:\inetpub\wwwroot\GMS\system\database\DB_driver.php
Line Number: 330
does this mean problem is with ` symbol and if yes would I have to manually go and modify all the SQL queries in my application (would be a gigantic task) or is there any way to handle this.

As far as I know, SQL Server does not support using backticks to escape table or column names (you can use brackets instead). You can easily try this by running a simple query like
select * from `ctbl_events`
If that doesn't work, you will almost certainly have to update all the queries to replace the backticks with brackets.
Second reason the query is probably failing is the dates in your query must be enclosed in quotes, and match SQL Server's date format (this is configurable, so you may need to experiment a little).
So, the query you're trying to run should look a little like this:
SELECT * from ctbl_events
WHERE '2017-01-11' <= startdate
AND [enddate] >= '2017-01-11'
and status=0
ORDER BY [ctbl_events].[id]
Though in this case, you don't really need the brackets around table or column names - it's best to agree a standard for this and stick to it.

Related

How can I fix this syntax error on my PHP MySQL query

I have been trying to make a query in MYSQL, I am doing 2 things in 1 query. My host has a Limit to queries so I need to limit the amount of queries I am doing. When I did the query I get
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 ', (UPDATE users SET last_online=NOW() WHERE
username='Welcome_234')' at line 1
I have put a semicolon ";" but it still doesn't work (This query worked in phpMyAdmin).
The code for the query is:
SELECT * FROM `users` WHERE username='Welcome_234',
UPDATE `users` SET last_online=NOW() WHERE username='Welcome_234'
I have tried many hours to figure out the problem and I still haven't found it.
Most PHP interfaces to run MySQL queries don't support multiple queries per call. You must run them one query per call.
Your host's limit on queries can't be so low that the difference between one and two queries will break it. I would guess the limit is in place to prevent clients from running excessive loops that run thousands of queries. If it is so low that you can't run two queries, then get a different host.

[SQL Server]Column 'retailers.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I am using laravel with mssql and getting this error with simple group by query.
RetailersModel::groupBy('state')->get();
And it returns me error.
I have migrating database server from mysql to mssql.
Mysql is running fine but error occur when try to get record from ms-sql database server.
I have also set strict false in mysql and mssql config in database php.
Thanks in advance.
RetailersModel::groupBy('state')->get();
Try to execute this query
RetailersModel::select('*','state', DB::raw('COUNT(state) as state'))->groupBy('state')->get();

MySQL statement becomes corrupt during query excution

I'm making Wordpress custom page with php. When I execute query, it gives error below:
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 '��brand VARCHAR(70),serialCode VARCHAR(70),QRCodeAddress VARCHAR(70),email VAR' at line 1
As you can see, you can see strange char � that usually happens from encoding problem. But what I wonder is that if I echo my sql statement, it has no any problem. Look bellow:
CREATE TABLE SerialTable(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,brand
VARCHAR(70),serialCode VARCHAR(70),QRCodeAddress VARCHAR(70),email VARCHAR
(70),sex VARCHAR(70),age VARCHAR(70),purchasePath VARCHAR(70),expiryDate
VARCHAR(70),registeredDate VARCHAR(70),modelName VARCHAR(70),colorCode VARCHAR
(70))
And it even works well if I excute in MySql shell.
My procedure is like this:
I'm reading UTF-8 csv file to read table column information
Depending on csv file information, make SQL statement like above and save it in variable $sqlStr
when i echo $sqlStr, it has no any syntax error and even works in MySql shell.
But when $wpdb->query($sqlStr), it give error like above.
I tried to use mysqli_query($conn, $sqlStr) as well. and it give same error.
MySql server info:
Of course connection is localhost as long as web server and mysql server is on same computer.
MySql server collation : utf8_general_ci
mysql server charset : latin_swedish_ci
I've been trying to solve this problem for more than 48 hours and am completely stuck?
It is very likely that your CSV file contains "unprintable" characters that allow it to look normal within an editor.
Try deleting the offending line or lines and recreate them.

SQL Server and tables? Invalid object

Firstly I'm a complete newbie when it comes to SQL Server. I have five SQL Server databases, in all of them except one I can easily do say...
SELECT TOP 10 * FROM dt_Organizers
However one of the new databases require me to do this:
SELECT TOP 10 * FROM dbuser.dt_Organizers
Note that all the tables in all databases have "dbuser" "dbamy" "dbyon" "dbmio" "dbcana" prefixes respectively, this never caused a problem before. Now if I use the query without doing dbuser.dt_Organizers it brings up:
Warning: mssql_query()
[function.mssql-query]: message:
Invalid object name 'dt_Organizers'.
I think you should have a look at
Understanding the Difference between
Owners and Schemas in SQL Server
SQL SERVER – Importance of Database
Schemas in SQL Server

mssql_query weird behaviour : automatically removes double-quotes around column value (and the column type is "integer")?

I can´t find an explanation to this behaviour (SQL Server 2008 + PHP 5.2.14)
-This piece of code works ok:
$query=' select id from News..news where id="727" ';
$conn=mssql_connect("myDDBBServer","user","password");
$idQuery=mssql_query($query);
$row=mssql_fetch_array($idQuery);
echo $row[0]
I have to say that id is an int typed column, so if you copy and paste that query on the sql server query console, you get the following error: "The column name '727' is not valid".
I have no idea why is working via mssql_query, it seems like the php_mssql driver is removing those double quotes...
I´m sure it has to do with php_mssql driver and not some other php.ini property, because if i execute that query using the php_sqlsrv driver i get the same error as through the sql server query console ("column name not valid").
Any help would be appreciated.
See SET QUOTED_IDENTIFIER in Books Online. When it's set to ON then double quotes are interpreted as marking an identifier; when it's OFF then double quotes mark a literal value. ODBC and OLE DB set it ON by default, but different libraries can do what they like and you can set it explicitly yourself if you need to.
The bigger picture is that you shouldn't be embedding literal values in your SQL anyway. Instead, you should be using parameterized queries (however they work in your particular client library) because it avoids issues with SQL injection, quoting and data type conversion.

Categories