1
<?php
require("config.inc.php");

$query = "Select 1 FROM dogs WHERE dog_id = :iddog ";

$query_params = array(':iddog'=> $_POST['iddogPHP2']);

$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);
$dono2 = $result ->fetchColumn(1);
$raca2 = $result ->fetchColumn(2); 
$sex2 = $result ->fetchColumn(3);
$estado2 = $result ->fetchColumn(4);

$query = "Select * FROM dogs WHERE dispon = 'Sim' AND dono != '$dono2' AND estado = '$estado2' AND raca = '$raca2' AND sex != '$sexo2' ";

try
{
    $stmt   = $db->prepare($query);
    $result = $stmt->execute();
}
catch (PDOException $ex)
{
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();

?>

This is my code and the problem is that the variables dono2, raca2, gender2 are not getting the values from the database. What's wrong in it?

3
  • print query result and check Commented Nov 17, 2015 at 4:48
  • From: Can not print I am working on android with a Communication THAT php Commented Nov 17, 2015 at 4:50
  • use postman(Google chrome extension) to make a Rest API call for testing Commented Nov 17, 2015 at 4:54

3 Answers 3

1

Instead of SELECT 1 you need to use SELECT * in your first query.

$query = "Select * FROM dogs WHERE dog_id = :iddog ";

The following returns an array of results

$rows = $stmt->fetchAll();

So you would need to specify which one to reference.

$dono2 = $rows[0]['dono'];
$raca2 = $rows[0]['raca']; 
$sexo2 = $rows[0]['sex'];
$estado2 = $rows[0]['estado'];
Sign up to request clarification or add additional context in comments.

3 Comments

Or change fetchAll to fetch. Looks nicer than adding the 0 index everywhere.
And rather than doing SELECT * get the columns you actually need
WORKS, really save my life haha
0
 $rows = $stmt->fetchAll();

fetch all rows from database you need to specify which row you want if it's multiple row

                $dono2 = $rows[0]['dono'];
                $raca2 = $rows[0]['raca']; 
                $sexo2 = $rows[0]['sex'];
                $estado2 = $rows[0]['estado'];

Comments

0

Try this code

 $stmt = $db->prepare($query);
 $stmt->bindParam(':iddog', $_POST['iddogPHP2']);
 $stmt->execute($query_params);

 $dono2 = $stmt ->fetchColumn(1);
 $raca2 = $stmt ->fetchColumn(2); 
 $sex2 = $stmt ->fetchColumn(3); 
 $estado2 = $stmt ->fetchColumn(4);

Comments

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.