I am having trouble storing array contents in mysqlI am using the code below
我使用下面的代码在mysqlI中存储数组内容时遇到问题
foreach($found_entry as $key => $sd) { echo "$key => $sd <br>";$insert_found_row = mysql_query("INSERT INTO myTable values ID = \"$sd[0]\" ,Folder = \"NULL\", Tsi = \"$sd[11]\", PT= \"NA\" ") or die(mysql_error()); echo "$sd";
If I echo the values I get
如果我回应我得到的价值观
0 => ST109281 =>2 => 2010-02-19 03:37:163 => \\fs1\4 =>5 =>6 =>7 =>8 => M19 =>10 =>11 => LOG1.TXT
3 个解决方案
#1
3
Your query syntax is wrong. Use SET
instead of VALUES
. Also, you probably don't need the foreach loop at all, as you are referencing both $sd[0]
and $sd[11]
. Try something like this:
您的查询语法错误。使用SET而不是VALUES。此外,您可能根本不需要foreach循环,因为您引用了$ sd [0]和$ sd [11]。尝试这样的事情:
$query = sprintf("INSERT INTO myTable SET ID = '%s', folder = NULL, Tsi = '%s', PT = 'NA'", mysql_real_escape_string($found_entry[0]), mysql_real_escape_string($found_entry[11]) );mysql_query($query) or die(mysql_error());
#2
0
If I use $sd[0], only the first character is getting displayed.
如果我使用$ sd [0],则只显示第一个字符。
$sd is the value (e.g. "ST10928") of your array, but you're treating the value as an array itself, so $sd[0] must return only the first element (= letter).
$ sd是数组的值(例如“ST10928”),但是您将该值视为数组本身,因此$ sd [0]必须仅返回第一个元素(= letter)。
If you're trying to insert values from your array into the database (instead of iterating through the array and slicing the values), maybe you should use $found_entry[0]
instead of $sd[0]
and $found_entry[11]
for $sd[11]
??
如果你试图将数组中的值插入数据库(而不是遍历数组并切片值),也许你应该使用$ found_entry [0]代替$ sd [0]和$ found_entry [11] $ sd [11] ??
#3
0
Adding data to MySQL with a PHP Array
Inserting data into MySQL
将数据插入MySQL
In your code you are performing an insert for every field.
在您的代码中,您正在为每个字段执行插入操作。
However, in most cases you know the field names before hand and so you can do a single INSERT with all of the fields.
但是,在大多数情况下,您事先知道字段名称,因此您可以对所有字段执行单个INSERT。
$sql = 'INSERT INTO tbl_name (field_a,field_b,field_c) VALUES('.$found_entry['field_a'].','.$found_entry['field_b'].','.$found_entry['field_c'].');';
This cuts down on the number of queries required and so makes your code run faster and use less resources.
这减少了所需的查询数量,从而使您的代码运行更快,并使用更少的资源。
Default values
You can pass NULL when a value is not known and the DB should insert a default value.
当值未知且DB应插入默认值时,您可以传递NULL。
#1
3
Your query syntax is wrong. Use SET
instead of VALUES
. Also, you probably don't need the foreach loop at all, as you are referencing both $sd[0]
and $sd[11]
. Try something like this:
您的查询语法错误。使用SET而不是VALUES。此外,您可能根本不需要foreach循环,因为您引用了$ sd [0]和$ sd [11]。尝试这样的事情:
$query = sprintf("INSERT INTO myTable SET ID = '%s', folder = NULL, Tsi = '%s', PT = 'NA'", mysql_real_escape_string($found_entry[0]), mysql_real_escape_string($found_entry[11]) );mysql_query($query) or die(mysql_error());
#2
0
If I use $sd[0], only the first character is getting displayed.
如果我使用$ sd [0],则只显示第一个字符。
$sd is the value (e.g. "ST10928") of your array, but you're treating the value as an array itself, so $sd[0] must return only the first element (= letter).
$ sd是数组的值(例如“ST10928”),但是您将该值视为数组本身,因此$ sd [0]必须仅返回第一个元素(= letter)。
If you're trying to insert values from your array into the database (instead of iterating through the array and slicing the values), maybe you should use $found_entry[0]
instead of $sd[0]
and $found_entry[11]
for $sd[11]
??
如果你试图将数组中的值插入数据库(而不是遍历数组并切片值),也许你应该使用$ found_entry [0]代替$ sd [0]和$ found_entry [11] $ sd [11] ??
#3
0
Adding data to MySQL with a PHP Array
Inserting data into MySQL
将数据插入MySQL
In your code you are performing an insert for every field.
在您的代码中,您正在为每个字段执行插入操作。
However, in most cases you know the field names before hand and so you can do a single INSERT with all of the fields.
但是,在大多数情况下,您事先知道字段名称,因此您可以对所有字段执行单个INSERT。
$sql = 'INSERT INTO tbl_name (field_a,field_b,field_c) VALUES('.$found_entry['field_a'].','.$found_entry['field_b'].','.$found_entry['field_c'].');';
This cuts down on the number of queries required and so makes your code run faster and use less resources.
这减少了所需的查询数量,从而使您的代码运行更快,并使用更少的资源。
Default values
You can pass NULL when a value is not known and the DB should insert a default value.
当值未知且DB应插入默认值时,您可以传递NULL。