English 中文(简体)
如何通过神秘的问询插入含有 com的插图?
原标题:How to insert a string which contains commas via a mysql query?
  • 时间:2012-05-18 20:38:34
  •  标签:
  • php
  • mysql
  • sql

I have a table with 3 columns : id, food_name, and category. For example, I have $food_name = "Amaranth grain, cooked" (there is a comma), and category = "Cereals".

I can t insert the values and have the following error:

Could not connect: 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  )
      VALUES ( Amaranth grain, cooked , )  at line 1

我要问:

$results1 = mysql_query("
  INSERT INTO " . $table . " (food_en, category)
  VALUES ( " . $food_name . " ,  " . $category . " )"
)or die(mysql_error());

我的猜测是,“阿马兰特谷、 co”中的ma正在造成一个问题,但我可以解决这个问题。 I Trial str_replace( , , , , , $food_name) and mysql_real_einski_string(. 我也许没有使用这些物品。

提前感谢。

www.un.org/spanish/ecosoc EDIT:我的确重复了“类别”,它并不完全无效或空洞(价值为Cereals/strong)。

当我印刷时,我没有使用我的频率,我得到:

INSERT INTO calories (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO carbohydrates (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO fats_and_fatty_acids (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO protein_and_amino_acids (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO vitamins (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO minerals (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO sterols (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )
      INSERT INTO other (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta )Could not connect: 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  )
      VALUES ( Amaranth grain, cooked , )  at line 1

(不同表格只是不同的表格,我希望插入一线)

问题回答

There is no problem in your queries because of comma. Since you are inserting records to different tables, you need to separate each query with a query-separator or query-delimiter.

INSERT INTO calories (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta ); <-- Notice semi colon at end of query

INSERT INTO carbohydrates (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta ); <-- Notice semi colon at end of query

INSERT INTO fats_and_fatty_acids (food_en, category)
      VALUES ( Amaranth grain, cooked ,  Cereal Grains and Pasta ); <-- Notice semi colon at end of query

例如,我把每次问询都与semi-colon分开,并在没有问题的情况下插入询问。

Refer Fiddle here:

只要在引文中(如你在座的单一引文中),你就可以在座标上 com。 实际错误可能与以下事实有关:美元类别似乎要么是空洞的,要么是无效的,因为它说:

VALUES ( Amaranth grain, cooked , )

确保你响应 que问,确保它完全按照你的期望行事。

你们可以使用“基地”的功能,增加冲突或我sql_real_e Jersey_string,在我sql中具有任何含义的每一种特性面前放下冲突。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签