Get microsoft server 2008 sql query working on phpmyadmin - php

I have the following query working on sql server managemengt studio but it does not seem to work on phpmyadmin, Can anyone figure out what is wrong, It gives this error message "#1064 - 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 '1 google_rank FROM eig_ranking mi WHERE mi.project_id = m.project_' at line 7"
I am using MySQL Version : 5.5.20 and php Version 5.3.13 and microsoft server 2008
SELECT project_id,
google_rank,
COALESCE(
(
SELECT TOP 1 google_rank
FROM eig_ranking mi
WHERE mi.project_id = m.project_id
ORDER BY
project_id
), 0 - google_rank AS movement
,keyword
,domain
FROM eig_ranking m where DATEDIFF(WEEK,rank_date, GETDATE())= 1 and google_rank!=0
order by movement desc

It your database is MySQL, then you need to write MySQL syntax and not Microsoft SQL Server syntax - replace TOP with LIMIT, and DATEDIFF arguments with the MySQL format, GETDATE() with NOW(), etc.

There is no SELECT TOP n expression on MySQL.
In general on SQL Server syntax is a little bit different than on MySQL. You have to adjust SQL syntax details to MySQL (e.g. this TOP).

Related

Join by key of JSON column giving error in MySQL 5.6 but working in MySQL 8.0.19

My current code is,
$query->leftJoin('templates as t', 't.id', '=', DB::Raw("CAST(revisions.value->'$.template_id' AS UNSIGNED)"));
In this case value is a JSON column in revisions table.
It is perfectly working in MySQL 8.0.19 but throwing error
"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 '>"$.template_id" AS UNSIGNED) left join" in MySQL 5.6
Json support was added in mysql v5.7.8 only, therefore no json operators are supported in v5.6. The column path operator (->) was added in v5.7.9. You need to migrate to at least v5.7.9 for this code to work in mysql.

Why isn't MySQL allowing access to columns with dot notation?

According to the [PHP manual item on MySQL Field tables], I should be able to use the following syntax to access table columns with PHP 5.3 and MySQL:
$query = "SELECT account.*, country.* FROM account,
country WHERE country.name = 'Portugal' AND account.country_id = country.id";
This syntax works on the live site on which I'm trying to work locally.
However, running the same version of PHP with the syntax above on my machine throws a syntax error on the star:
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 '*' at line 1
EDIT:
Here is the version of mySql this isn't working on:
Server version: 5.1.43-community
Protocol version: 10
Using PHP 5.3.0 on Apache 2/2.11
I appreciate the help on this. I can't know exactly what my problem was but a few things I changed:
1. The original server was using fastcgi while I was using apache, this i changed
2. The original server was running non-thread safe PHP but I was, this I changed by installing a module on wamp as well as a NTS version of the same php
I wont leave as answer bc I dont know what it is
It looks like you're trying to retrieve all of the columns from account and country. If that's correct, you can use the following (I'm also going to use the JOIN as #Juan mentioned in his comment):
$query = "SELECT * FROM account JOIN country ON account.country_id = country.id WHERE country.name = 'Portugal'";
Because you joined the tables you can't use the '*' wildcard to grab all the columns from each table individually. This is why MySQL threw an error.
To get the all the columns from the results of your join you can use:
$query = "SELECT * FROM account,country WHERE country.name = 'Portugal' AND account.country_id = country.id";

MySql query - syntax error

The following code is an SQL query that I am using in PHP.
$sql = "INSERT INTO updates (update,user_id_fk)
VALUES ('$post_update','$username')";
It does not work - I get the following error message when it is run.
Error: 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,user_id_fk)
This syntax loks fine to me - it is the same as I found on W3Schools.
The mysql verion I am using is 5.5.24
update is a reserved keyword backtick it
INSERT INTO updates (`update`,user_id_fk)
check the list below and in future do not use these words for your table or column names
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

MySQL query working in phpmyadmin but not in PHP

i've created a mysql view which is expecting 2 parameters and i'm able to query it without problems in phpyadmin with the following sql-string:
SET #date1 = '2014-02-06';
SET #date2 = '2014-02-07';
SELECT * FROM _myquery
it will not work under php - i'm getting the following error:
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 'SET #date2 = '2014-02-07'; SELECT * FROM _myquery' at line 2
when it's working in phpmyadmin, shouldn't it also work under php?
any idea what's wrong?
Do you really need the MySQL variables? because your SELECT query is not using them.
You can try just with
SELECT * FROM _myquery
But if you really need to use MySQL variables, take a look at this:
Mysql Variables not working through php mysql query
MySql variables and php

MySQL Syntax Error When Using Data From MSSQL Imported Tables

I have imported a number of tables (structure and data) from an MSSQL DB into mysql - all data is in UTF8
I can pull data from the old tables, but when I use any of the data from the imported tables in a query from PHP I get a syntax error like this:
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 ''data' at line 1
for this query
SELECT * FROM tableName WHERE field='data'
This only happens if executing the query from PHP, if I echo out the query from php and then copy, paste and execute the query from phpmyadmin then it runs fine.
Any ideas???
I'm using PHP 5.2.17 running on Apache 1.3.42 and MySQL 4.1.22-standard
If you using query like this then query should be :
E.G
"SELECT * FROM tableName WHERE field='data'";
or
'SELECT * FROM tableName WHERE field="data"';
Please check for "Field" data-type whether is Varchar or sumthing else!

Categories