我有这个php的一些问题,因为它没有从数据库返回任何东西,所以我意识到它可能没有从$ _POST接收变量。所以我试着打印它并没有返回任何东西。所以我手动输入了它的价值,但它仍然没有打印!虽然它打印文本,但如果脚本中存在语法问题,它也不会打印文本(我认为)。所以我不知道出了什么问题。
带有表格的HTML部分被打印出来! 最后有一个" echo"任何&#34 ;;"它也印刷了!唯一没有打印的是$ 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>
答案 0 :(得分:1)
替换此行
if ($nome = ""){
带
if ($nome == ""){
您正在分配值而不是比较。
答案 1 :(得分: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."'");
}