Compare SQL result using PHP

0

I started a session and performed a SQL query

<?
    session_start();
    $_SESSION['modulo'] = "WebSocialSocial/";
    $_SESSION['root'] = substr(__FILE__, 0, strpos(__FILE__, substr($_SESSION["modulo"], 0,-1)));
    $_SESSION['linkroot'] = "http://".$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $_SESSION['modulo']));
    $_SESSION['comum'] = "WebSocialComum/";
    require_once $_SESSION['root'] . $_SESSION['comum'].'class/commonClass.php';
    $common = new commonClass();
    echo $common->incJquery();

    $sqlLogon = "SELECT uni_codigo, esp_codigo FROM logon WHERE id_login = '" . $_SESSION['usr_codigo'] . "' ORDER BY id DESC LIMIT 1";

    //die($sqlLogon);

    $queryLogon = pg_query($sqlLogon);
?>

If this is done, this is the result of the query: SELECT uni_codigo, esp_codigo FROM logon WHERE id_login = '649' ORDER BY id DESC LIMIT 1 , which returns the unit code and specialty. What matters to me is the unit code. Next I have an HTML that will be printed:

<html xmlns="http://www.w3.org/1999/xhtml" lang="pt" xml:lang="pt">
    <head>
        <link href='<?= $this->baseUrl('/public/css/paisagem-print.css') ?>' rel='stylesheet' type='text/css'></link>
        <script type="text/javascript">
            var baseUrl = '<?= $this->baseUrl(); ?>';
            $(function () {
                window.print();
            });
        </script>
        <?= $this->headScript() . "\n"; ?>
        <?= $this->analytics(); ?>
        <style type="text/css">
            #imagemFundo{
                background-image: url('/public/images/creas.png');
                background-repeat: no-repeat;
                background-size: cover;
                width: 800px;
                height: 120px;
            }
        </style>
    </head>
    <body>
        <div id="page">
            <!--CABEÇALHO-->
            <div class="cabecalho">
                <div class="brasao">

                </div>
                <div class="dados_cabecalho">
                    <?
                        while ($req = pg_fetch_array($queryLogon)) {
                            if ($req['uni_codigo'] == 22) {
                                echo "ola mundo";
                            }
                        }
                    ?>
                </div>
                <div class="cod_bar">
                    <div id="img_bar">&nbsp;
                    </div>
                </div>
            </div>

            <div id="titulo_impressao"><b><?= $this->tipo_impressao; ?></b></div>
            <!--FIM DO CABEÇALHO-->
            <!--CONTEÚDO-->
            <?= $this->layout()->content; ?>
            <!--FIM DO CONTEÚDO-->
            <!--RODAPE -->
            <div id="footer">
                <div id="assinatura">
<table width='100%' cellspacing=0 cellpadding=0 border=0>
<tr>
<td>
   _____________________________<br/>
 <b>Responsável:</b>
</td>
<td>
                    _____________________________<br/>
                    <b>Assistente Social:</b>
<td>
</tr>
</table>
                </div>
                <div id="endereco">
                    <?= $this->secretaria->endereco_secretaria ?>,
                    <?= $this->secretaria->numero_end_secretaria ?>,
                    <?= $this->secretaria->sec_bairro ?>,
                    TELEFONE:<?= $this->secretaria->telefone_secretaria ?>
                </div>
            </div>
            <!--fim do rodapé-->
        </div>
    </body>
</html>

But it's like the while did not even exist, the whole page loads minus the while. Did I make any syntax or logical errors?

    
asked by anonymous 22.08.2018 / 16:12

1 answer

1

The error is that pg_query does not only accept one parameter, but two!

You need to pass resouce of the current connection as the first parameter.

Example:

$db = pg_connect("dbname=wallace");

$result = pg_query($db, "SELECT * FROM x WHERE a=b;");

If there are errors in the query, $result will return FALSE .

What can be verified like this:

 if (! $result) {
      die('Epa, deu pau aí');
 }

In this case, you can find out what the error is, also using the pg_last_error function.

So:

 if (! $result) {
       die(pg_last_error($db));
 }
    
22.08.2018 / 16:45