What should Query be to get a tuple result where the value of a column is 'x'?

Asked

Viewed 319 times

2

I have the following query:

$select = "SELECT concat(mid(cpf,1,3),'.',mid(cpf,4,3),'.',mid(cpf,7,3),'-',mid(cpf,10,2)) as cpf, nome, genero, nome_cracha, rg, crm, crm_uf, rua, bairro, cep, cidade,
    estado, telefone, celular, fax, email, cna, ano_titulo_especialista, categoria_inscricao,
    forma_pagamento, dataconfirmacao, data from inscricao order by nome ASC";

and I want the variable $x to know the value "categoria_registration" (which the query will show). This is what I want to do:

if($x == "valor de categoria_inscricao" ){
   ... 
}

Edited

I’m using a code to convert table values to Excel. But I want instead of appearing the column value, I can show that if the column value is "x" what will appear in Excel is "y".

Follows the code:

<?php
if (!isset($_SESSION)) session_start();
if (!isset($_SESSION['usuario'])) {
    session_destroy();
    header("Location: index.php"); exit;
}
    include("conexao.php");

    $select = "SELECT concat(mid(cpf,1,3),'.',mid(cpf,4,3),'.',mid(cpf,7,3),'-',mid(cpf,10,2)) as cpf, nome, genero, nome_cracha, rg, crm, crm_uf, rua, bairro, cep, cidade,
    estado, telefone, celular, fax, email, cna, ano_titulo_especialista, categoria_inscricao,
    forma_pagamento, dataconfirmacao, data from inscricao order by nome ASC";



    $export = mysql_query($select) or die ("Sql error : ".mysql_error());

    $fields = mysql_num_fields($export);

    $header = "";
    $data = ""; 
    for ( $i = 0; $i<$fields; $i++ )
    {
    $header .= mysql_field_name($export, $i)."\t";
    }

    while( $row = mysql_fetch_row( $export ) )
    {
    $line = '';
    foreach( $row as $value )
    {
    if ((!isset($value)) || ($value == ""))
    {
    $value = "\t";
    }
    else
    {
    $value = str_replace( '"' , '""' , $value );
    $value = '"'.$value.'"'."\t";
    }
    $line .= $value;
    }
    $data .= trim( $line ) . "\n";
    }
    $data = str_replace( "\r" , "" , $data );

    if ( $data == "" )
    {
    $data = "\n(0) Records Found!\n";
    }

    $novonome = date('Ymd');

    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=".$novonome.".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$data";

    ?>

2 answers

4


Have this example below.
Note that in the printf to compare he uses $row["id"], that is, as the index of the array it uses the name of the field you return. In your case

if($row['valor de categoria_inscricao'] == $x)

serious ...or serious that.

mysql_fetch_array()

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Não foi possível conectar: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>
  • aeee ... that ae man ...haha vlw!

1

You can restrict the search value in the query to do this. For example:

SELECT nome
FROM inscricao
WHERE categoria_inscricao = <valor desejado>
ORDERBY nome ASC

This query will return only the names of the lines whose value of categoria_inscricao has been predefined.

For more information on claúsula WHERE, click here.

  • understood... but I think my doubt goes beyond ... I edited the post to explain better.

  • Really, yes, I thought you just wanted to return what you had in the bank with the "value x".

Browser other questions tagged

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