I am currently working on the following sql code which should insert a new data set only if it doesn't exist so far. The sql code works fine when being executed in phpmyadmin. If I execute the code within php I get the error (see below).
The sql code is the following:
INSERT INTO `historiclist` (`id`, `date`, `name`, `idnumber`, `prop1`, `prop2`, `prop3`, `difflimit1`, `difflimit2`)
SELECT * FROM (SELECT
0 as `id`,
1515529465 as `date`,
'johndoe' as `name`,
'381' as `idnumber`,
105 as `prop1`,
240 as `prop2`,
60 as `prop3`,
'-10' as `difflimit1`,
'-10' as `difflimit2`
) AS tmp
WHERE NOT EXISTS (
SELECT `id` FROM historiclist
WHERE `date` = 1515529465
AND `name` = 'johndoe'
AND `idnumber` = '381'
) LIMIT 1;
The mysql error I receive is:
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO historiclist (id, date, name, idnumber, prop1, `prop' at line 21
The mysterious situation is also that prop2 is not fully displayed. So it cuts the 2 in the end of the name as well as e. g. prop3.
What am I missing?
//Edit I was missing something else as well ... here my php code:
$sql1return = sqlQUERY($connection, $sql1);
$message = 'Invalid query: ' . mysql_error() . "\n";
function sqlQUERY($connection, $sqlinput){
$return = #mysql_query($sqlinput, $connection);
return $return;
}
The database connection works just fine in any other place (I know it is a bit outdated however).
Related
I have a page that updates the data of a specific user. The user has position, which is a foreign key. The query update (below) works fine without the position, but with the position I get the following error.
Query :
$queryUpdate = "UPDATE visitorsystem.employee SET idNumber = '$idNumber', name = '$name',
surname = '$surname',
position = 'SELECT positionid FROM visitorsystem.position WHERE positionName LIKE '%$position%'',
email = '$email'
WHERE employeeid = '$empId'";
$resultUpdate = mysqli_query($connection,$queryUpdate)
or die("Error in query: ". mysqli_error($connection));
Error in query: 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 'SELECT positionid FROM visitorsystem.position WHERE
positionName LIKE '%Informat' at line 3
I have tried to work my way around by using inner join as I have seen some solutions given here on stack but nothing has worked. Any Suggestions ?
Subqueries go within regular parens, not quotes, so in a general sense:
SELECT x FROM y WHERE z IN (SELECT z FROM a)
Single and double quotes (by default) are only for string values.
I have an SQL statement which runs perfectly when used on phpmyadmin,
$q1 = " SET #pos = 0; UPDATE `songs` SET `tweek` = ( #pos:= #pos+1) WHERE `approved` = 1 ORDER BY votes DESC ";
my connection is fine, every other thing is fine but I keep getting an error when I use it in my php code.
mysql_query($q1, $link) or die(mysql_error());
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 `songs` SET `tweek` = 1 WHERE `approved` = 1 ORDER BY
votes DESC' at line 1
Please help.
SET and UPDATE are two queries and you must split it into two different calls of mysql_query
I have syntax error with my code
$insert = #mysql_query("INSERT INTO topics (t_title, t_desc, t_pic, t_link, t_date,cat_id)
SELECT '$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id'
WHERE NOT EXISTS (SELECT t_link
FROM topics
WHERE t_link = $t_link
)
")or die(mysql_error());
This returns an 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 'WHERE NOT EXISTS (SELECT t_link FROM topics WHERE t_link = 'showthread.php?t=120' at line 3
I thought that the problem is with t_link = $t_link
But when i replaced it with normal value , the problem persists.
Any help ?
You missed the FROM on first SELECT
SELECT '$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id'
# MISSED HERE FROM ???
WHERE NOT EXISTS
Here solution for FROM CLAUSE, please, check as solution chumkiu's answer, not mine.
create table a ( i int);
insert into a (i )
select 1
from dual
where 1=2;
insert into a (i )
select 3
from dual
where 1=1;
Results
If t_link is has a unique index in the table, you can do:
$insert = #mysql_query("INSERT IGNORE INTO topics (t_title, t_desc, t_pic, t_link, t_date,cat_id)
VALUES ('$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id');
The IGNORE keyword tells it to do nothing if the insert would duplicate a unique key constraint.
I am running the following query :
SELECT #newNo := MAX( category_code ) FROM category_master;
INSERT INTO category_master VALUES (#newNo +1, 'Test')
The query runs flawlessly in phpmyadmin but it shows a database error when run using codeigniter :
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 'INSERT INTO category_master VALUES(#newNo+1, 'Test')' at line 2
what could be the reason ??
In codeigniter model i use the following code :
$query = 'SELECT #newNo := MAX(category_code) FROM category_master;
INSERT INTO category_master VALUES(#newNo+1,
\''.$category_name.'\')';
$result = $this -> db -> query($query);
You cannot run two queries at once. Seperate them:
$query = 'SELECT #newNo := MAX(category_code) FROM category_master';
$result = $this->db->query($query);
$query = 'INSERT INTO category_master VALUES(#newNo+1, \''.$category_name.'\')';
$result = $this->db->query($query);
EDIT:
On your second query it is recommended to use query bindings:
$query = 'INSERT INTO category_master VALUES(#newNo+1, ?)';
$result = $this->db->query($query, $category_name);
Make sure the query does not contains any special characters. The browser will convert the special characters, so the query is running in phpmyadmin.
To know the special characters, echo the query and copy it and paste in an editor like dreamweaver, it will show you the special characters. Hope this helps.
I run this command in SQL Workbench and it returns my desired results, but it return a syntax error in the browser...
$sql = "SELECT
SUBSTRING(`last_name`, 1, 1) AS alpha,
SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
`idClients`,
`type`,
`first_name`,
`middle_name`,
`last_name`,
`address`,
`primary_number`,
`secondary_number`,
`home_number`,
`office_number`,
`cell_number`,
`fax_number`,
`ext_number`,
`other_number`,
`comments`
FROM `clients`
WHERE `user_id` = 2
AND `is_sub` = 0
AND `prospect` = 1
ORDER BY `last_name`";
Also user_id, is_sub, and prospect are of the INT data type if anyone wants to know. I tried to treat them as strings in the query, but that still didn't help.
this is the error 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 'AND prospect = 1 AND type = 'Buyer'' at line 1
You're not showing us the same query, or the relevant PHP code, as nowhere does the above query use the string 'Buyer'.
That said, you may need to escape the column name type with backticks:
AND `prospect` = 1 AND `type` = 'Buyer'