带数组的FETCH num行

时间:2016-01-04 06:47:30

标签: php mysql mysqli pdo

如果电子邮件和密码相同,我使用pdo来获取数据,但它给我一个错误,而不是在下面提取数据是代码.........

if(isset($_POST['submit'])){
  $email      = $_POST['email'];
  $password   = $_POST['password'];
  //Encrypt Password
  $encrypted  = md5($password);

  //Include Database Connection
  require 'assets/_conn.php';
  $result = dbConnect()->prepare("SELECT email, password FROM users WHERE email= :email AND password= :password");
  $result->bindParam(':email', $email);
  $result->bindParam(':password', $encrypted);
  $result->execute();

以下是未获取的问题数据....

$rows = $result->fetch(PDO::FETCH_NUM);
if(count($rows) > 0) {
    foreach($rows as $row){
       $image  = $row['image'];
       $pic    = array('1.jpg',$image);
    }
}

2 个答案:

答案 0 :(得分:2)

<强> PDO::FETCH_NUM

  

返回由结果中返回的列号索引的数组   设置,从第0列开始

因此,不使用列名使用索引

spring-context

您需要添加$image = $row[2];// your indexing 。您的查询将是

image column in your query

答案 1 :(得分:1)

用于获取您应该用于$result->fetch(PDO::FETCH_OBJ)的数据 如下所示

并且提到@Saty请在您的查询中添加图片列。

$rows = $result->fetch(PDO::FETCH_NUM);
$pic = array();
if(count($rows) > 0) {
    /* foreach($rows as $row){
       $image  = $row['image'];
       $pic  = array('1.jpg',$image);
    } */
    while($row=$result->fetch(PDO::FETCH_OBJ)) {
        /*its getting data in line.And its an object*/
        $image  = $row->image;
        $pic[]    = array('1.jpg',$image);
    }
}

我希望这对你有用。

由于