MySQL query working in phpmyadmin but not in PHP - 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

Related

Why $wpdb->insert() makes sql syntax error?

I am using wpdb->insert to insert a row to the table,
like this
$wpdb->insert($table_name, array("title"=>"foo"));
$wpdb->last_query returns the below query
INSERT INTO `some_table` (`title`) VALUES ('foo')
But i get a error in sql syntax
WordPress database 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 '1' at line 1
The error is little confusing, i passed a string 'foo' in to insert statement, but somehow it is showing '1' in the error.
This error occurs when i am running the $wpdb->insert inside phpunit, i copied the query and ran it on my test wordpress database and this query works correctly in the different method in the same test suite, it works fine.Any idea how to fix this error?
Please try this code. The recommended way (as noted in codex):
$wpdb->insert($table_name,array('title'=>'foo'),array('%s'));

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

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