0

Hi i am bit confused on how to fetch my data from database because i have 2 tables namely

credentials and info

Now I am using the credential table for the login purpose only but the username is I used for getting the information from table info like this

$user_id = $_SESSION['user_session'];
$stmt = $db_con->prepare("SELECT * FROM credentials WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);

$bigoid = $userRow['username'];

$sql = "SELECT * FROM member WHERE bigoid=:bigoid";
$query= $db_con->prepare($sql);
$query->execute(array(':bigoid' => $bigoid));

Now the problem is that this while loop

while($row=$query->fetch(PDO::FETCH_ASSOC))
{
        $bigoname = $row['bigoname'];
        $bigoid = $row['bigoid'];
        $name = $row['name'];
        $address = $row['address'];
        $cellphonenumber = $row['cellphonenumber'];
        $emailaddress = $row['emailaddress'];
        $facebooklink = $row['facebooklink'];
        $content = $row['content'];
        $agency = $row['agency'];
        $code = $row['code'];
        
}

is not getting that informations it just says

Undefined variable

all of them those who are inside the clause of the while loop

#EDIT

when i am trying to echo them on my html like this

<?php
      echo $bigoname;
 ?>

it says undefined . Also the other variables like address,email etc.

  • Which line exactly gives You "undefined variable"? What is the undefined variable's name? – Roman Hocke Aug 03 '20 at 18:36
  • 1
    Please provide the full error message. It's mentioning undefined variable, not undefined index, so it's not complaining about the results from credentials. – aynber Aug 03 '20 at 18:36
  • 1
    I edited my question @RomanHocke –  Aug 03 '20 at 18:39
  • 1
    I edit my question sir @aynber –  Aug 03 '20 at 18:39
  • The variables are only defined once you enter the while loop. If there are no results, then the variables do not exist. – aynber Aug 03 '20 at 18:42
  • @aynber i have a bit similar process on the other page sir and it works as getting those variables but my problem is that i am querying from one table and comparing a row there to get the data from another table –  Aug 03 '20 at 18:51
  • You're getting that error because there are no results from the query. You can get around it by pre-defining the variables before the loop. Set them to null, empty strings, or something like "not defined", and you'll make sure the variables exist no matter what. (Side note, not everyone on the internet or even SO is a sir) – aynber Aug 03 '20 at 18:55
  • so i did what you told i set those variables to an empty string and make the while loop and var_dump() those variable and it just gives me empty also so it means that I am not getting my query –  Aug 03 '20 at 19:00
  • So now you need to figure out why you're not getting any results from your query. I don't know how your tables and data are set up, but you're getting username from your credentials table, then trying to use that to query the bigoid column in your members table. Does credentials.username = members.bigoid, or are they linked on another column? – aynber Aug 03 '20 at 19:21
  • done :) thank you @aynber –  Aug 03 '20 at 20:20

0 Answers0