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

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";

Related

Receiving MySQL Error in PHPMyAdmin

BACKGROUND: I am using PHPMyAdmin tool to run MySQL queries. MySQL version is 5.1.55. I have been using MySQL and this PHPMyAdmin tool for about 7 years and have never seen this error. I am trying to do a simple update query changing ne to gb (column = team, table = info). When I use PHPMyAdmin to try to make the changes,
I get the error message:
UPDATE `pickem`.`info` SET `team` = 'gb' WHERE CONVERT(`info`.`team` USING utf8) = \'ne\'.
QUESTION: What is going wrong here? What is the CONVERT Using UTF8 message coming from. How can I get this to just update the fields I want? I have tried to type the SQL code in myself ex: update info set team ="gb" where team = "ne" , but that does not work either. I get the error message:
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem
ERROR: Unknown Punctuation String # 22
STR: =\
SQL: update info set team =\"gb\" where team = \"ne\"
It seems the system is putting the slashes in there and it is not letting me do this simple update query.
I appreciate any advice on how to fix this.
Thanks!
You are using phpMyAdmin, right? Try using HeidiSQL.
Also check your PHP version, gpc_magic_quotes is already removed at PHP 5.4.0
I tried running your code on phpMyAdmin, removed the backslashes at \'ne\' and its running fine.
Try running a simple update query in HeidiSQL and check if the problem exists.
UPDATE `pickem`.`info` SET `team` = 'gb'
If it runs fine, modify it like
UPDATE `pickem`.`info` SET `team` = 'gb' WHERE `team` = 'ne'
Additional sidenote - check the encoding and data types. You might have overlooked something...
If your query runs fine on HeidiSQL, you can safely assume that something is wrong with your phpMyAdmin installation/configuration
EDIT:
Are the values you are providing contain '\' inside them? If so, you have to do it like
UPDATE `pickem`.`info` SET `team` = 'gb\\'
EDIT 2:
For the meantime, try to use this to solve your problem temporarily.
UPDATE `pickem`.`info` SET `team` = CONCAT('en') WHERE `team` = CONCAT('gb')
EDIT 3:
If all else fails, you probably should get this or this. Since you are on PHP 5.3, I am afraid to say that the gpc_magic_quotes option might have been enabled by your hosting provider. Contact them.

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

Get microsoft server 2008 sql query working on phpmyadmin

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).

PHP/MySQL error while trying to create MySQL variable

I'm running this code in PHP:
mysql_query("SET #update_id:=NULL");
echo mysql_error();
And this is what 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 '' at line 1
Also this same code runs perfectly in PHPMyAdmin. What am I doing wrong?
Additional information. I'm trying to write a query described here:
How to get ID of the last updated row in MySQL?
But the problem right now is that I even can't run a simple query to create variable.
P.S. Ok, now it seems that it desn't work because of some previous queries that are not related to this one. If i move this query to the top of the php file it works. Also if I try to make this:
mysql_query("SET #update_id:=NULL; SELECT #update_id;");
It fails with syntax error. But this works fine:
mysql_query("SET #update_id:=NULL;");
mysql_query("SELECT #update_id;");
Does somebody knows what am I missing here?
Why can't I run two commands in one query and why they're the separate queries are related to each other?
mysql_query("UPDATE your_table SET update_id=NULL");
Check this it may be helpful
SELECT #update_id IN("SET #update_id:=NULL");

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