1

I dunno how to describe the problem. I'm trying to fetch and I tried the query in PHPmyAdmin it is good it returns the result but when I transfer it to PHP.

The error is undefined variable.

Here is my code. Kindly help me please I'm stuck here for 2 hours thank you!

$targetint_id = $_GET["int_id"]; 
$targetbrch_id = $_GET["brch_id"];
$targetcst_id = $_GET["cst_id"]; 
$targetcbrch_id = $_GET["cbrch_id"]; 
$targetdateadded = $_GET["dateadded"]; 
$targettimeadded = $_GET["timeadded"]; 

$stmt = $db->prepare("SELECT * FROM tbl_soldinventory where int_id= :int and brch_id= :brch and cst_id= :cst and cbrch_id= :cbrch and date= :date and timeadded= :time");
$stmt->execute(array(':int' => "$targetint_id", ':brch' => "$targetbrch_id",':cst' => "$targetcst_id", ':cbrch' => "$targetcbrch_id",':date' => "$targetdateadded", ':time' => "$targettimeadded"));

while($rows = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $dateadded = $rows["date"];
    $timeadded = $rows["timeadded"];
    $quantity = $rows["quantity"];
}
echo $dateadded;
Al.G.
  • 4,327
  • 6
  • 31
  • 56
Bladeism
  • 21
  • 5
  • I can't see any error that affects the query :/ – Bladeism May 11 '14 at 17:24
  • The error is clear - there are undefined variables. – Al.G. May 11 '14 at 17:25
  • Which variable is reported undefined? Could it be that it's because you don't have column named `quantity` in the result? – hakre May 11 '14 at 17:25
  • the dateadded, timeadded and quantity I tried to query it in phpmyadmin it returns a result but when I tried it to use in php nothing comesout – Bladeism May 11 '14 at 17:26
  • @user3626061: That happens when there is no result. You need to initialize those variables first for such a case. That's necessary for every loop you write: Check pre- and post-conditions for every loop. – hakre May 11 '14 at 17:27
  • You should **always** check if the `$_GET[...]` or `$_POST[...]` are defined! Use `if(isset($_GET['variable'])) $variable = $_GET['variable']` – Al.G. May 11 '14 at 17:28
  • there are no problems in the $_GET sir – Bladeism May 11 '14 at 17:31
  • @hakre I noticed that I initialized the value before the query it doesn't execute query. Because when I echo it it returns null – Bladeism May 11 '14 at 17:33
  • @user3626061: because uninitialized variables give warning and then return NULL. Perhaps that? – hakre May 11 '14 at 17:36
  • @hakre Yes sir if I remove the initialized value before the query it will return undefined variables again. – Bladeism May 11 '14 at 17:38

1 Answers1

0

Your code misses error handling when you execute the query. I added some exemplary here, I assume as you report the warning and NULL values that the query just does not return any rows.

Perhaps because it fails, so you need to check for that and handle such a case, e.g. by collecting the error information and showing the query to actually make debugging easier (don't make yourself blind):

$result = $stmt->execute(array(
    ':int'  => "$targetint_id",    ':brch'  => "$targetbrch_id", 
    ':cst'  => "$targetcst_id",    ':cbrch' => "$targetcbrch_id", 
    ':date' => "$targetdateadded", ':time'  => "$targettimeadded",
));

if (FALSE === $result) {
    var_dump($stmt->queryString);
    throw new RuntimeException(vsprintf(
        '[SQLSTATE %s] %d %s', $stmt->errorInfo()
    ));
}

Additionally please see this reference question:

Full code with that change:

$targetint_id    = $_GET["int_id"];
$targetbrch_id   = $_GET["brch_id"];
$targetcst_id    = $_GET["cst_id"];
$targetcbrch_id  = $_GET["cbrch_id"];
$targetdateadded = $_GET["dateadded"];
$targettimeadded = $_GET["timeadded"];

$stmt = $db->prepare(
    "SELECT *
     FROM tbl_soldinventory
     WHERE
       int_id= :int and brch_id= :brch and cst_id= :cst
       and cbrch_id= :cbrch and date= :date and timeadded= :time
    "
);
$result = $stmt->execute(array(
    ':int'  => "$targetint_id",    ':brch'  => "$targetbrch_id", 
    ':cst'  => "$targetcst_id",    ':cbrch' => "$targetcbrch_id", 
    ':date' => "$targetdateadded", ':time'  => "$targettimeadded",
));

if (FALSE === $result) {
    var_dump($stmt->queryString);
    throw new RuntimeException(vsprintf(
        '[SQLSTATE %s] %d %s', $stmt->errorInfo()
    ));
}

$dateadded = $timeadded = $quantity = NULL;

foreach ($stmt as $row)
{
    $dateadded = $row["date"];
    $timeadded = $row["timeadded"];
    $quantity  = $row["quantity"];
}

var_dump($dateadded, $timeadded, $quantity);
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Sir it returns NULL NULL NULL – Bladeism May 11 '14 at 17:43
  • 1
    Then also `var_dump($stmt->queryString);` at the end and try that query in your mysql admin. – hakre May 11 '14 at 17:44
  • 1
    Sir the query string returns "SELECT * FROM tbl_soldinventory WHERE int_id= :int and brch_id= :brch and cst_id= :cst and cbrch_id= :cbrch and date= :date and timeadded= :time" I query this to my phpmyadmin "SELECT * FROM tbl_soldinventory WHERE int_id= 2 and brch_id= 1 and cst_id= 10 and cbrch_id= 1 and date= '14-05-11' and timeadded= '7:57:24 pm'" It returns the result – Bladeism May 11 '14 at 17:47
  • I found the problem sir it is about the date in my query. If I remove this the query is successful but I need it – Bladeism May 11 '14 at 18:33
  • you are one step further because you now know why exactly it returns zero results. – hakre May 11 '14 at 19:08