Compare SQL result using PHP

Asked

Viewed 197 times

0

I started a Session and performed an SQL search

<?
    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 you die 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 by its made returns the unit code and the specialty . What I really care about 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 while doesn’t even exist ,the whole page is loaded minus while . I made some syntax or logic error ?

1 answer

1


The mistake is that pg_query accepts not only one parameter, but two!

You need to pass the 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 checked so:

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

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

Thus:

 if (! $result) {
       die(pg_last_error($db));
 }
  • I tried to make these changes even though I haven’t had results yet .

  • And one thing the connection is not required correct ? for example I did not pass the Connection it will use the last call of the page is not ? or spoke nonsense .

  • Thank you very much, I found the problem really p_query recovers the last session but I had not informed correctly . Problem solved.

Browser other questions tagged

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