2014-10-18 108 views
0

我有這個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> 

回答

1

替換該行

if ($nome = ""){ 

if ($nome == ""){ 

你分配值,而不是比較。

+0

老兄!非常感謝!多麼愚蠢的錯誤!的xD – renatoff 2014-10-18 06:18:33

0
`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."'"); 
}