Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KTSH · Mar 05, 2017 at 09:58 AM · phpsqlnestedquerystatements

SQL Query SET Statement issue with nested Statements in PHP

The Details

I have a table in a database. In the table, there are the following columns:

  • id

  • name

  • score

...

What I am trying to do

To try out nested query statements in parentheses

My Statement
 $sth = $dbh->query
 ("
     UPDATE scores
     SET name='$name', score='$score'
     WHERE id = (SELECT id FROM scores ORDER BY id ASC LIMIT 1);
 ");
Expected Result

The "name" and "score" of row with "id" 1 is changed to "$name" and "$score" respectively

Actual Result

The "name" and "score" were not changed

additional information 1

To make sure that the statement within the parentheses is working fine, I applied it to an isolated SELECT statement:

 $echo = $dbh->query
 ("
     SELECT *
     FROM scores
     WHERE id = (SELECT id FROM scores ORDER BY id ASC LIMIT 1);
 ");

The statement above worked exactly as expected... that is... to SELECT the whole row with the id 1.

additional information 2

To make sure that the statement outside of the parentheses is working fine, I simplified the WHERE condition:

 $sth = $dbh->query
 ("
     UPDATE scores
     SET name='$name', score='$score'
     WHERE id =1;
 ");

The statement worked exactly as expected... that is... to change the name and score of row with id 1.

additional information 3

Out of desperation, I also tried to split the PDO::query class into PDO::prepare and PDO::execute classes.

 $sth = $dbh->prepare
 ("
     UPDATE scores
     SET name = '$name', score='$score'
     WHERE id = (SELECT id FROM scores ORDER BY id ASC LIMIT 1);
 ");

and

 $sth->execute($_GET);

And as expected, it did not work.

Summary

It seemed like the (SELECT id FROM scores ORDER BY id ASC LIMIT 1) nested statement does not work in a statement with UPDATE SET only.

I have no idea what went wrong. Please provide advice. If there is any information required, do let me know.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jwulf · Mar 06, 2017 at 11:19 AM 0
Share

I'm genuinely curious: How did you get to post this question here? :D Are you running a PHP script on the server side of an online unity game or something?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jamb84 · Mar 07, 2017 at 04:25 PM

You haven't mentioned which database provider you are using (MySQL etc) but in the past when I have attempted this sort of query I receive a syntax error telling me that you cannot update a table that is being queried at the same time. So in this case because you are performing a select on scores you wouldn't be able to update it.

This may not be the case here depending on the provider and version etc.

In MySQL a workaround for this would be hiding the subselect in another subselect.

 UPDATE scores
      SET name='$name', score='$score'
      WHERE id = (SELECT id FROM (SELECT id FROM scores ORDER BY id ASC LIMIT 1) as temp1);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Mar 06, 2017 at 06:07 AM

This question is actually off-topic. Nothing about that question is about Unity.

Anyways, MySQL can not update a table that you currently read from. See this SO question. If you run that query in an actual SQL client you should actually get that error.

The easiest fix is to execute two seperate queries, one after the other, So first do your "id" select, store it in a variable and then do your update query.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My code to php -> sql in PC good, in Android not work. plese fast 1 Answer

Unity PHP/SQL table creation adds strange characters to table name 2 Answers

Why is my PHP echo returning entire PHP file after WWW request? 1 Answer

Getting Specific mySql Row by searching table with a name 0 Answers

Scoreboard/Leaderboard Script for both server and client? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges