【发布时间】:2012-11-22 13:30:28
【问题描述】:
我正在尝试将二维数组放入数据库。我的代码如下(这是PHP):
public function addspot($linearray,$data){
$dbname=$data['dbname'];
try {
/* Create a connections with the supplied values */
$pdo = new PDO("mysql:host=" . Config::read('hostname') . ";dbname=" . Config::read('database'). "", Config::read('username'), Config::read('password'), array(PDO::ATTR_PERSISTENT => true));
} catch(PDOException $e) {
/* If any errors echo the out and kill the script */
return 'Database conncetion fail in assets/garage.class.php!Make sure your database information is correct';
}
foreach ($linearray as $lines) {
$spot="INSERT INTO `$dbname`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('$lines[0]', '$lines[1]', '$lines[2]', '$lines[3]', '$lines[4]', CURRENT_TIMESTAMP);";
$statement = $pdo->prepare($spot);
if($statement->execute()){
//silent
} else {
return 'Spot not added!';
}
}
}
配置值被正确读取,添加点的语句也是正确的。 我知道这一点是因为当我运行该函数时,它会正确添加 1 个“点”,但不会添加 2D 数组中的其余行。
我的数组如下:
array (size=16)
0 =>
array (size=5)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '0' (length=1)
4 => string '1' (length=1)
1 =>
array (size=5)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '1' (length=1)
3 => string '0' (length=1)
4 => string '1' (length=1)
(and onwards)
我的问题是我编写的函数只将第一行 (line[0]) 写入数据库,而其他的没有写入。
更新 语句的输出(使用 print_r): 准备后放置
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '1', '1', '0', '1', CURRENT_TIMESTAMP);
)
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '2', '1', '0', '1', CURRENT_TIMESTAMP);
)
print_r($pdo->errorInfo());输出 放在execute语句的else(fail)部分
Array
(
[0] => 00000
[1] =>
[2] =>
)
【问题讨论】:
-
font, small, b, i ?? 90 年代被调用并希望它的 html 回来 - 请学习 css 和现代 html
-
@Dagon Thats XDebug...对于那些在 21 世纪使用 PHP 的人:P 我会删除它的格式
-
不使用绑定参数时,为什么要使用
prepare?这也将摆脱正在发生的多个不需要的准备(虽然不是自动的)。注意PDO不是可以用来替换mysql_*代码的魔法。 -
@PeeHaa 如果没有,我会调用非对象上的成员函数 execute()
-
@PeeHaa 我不使用 mysql。从来没有。它已被弃用,所以我什至从未理会它。