Blank consultation

Asked

Viewed 53 times

1

I’m starting in the world of programming and at first I have a problem when I run the query locally the query shows me the return normally but when I try to query through a simple application:

<?php
/**
 * 
 * @var string $dsn  : driver de conexão : endereco do servidor : nome do banco
 * @var string $user : usuario do banco
 * @var string $pass : senha do usuario 
 */
$dsn = 'odbc:driver={SQL Server};server=IP_DO_SERVIDOR;database=TESTE';
$user = 'xxxxxxx';
$pass = 'xxxxxxx';

try 
{
    $pdo = new PDO($dsn,$user,$pass);
}
catch(PDOException $exeption)
{
   die("Erro: $exeption");
}

// Definindo o mode de erros da classe
//$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$select = "SELECT * FROM FIN_TITULO WHERE EMPRESA = 1 AND REVENDA = 1 AND TITULO = 435376";

$result = $pdo->exec($select);

var_dump($result);

the var_dump returns the value 0, that is, no affected line, I do not know what to do, it would perhaps be the user’s permission because the direct query through SQL management works normally?

1 answer

0

Try it this way:

$result = $pdo->query($select);

or

$select = "SELECT * FROM FIN_TITULO WHERE EMPRESA = 1 AND REVENDA = 1 AND TITULO = 435376";
$statement = $pdo->prepare($select);
$statement->execute();

// Verifica e gera array assoc
if($statement->rowCount()) {
    $stt = $statement->fetch(PDO::FETCH_ASSOC);
    print_r($stt);
}

Opa worked, just did not understand why the result of my consultation initial had no return - Guilherme Arantes

When using PDO::EXEC the returned result is not a PDOStatement, and yes an entire of the affected lines.

When using PDO::QUERY the returned result is a PDOStatement.

Sources: 1 2

  • my problem is not related to connection but with query the return displayed in var_dump is still 'int(0)' should be 'int(1)'

  • @Guillermos updated. Sorry for the confusion

  • Opa worked, just did not understand why the result of my initial consultation had no return

  • You can try to check like this: $db->exec($select) or die(print_r($db->errorInfo(), true));, dessa is will display the error for you to debug. You can also use the $db->query($select).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.