Problems with choosing PHP / MYSQL by first key

So, I have a primary key column called key. I am trying to select a line with key = 1 through this code:

$query ="SELECT * FROM Bowlers WHERE key = '1'"; $result = mysql_query($query) or die(mysql_error()); 

For some reason, I get this result:

You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use next to 'key =' 1 '' on line 1

The mysql statement works to use other keys, i.e. WHERE name = 'djs22'.

Any ideas?

+4
source share
5 answers
Key

is a reserved word, try tagging it around:

 $query ="SELECT * FROM `Bowlers` WHERE `key` = '1'"; $result = mysql_query($query) or die(mysql_error()); 

To view all reserved words, go here and scroll down: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

+7
source

'key' is a reserved keyword, put quotation marks there:

 "SELECT * FROM Bowlers WHERE `key` = '1'" 
+2
source

Without verification, it is likely that the β€œkey” is a reserved word in MySQL. Try to wrap it in backticks

 $query ="SELECT * FROM Bowlers WHERE `key` = '1'"; 
+2
source

You must write the column name in quotation marks

 $query ="SELECT * FROM Bowlers WHERE `key` = '1'"; 

Otherwise, this is a keyword.

+1
source

I run into this all the time. MySQL has crappy loading of reserved words. And when you come across it, the mysql error function is not good enough for you to know what is wrong.

The only thing you can do is change the column name. I accidentally used the "date, time and back" the other day. He pulled my hair when it dawned on me, DuHH !!! These are reserved databases.

You can wrap all quotation marks around it; it doesn't matter when it refers to the column name. Reserved Reserved!

It is usually customary to do a couple of things.

1) When creating tables: Separate the resource type with the resource name using the underscore. Example: xref_userMessages

 This would mean it is a cross reference table for User messages. 

2) Other examples of table names:

  msg_Messages |  sys_Settings |  cli_Logins 
Therefore, any other table associated with messages will be called msg _ ???, this not only keeps them in the phpMyadmin group, but also makes it easy to remember names.

3) When creating columns: never use reserved. Thus, there should always be 6 didgets for key columns. Example:

  admkey |  usrkey |  msgkey |  clikey grpkey 
 Obviously Admin Key | User Key | Message Key | Client Key | Group Key 

So this means that the msg_Messages keys are "msgkey", and the xref table is xref_Messages, and its keys are xref_msgkey. Following this logic, you not only know what to call everything without even thinking about it, but you never come across any reserved words that do this.

4) Examples of column names:

  dateInsert dateStart timeCreate admName admAddress admPhone admCell 

As above, there is logic. Placing a target / owner and a noun / element together makes a name and again allows you to reserve words.

Last example:

Table: users_Admins users_Clients Key: admkey usrkey

Table: msg_Messages Columns: msgkey admkey usrkey msgRead msgMessage msgTitle

In this short example, I avoided 2 reserved words. Key and reading

In short, your problem is not reading the primary key. This is a problem with column names. MySQL sees that your code has syntax that has commands inappropriate. SELECT read ... or SELECT ... it doesn’t matter if you put quotation marks around it or not. MySQL basically sees ...

  SELECT (SELECT, WHERE, FROM) FROM select, from, where 
 WHERE SELECT = WHERE & FROM = SELECT.  hehehehehehehe 

Putting another quote around this will not change the level of confusion that you just sent to MySQL.

Mixing my mistake and your mistake together looks like this ...

 SELECT key, from, to, date FROM my_table WHERE key = '1';

 // Same as ...
 SELECT SELECT, SELECT, SELECT, SELECT FROM my_table WHERE SELECT = '1';

The first one you cannot say when looking at him is something wrong with him. Secondly, it is obvious that this is wrong and will not work. However, according to MySQL, they are SO.

MySQL gets this syntax so ... SELECT? You told me SELECT 5 times, you never told me what to choose. You get the FROM right, but then you ended up with a left hook telling you to choose something else, not only did you not tell me what to choose again, but you chose NULL = '1'; what it is? That is why when you make such errors, the error function does not even report what happened. There were so many errors that he cannot give you the error number, so he just stops.

  So this means your syntax is like this
 SELECT * FROM Bowlers WHERE SELECT = '1'; 

Sometimes I get upset and say: "I would like MySQL to be smarter than that!" But then I understand that I will have to trade keywords for a less important database. Each of these reserved words represents a word that does a lot more work with the database for me. When I started learning programming, I had to write my own text input routines, so I appreciate everything that MySQL does for me.

0
source

Source: https://habr.com/ru/post/1311184/


All Articles