1

I am using pdo to fetch data if email and password is same but it giving me an error and not fetching data below is the code .........

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();

Here is the problem data in not fetching ....

$rows = $result->fetch(PDO::FETCH_NUM);
if(count($rows) > 0) {
    foreach($rows as $row){
       $image  = $row['image'];
       $pic    = array('1.jpg',$image);
    }
}
2
  • it giving me an error What error? Commented Jan 4, 2016 at 6:55
  • Undefined variable: query Commented Jan 4, 2016 at 6:58

2 Answers 2

2

PDO::FETCH_NUM

returns an array indexed by column number as returned in your result set, starting at column 0

So instead of using column name use indexing

$image  = $row[2];// your indexing

And you need to add image column in your query . Your query would be

$result = dbConnect()->prepare("SELECT email, password, image FROM users WHERE email= :email AND password= :password");
Sign up to request clarification or add additional context in comments.

1 Comment

You need to add image column in your query
1

for fetching data you should used to $result->fetch(PDO::FETCH_OBJ) like below

and as mention @Saty please add image column into your query.

$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);
    }
}

i hope this is working for you.

Thanks

4 Comments

thanks reniskhunt but its not working it not fetching data error coming is Undefined variable: pic
it's not possible men just check your code. are you used $pic variable outside of while loop?
@StackOverflow if you used $pic variable outside f while loop then you should define the variable before while loop.
CHeck this code .... $rows = $result->fetch(PDO::FETCH_NUM); if(count($rows) > 0) { while($row=$result->fetch(PDO::FETCH_OBJ)) { $image = $row->image; $pic = array('1.jpg',$image); } } its in loop and print this $pic outside the loop <?php for($i=0;$i<2;$i++) echo"<option data-img-src=\"$pic[$i]\" value=\"$pic[$i]\" ></option>"; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.