PHP - 为什么不打印变量,尽管它打印文本?

问题描述:

我有这个PHP的一些问题,因为它没有从数据库中返回任何东西,所以我意识到也许它没有收到$ _POST的变量。所以我试图打印它,它没有返回任何东西。所以我手动输入了它的值,但它仍然没有打印任何东西!虽然它打印文本,但如果脚本中存在语法问题,它也不会打印文本(我认为)。所以我不知道什么是错的。PHP - 为什么不打印变量,尽管它打印文本?

打印表格的HTML部分! 最后有一个“回声”什么“;”它也是印刷的!唯一不打印的是$ nome变量。

对不起葡萄牙语文本,但你不需要担心它们。

<?php 

     //$nome = $_POST['nome']; 
     $nome = "renato"; //ive set the value manually for tests 

     $pdo = new PDO('mysql:host=myserver;dbname=mydb', 'myuser', 'mypw'); 


     if ($nome = ""){ 
      $sql = $pdo->query("SELECT * FROM clientes"); 
     } 
     else{ 
      $sql = $pdo->query("SELECT * FROM clientes WHERE nome='".$nome."'"); 
     } 

     echo $nome; //prints nothing! 
    ?> 

    <table border="1px"> 
     <tr> 
      <td> 
       <h3>ID</h3> 
      </td> 
      <td> 
       <h3>NOME</h3> 
      </td> 
      <td> 
       <h3>ENDERECO</h3> 
      </td> 
      <td> 
       <h3>BAIRRO</h3> 
      </td> 
      <td> 
       <h3>CIDADE</h3> 
      </td> 
      <td> 
       <h3>FIS_JUR</h3> 
      </td> 
      <td> 
       <h3>RG</h3> 
      </td> 
      <td> 
       <h3>CPF</h3> 
      </td> 
      <td> 
       <h3>TEL</h3> 
      </td><td> 
       <h3>TEL2</h3> 
      </td> 
      <td> 
       <h3>DATA_NASC</h3> 
      </td>  
     </tr> 


//this table gets printed! 


    <?php 

     while($row = $sql->fetch(PDO::FETCH_ASSOC)){ 
     echo " 
     <tr> 
      <td> {$row['id']} 
      </td> 

      <td> {$row['nome']} 
      </td> 

      <td> {$row['endereco']} 
      </td> 

      <td> {$row['bairro']} 
      </td> 

      <td> {$row['cidade']} 
      </td> 

      <td> {$row['fis_jur']} 
      </td> 

      <td> {$row['rg']} 
      </td> 

      <td> {$row['cpf']} 
      </td> 

      <td> {$row['tel']} 
      </td> 

      <td> {$row['tel2']} 
      </td> 

      <td> {$row['data_nasc']} 
      </td> 
     </tr>"; 

//this one prints nothing! 
     } 

     echo $nome; //prints nothing 
     echo "anything"; //yes, this one gets printed 
     $pdo = null; 
     $sql = null; 
    ?> 
     </table> 

替换该行

if ($nome = ""){ 

if ($nome == ""){ 

你分配值,而不是比较。

+0

老兄!非常感谢!多么愚蠢的错误!的xD – renatoff 2014-10-18 06:18:33

`the problem is that single = sign. rather use double == for comparing.` 

the batter way checking empty string use 

` if (empty($nome)){ 
    $sql = $pdo->query("SELECT * FROM clientes"); 
}else{ 
    $sql = $pdo->query("SELECT * FROM clientes WHERE nome='".$nome."'"); 
}