Using mysql user defined variables with php - php

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

Related

MYSQL Error #1064 - Update multiple rows

I have to update a dynamic form where the user can add, deleted and update questions in a questionnaire. I've created all my code that will generate one query which is executed once at the end.
I get a problem when I try to update more than one question at the same time (INSERT and DELETE works like a charm).
$query = ''; // append every thing to update in this variable
// for each questions check if needs to be modify, if yes append.
$query .= 'UPDATE `questionnaire` SET `id_category` = '.$categoryValue.' WHERE `id` = '.$idQuestion.'; '
Example of an output
UPDATE `questionnaire` SET `id_category` = 1 WHERE `id` = 1; UPDATE `questionnaire` SET `id_category` = 3 WHERE `id` = 2;
Submit to the database;
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->close();
}
If I only have one UPDATE, it works like a charm. As soon that I have 2 updates, I get this MYSQL error :
errno : 1064
error : 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 'UPDATE `questionnaire` SET `id_category` = 3 WHERE `id` = 2' at line 1
Does anyone have an idea why it only happens when I have two UPDATE ? If yes, how should I fix this.
As suggested by Mike W (Thank you), I changed
if ($stmt = $mysqli->prepare($query)) {
$stmt->execute();
$stmt->close();
}
for
if ($mysqli->multi_query($query)) {
// Success
}

Error when running MySQL query, looking for right syntax

I get the following error when running my .php file:
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 ''feeds_list' SET 'cron' = now() WHERE 'feed_id' = '1'' at line 1
Here is the code:
$sql = mysql_query("UPDATE 'feeds_list' SET 'cron' = now() WHERE 'feed_id' = '$feed_id'") or die (mysql_error());
Thanks for helping me out
Remove the '' around table names and column names, it should be:
UPDATE feeds_list SET cron = now() WHERE feed_id = '$feed_id'
or use a backticks (``).
$sql = mysql_query("UPDATE feeds_list SET cron = now() WHERE feed_id = '$feed_id'") or die (mysql_error());
USE cron Not 'cron'
if you are putting single quotes on the field name it will show error, don't get confused in single quotes and backticks they are completely different.

Azure MySQL Error

I am trying to install http://prosper202.com (self hosted script on Azure )
Here is what i have done.
Created a Azure Website with MySQL Database.
Configured Database login Credentials.
Now i am getting this 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 'AND 202_summary_overview.click_time < AND landing_page_id=0 ' at
line 12
SELECT 202_aff_campaigns.aff_campaign_id
, aff_campaign_name
, aff_campaign_payout
, aff_network_name
FROM 202_summary_overview
LEFT JOIN 202_aff_campaigns USING (aff_campaign_id)
LEFT JOIN 202_aff_networks USING (aff_network_id)
WHERE 202_aff_networks.user_id = '1'
AND 202_aff_networks.aff_network_deleted = 0
AND 202_aff_campaigns.aff_campaign_deleted = 0
AND 202_summary_overview.click_time >=
AND 202_summary_overview.click_time <
AND landing_page_id = 0
GROUP BY aff_campaign_id
ORDER BY 202_aff_networks.aff_network_name ASC
, 202_aff_campaigns.aff_campaign_name ASC
Warning: Division by zero in C:\DWASFiles\Sites\click\VirtualDirectory0\site\wwwroot\202-config\functions-tracking202.php on line 1048 SELECT * FROM 202_sort_keywords LEFT JOIN 202_keywords ON (202_sort_keywords.keyword_id = 202_keywords.keyword_id) WHERE 202_sort_keywords.user_id='1' ORDER BY 202_sort_keywords.sort_keyword_clicks DESC LIMIT
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
I have no clue to fix this problem.
Can i use Azure SQL in place os MySQL , without any modification in script?
Further http://click.azurewebsites.net/tracking202/setup/aff_campaigns.php
Gives me these Errors
INSERT INTO `202_aff_campaigns` SET`aff_network_id`='1', `user_id`='1', `aff_campaign_name`='eDates', `aff_campaign_url`='http://googl.com', `aff_campaign_url_2`='', `aff_campaign_url_3`='', `aff_campaign_url_4`='', `aff_campaign_url_5`='', `aff_campaign_rotate`='0', `aff_campaign_payout`='0.9', `aff_campaign_cloaking`='1', `aff_campaign_time`='1355885344'
Field 'aff_campaign_id_public' doesn't have a default value
This same script hosted here http://prosper202.com/ is working of thousands on server. But its not working on Azure MySQL.
Some more details
http://i.stack.imgur.com/SfhPs.png
you have no value supplied on which the column will be compare to,
SELECT ....
FROM ....
WHERE ....
AND 202_aff_campaigns.aff_campaign_deleted = 0
AND 202_summary_overview.click_time >= // << error on this line
AND 202_summary_overview.click_time < // << also here
AND landing_page_id = 0
You need to add a record into user_pref for the new user...so i just copied the first user record into second and just update the user_id accordingly.
CREATE TEMPORARY TABLE tmp SELECT * FROM 202_users_pref WHERE user_id = 1;
UPDATE tmp SET user_id=2 WHERE user_id = 1;
INSERT INTO 202_users_pref SELECT * FROM tmp WHERE user_id = 2;
DROP TABLE tmp;
Hope this helps someone

PHP/MySQL Concat to a single column and Update other columns in table

Am trying to only concat new updates to column updates and UPDATE the values in the rest of the columns but I've hit bit of a snag that I can't seem to workout.
My SQL looks like this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";
the updates column is like a comments column, where new updates are meant to be appended to the existing text.
The error I'm getting on the web server:
Update tickets SET product='T-Box', p='00000817766', i='-', summary='Testing update field
\r\nAdding an update\r\ntesting if null works for update', workaround='n/a', concat(updates,' ','test2#18:53:17:second update/n'), status='Open', raised_by='No', updated_by_user='test2' WHERE id='223'
Running the query directly in MySQL:
#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 '(updates,'test2#18:53:17:second update/n'), status='Open', raised_by='No', updat' at line 1
Help is much appreciated!
You need to specify where the value of this statement concat(updates,'$additional_update') to be set.
Update tickets
SET product = '$product',
p = '$p',
i = '$i',
summary = '$summary',
workaround = '$workaround',
updates = CONCAT(updates,'$additional_update'), // <== see this
status = '$status',
raised_by = '$raised_by',
updated_by_user = '$updated_by'
WHERE id = '$id'
try this:
$query="Update tickets SET product='$product',
p='$p',
i='$i',
summary='$summary',
workaround='$workaround',
updates=concat(updates,'$additional_update'),
status='$status',
raised_by='$raised_by',
updated_by_user='$updated_by' WHERE id='$id'";

Help resolving SQL error that occurs in code but not in SQL workbench

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'

Categories