2016-05-17 51 views
1

有一点问题与我的PHP代码数据..PHP的MySQL提取毛坯

$stmt = $db->prepare("SELECT * FROM mytable WHERE TheGroup = :SearchName ORDER BY TheTime DESC"); 
$stmt->bindParam(':SearchName', $request, PDO::PARAM_STR); 
$stmt->execute(); 
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); 

$count = count($result); 
for ($i = 0; $i < $count; $i++) { 
    $mTheAvatar = $result[$i]->TheAvatar; 
    $mTheDirection= $result[$i]->TheDirection; 
    $mTheGroup = $result[$i]->TheGroup; 
    $mTheMedia = $result[$i]->TheMedia; 
    $mTheMessage = $result[$i]->TheMessage; 
    $mTheSenderName= $result[$i]->TheSenderName; 
    $mTheThumbImage = $result[$i]->TheThumbImage; 
    $mTheTime = $result[$i]->TheTime; 
    $mTheMediaExtension = $result[$i]->TheMediaExtension; 

    echo "hello"; 
    echo $mTheAvatar; 
    echo " <- this is avatar"; 
} 

如果我做的var_dump()我看到被请求的数据没有问题。 如果我附和变量,它们是空白.. 我有三重检查,该表的列名是否正确..

的$ mTheAvater是在表中的PIC,如果给出了一个可能的线索,但其余的都是空白以及不知道什么是???

+0

你可以在这里包含'var_dump()'的输出吗? – Epodax

回答

3

你可以测试:

$mTheAvatar = $result[$i]['TheAvatar']; 

据我所知在FETCH_ASSOC它在上述结构中返回的数据。

+0

没错,'FETCH_ASSOC'返回一个关联数组 – Dale

+0

谢谢!就是这样.. – Migz

+0

不客气 –

1

你想读他们,如果他们的对象,但PDOStatement::fetchAll返回一个数组,所以你的代码应该是这样的:

for ($i = 0; $i < $count; $i++) { 
    $mTheAvatar = $result[$i]['TheAvatar']; 
    $mTheDirection= $result[$i]['TheDirection']; 
    . 
    . 
    . 
    . 
    echo "hello"; 
    echo $mTheAvatar; 
    echo " <- this is avatar"; 
} 

如果你要处理的对象,你应该使用PDOStatement::fetchObject

+0

谢谢,这是def [[...]]我失踪了 – Migz

0

这应该更好 - 1)它使用foreach; 2)unset(); 3)不同的结构

$stmt = $db->prepare("SELECT * FROM mytable WHERE TheGroup = :SearchName ORDER BY TheTime DESC"); 
$stmt->bindParam(':SearchName', $request, PDO::PARAM_STR); 
$stmt->execute(); 
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); 

if($results){ 

    foreach($results as $result_data) { 

    echo $result_data['TheAvatar']; 

    echo $result_data['TheDirection']; 

    //and so on 

    unset($result_data); 

    } 

} 
else{ 

    echo 'Empty'; 

}