我不知道我的阵列中有什么意思。
我的代码是
try {
$conn=new PDO("mysql:host=localhost;dbname=moviesite","root","");
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("SELECT movie_name,movie_year,movie_type,movie_leadactor,movie_director
FROM movie
ORDER BY movie_name,movie_year");
$stmt->execute();
$result=$stmt->fetchAll();
foreach ($result as $row) {
var_dump($row);
}
} catch (PDOException $e) {
echo $e->getmessage();
}
**And my result**
我的问题是
0 => string 'Bruce Almighty' (length=14)
1 => string '2003' (length=4)
.
.
.
我只是要求movie_name
,movie_year
,movie_type
,movie_leadactor
,movie_director
但它显示了额外的行,如0 =>字符串'Bruce Almighty'(长度= 14)
答案 0 :(得分:2)
fetchAll()
的第一个参数是fetch_style
按照中的说明控制返回数组的内容 PDOStatement对象:: fetch()方法。默认值为 PDO :: ATTR_DEFAULT_FETCH_MODE(默认为PDO :: FETCH_BOTH)
这就是为什么当您var_dump($row);
时,您的数组中同时包含numeric
和associative
格式数据。您只需要在$stmt->setFetchMode(PDO::FETCH_ASSOC);
之前设置$stmt->execute();
或者使用$result = $stmt->fetchAll(PDO::FETCH_ASSOC)
作为关联索引。
try {
$conn = new PDO("mysql:host=localhost;dbname=moviesite","root","");
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT movie_name,movie_year,movie_type,movie_leadactor,movie_director
FROM movie
ORDER BY movie_name,movie_year");
//ADD BELOW LINE
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
//OR REPLACE BELOW LINE LIKE $result=$stmt->fetchAll(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
foreach ($result as $row) {
var_dump($row);
}
} catch (PDOException $e) {
echo $e->getmessage();
}
答案 1 :(得分:0)
转储为您提供有关数组中每个变量的信息。因此,字符的类型和数量。如果您只想输出它们。
foreach ($result as $row) {
echo $row['movie_name'].','. $row['movie_year'].','.$row['movie_type'].','.$row['movie_leadactor'].','.$row['movie_director'].'<br />';
}
答案 2 :(得分:-1)
更改显示儿童的限制
; with sane limits
xdebug.var_display_max_depth = 16
xdebug.var_display_max_children = 400
xdebug.var_display_max_data = 2048
; with no limits
; (maximum nesting is 1023)
xdebug.var_display_max_depth = -1
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1
也可以在编码方面完成以下
ini_set('xdebug.var_display_max_depth', 16);
ini_set('xdebug.var_display_max_children', 400);
ini_set('xdebug.var_display_max_data', 2048);