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


PHP - Why its not printing the variable although it prints text?

我对这个php有一些问题,因为它没有从DB返回任何内容,所以我意识到它可能没有从$_POST接收变量。所以我试着把它打印出来,但没有收到任何回报。所以我手动输入了它的值,但它仍然没有打印任何内容!虽然它打印文本,但如果脚本中存在语法问题,它不会打印文本(我认为)。所以我不知道怎么了。

带有表格的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 == ""){

您正在分配值,而不是进行比较。

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