Due to many limitations, I'm only able to use vanilla PHP. The current API is plain and simple, it has two tables, one is for stats, the other includes the records.
First, stats table will be fetched:
$tempStatsName = $this->getRandomIndexString();
$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS " . $tempStatsName . " AS SELECT COUNT(*) AS total_visits, series_id FROM my_stats_table d2 WHERE d2.stats_type = 1 GROUP BY series_id;";
$this->conn->exec($sql);
Then, an index will be added
$sql = "ALTER TABLE `" . $tempStatsName . "` ADD INDEX `" . $this->getRandomIndexString() . "` (`series_id`);";
$this->conn->exec($sql);
getRandomIndexString is a function that generates a unique random string.
return substr(str_shuffle(MD5(microtime())), 0, 10)
Finally, the last query will be executed:
SELECT database_series.id, database_stats.total_visits,(SELECT COUNT(*) FROM myreviewstable d_reviews WHERE d_reviews.media_id = database_series.id AND d_reviews.review_media_type = 0) series_reviews_count, (SELECT TRUNCATE(AVG(d_reviews2.review_rating_value), 1) FROM myreviewstable d_reviews2 WHERE d_reviews2.media_id = database_series.id AND d_reviews2.review_media_type = 0) series_reviews_value, database_series.series_title, database_series.series_description, database_series.series_thumbnail, (SELECT COUNT(*) FROM myepisodestable d_episodes WHERE d_episodes.episode_series = database_series.id) series_episodes_count FROM myrecordstable database_series LEFT JOIN " . $tempStatsName . " database_stats ON (database_stats.series_id = database_series.id)
Exception:
Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 '189e087f9e0ce79e31b4 AS SELECT COUNT(*)
Another example:
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 '2332e22834 database_stats ON (database_stats.series_id = database_series.id) WHE'
This exception only happens when I request a call to the API 3x times in a row (in less than 2s due to lazy-scroll methods being called).
If I wait a few seconds and then try scrolling, the API will be called and no exception will be shown.
I'm unable to know why the exception happens, are there any duplicated temp tables? I'm pretty sure it's not an issue with the max_heap_size, the records are less than 100.
The important parts of the database are the stats & records tables, other tables are not important and didn't cause any issues.
my_stats_table:
id stats_type series_id created_at created_at_text
myrecordstable:
id series_title created_at
Basically, the three queries are executed in the same connection and are included in a function called getSeries(). It executes the first, which creates the temp table, after that alters the table, finally, it executes the code which fetches the records of myrecordstable.
Related
This question already has answers here:
INSERT with SELECT
(7 answers)
Closed 8 months ago.
When connecting to Postman, I am able to insert data correctly. Here is my query in postman:
{<br>
"Company2_ID_Company2": "120",<br>
"Company2_DHS6_ID": "144", <br>
"Company2_ID_Main": "153",<br>
"Retake_ID_Main": "120", <br>
"Retake_ID": "120", <br>
"Retake1_ID": "120",<br>
"Retake1_ID_Retake1": "120",<br>
"Pass": "Yes"<br>
}
However, the error is
<br />
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[42000]:
Syntax error or access violation: 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
<b>'Company2_ID_Company2 = '120', Company2_DHS6_ID = '144', Company2_ID_Main = '153''</b>
at line 17 in C:\XAMPP\htdocs\API_Demo2\Models\Post.php:212
Stack trace:
#0 C:\XAMPP\htdocs\API_Demo2\Models\Post.php(212): PDOStatement->execute()
#1 C:\XAMPP\htdocs\API_Demo2\Post\Create.php(43): Post->create()
#2 {main}
thrown in <b>C:\XAMPP\htdocs\API_Demo2\Models\Post.php</b> on line <b>212</b><br />
Whats strange is that whenever I change the numbers in the query, they show up correctly in the error.
Here is my PHP code with the entire query:
$query = 'SELECT
sandbox.company2.Company2_ID_Company2 AS Company2_ID_Company2,
sandbox.company2.Company_DHS6_ID AS Company2_DHS6_ID,
sandbox.main.Retake_ID_Main AS Retake_ID_Main,
sandbox.main.Company2_ID_Main AS Company2_ID_Main,
sandbox.retake.Retake_ID AS Retake_ID,
sandbox.retake.Retake1_ID AS Retake1_ID,
sandbox.retake1.Retake1_ID_Retake1 AS Retake1_ID_Retake1,
sandbox.retake1.Pass AS Pass
FROM ' . $this->table . ' company2
JOIN main ON sandbox.Company2.Company2_ID_Company2 = sandbox.main.Company2_ID_Main
JOIN retake ON sandbox.main.Retake_ID_Main = sandbox.retake.Retake_ID
JOIN retake1 ON sandbox.retake.Retake1_ID = sandbox.retake1.Retake1_ID_Retake1' .
'INSERT INTO ' . $this->table . '
SET
Company2_ID_Company2 = :Company2_ID_Company2,
Company2_DHS6_ID = :Company2_DHS6_ID,
Company2_ID_Main = :Company2_ID_Main,
Retake_ID_Main = :Retake_ID_Main,
Retake_ID = :Retake_ID,
Retake1_ID = :Retake1_ID,
Retake1_ID_Retake1 = :Retake1_ID_Retake1,
Pass = :Pass';
You should study the INSERT...SELECT syntax more carefully. If you use INSERT...SELECT, then you don't need a SET clause or a VALUES clause for your INSERT statement, nor do you need parameter placeholders.
I'd code it this way:
$query = <<<SQL_END
INSERT INTO `{$this->table}` (
Company2_ID_Company2,
Company2_DHS6_ID,
Company2_ID_Main,
Retake_ID_Main,
Retake_ID,
Retake1_ID,
Retake1_ID_Retake1,
Pass)
SELECT
sandbox.company2.Company2_ID_Company2 AS Company2_ID_Company2,
sandbox.company2.Company_DHS6_ID AS Company2_DHS6_ID,
sandbox.main.Company2_ID_Main AS Company2_ID_Main,
sandbox.main.Retake_ID_Main AS Retake_ID_Main,
sandbox.retake.Retake_ID AS Retake_ID,
sandbox.retake.Retake1_ID AS Retake1_ID,
sandbox.retake1.Retake1_ID_Retake1 AS Retake1_ID_Retake1,
sandbox.retake1.Pass AS Pass
FROM `{$this->table}` company2
JOIN main ON sandbox.Company2.Company2_ID_Company2 = sandbox.main.Company2_ID_Main
JOIN retake ON sandbox.main.Retake_ID_Main = sandbox.retake.Retake_ID
JOIN retake1 ON sandbox.retake.Retake1_ID = sandbox.retake1.Retake1_ID_Retake1
SQL_END;
The columns named in the parentheses after the table name must be in the same order as the columns returned by the select-list of the SELECT query.
I also make use of PHP heredoc syntax to format the string, and also variable expansion inside the heredoc. I also put the table name in back-ticks just in case the name conflicts with an SQL reserved keyword or contains special characters.
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 am an amateur programmer creating a PHP based online portal which will update values in a MySQL database in relation to a MMO-type game, in which we are using the portal to track a total number of land tiles protected by each user.
I am working on the script which will update the table count for a given type of protected land, upon submission of an HTML form through a $_POST array.
The MySQL table (players) in question has four similar fields (along with other fields):
wild_count
city_count
nether_count
end_count
On the HTML form, the user can select a land type when submitting, and the script attempts to perform a string concatenate to complete the field, then supplies this for the placeholder in the prepared SQL query, as such:
//Set land type string
$landtype = $_POST['landtype'] . '_count';
//Process ADD request
if (!isset($_POST['negative']))
{
$action = 'ADDED'; //This is for a transaction report further down in the code
try
{
$sql = 'UPDATE players SET
`:landtype` = `:landtype` + :tiles WHERE id = :id';
$query = $link->prepare($sql);
$query->bindValue(':landtype', $landtype);
$query->bindValue(':tiles', $_POST['tiles']);
$query->bindValue(':id', $_POST['player']);
$query->execute();
}
catch (PDOException $e)
{
$error = 'Error updating land count: ' . $e->getMessage();
include './includes/error.inc.php';
exit();
}
...more code follows...
When trying to POST my form using the following code, I get the following error:
Error updating land count: SQLSTATE[42S22]: Column not found: 1054 Unknown column ''city_count'' in 'field list'
(I had selected city in my form example).
I've tried the same code, except without the backticks around the placeholder :landtype (i.e. $sql = 'UPDATE players SET :landtype = :landtype + :tiles WHERE id = :id';) and I get a different error:
Error updating land count: SQLSTATE[42000]: Syntax error or access violation: 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 ''city_count' = 'city_count' + '300' WHERE id = '1'' at line 2
I'm not certain how to proceed. Does the attempt at setting the field value by creating a concatenated string break it here?
Don't try to bind column name like it's a value:
$sql = 'UPDATE players SET `'.$landtype.'` = `'.$landtype.'` + :tiles WHERE id = :id';
Can PHP PDO Statements accept the table or column name as parameter?
public function delete($group_id)
{
$this->db->where('t1.*, t2.*, t3.*, t4.*, t6.*, COUNT(t5.id) AS total_students')
->join('groups_days AS t6', 't1.group_id = t6.group_id')
->join('groups_members AS t5', 't1.group_id = t5.group_id')
->group_by('t1.group_id')
->delete("groups AS t1")
->result();
}
Error Number: 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 'AS `t1` WHERE t1., t2., t3., t4., t6.*, COUNT(t5.id) AS total_students' at line 1
DELETE FROM `groups` AS `t1` WHERE t1., t2., t3., t4., t6.*, COUNT(t5.id) AS total_students
Filename: D:/www/domains/uzdev/taraqqiyot/application/models/Group_model.php
My delete not working and showing above error. What is wrong here
According to this link you can't do delete with join with CodeIgniter Active Records:
link
Another option is using several queries as many as number of tables:
$this->db->delete('groups', array('group_id'=>$group_id));
$this->db->delete('groups_days', array('group_id'=>$group_id));
$this->db->delete('groups_members', array('group_id'=>$group_id));
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