PHP:未从MySQL和未定义变量获取结果
我正在从事一个项目,在这个项目中,我从数据库中获取一些信息,并希望将其显示在表单中。我在SQL日志中检查的SQL查询看起来不错,但我没有从mysql中得到任何结果。我想从数据库中检索值并在表单中设置它。SQL有什么问题吗?如何显示从数据库读取的值并将其显示在表单中?多谢各位 代码:PHP:未从MySQL和未定义变量获取结果,php,html,mysql,Php,Html,Mysql,我正在从事一个项目,在这个项目中,我从数据库中获取一些信息,并希望将其显示在表单中。我在SQL日志中检查的SQL查询看起来不错,但我没有从mysql中得到任何结果。我想从数据库中检索值并在表单中设置它。SQL有什么问题吗?如何显示从数据库读取的值并将其显示在表单中?多谢各位 代码: <?php $client_id = $_GET['clientId']; $name ="rahul"; $contact; $link = mysqli_connect(
<?php
$client_id = $_GET['clientId'];
$name ="rahul";
$contact;
$link = mysqli_connect("localhost", "root", "akshay2787", "tim");
$sql = "select name,contact,tel1,tel2,email1,email2,investment_time,interest,transport,deposit,tax1,tax2,tax3,
address1,address2,address3,notes from client_setup where client_id='".$client_id."'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$name = $row['name'];
echo "<script type=\"text/javascript\">alert('Thank you form is submitted. $name');</script>";
$contact = $row['Contact'];
$tel1 = $row['Telephone1'];
}
}
?>
///And below I display the values.
<form action="" method="get" align="center" >
<table align="center" class="table-block">
<tr class="highlight">
<td width="100"><label for="netmask">Name</label></td>
<td width="600"><input class="highlight" type="text" name="Name" id="name"><?php print $name;?></input></td>
</tr>
</table>
客户端设置表:
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| client_setip_id | int(30) unsigned | NO | PRI | NULL | auto_increment |
| client_id | int(30) unsigned | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| username | varchar(50) | YES | | NULL | |
| password | varchar(50) | YES | | NULL | |
| contact | varchar(50) | YES | | NULL | |
| tel1 | varchar(50) | YES | | NULL | |
| tel2 | varchar(50) | YES | | NULL | |
| email1 | varchar(50) | YES | | NULL | |
| email2 | varchar(50) | YES | | NULL | |
| investment_time | double | YES | | NULL | |
| interest | double | YES | | NULL | |
| transport | double | YES | | NULL | |
| deposit | double | YES | | NULL | |
| tax1 | double | YES | | NULL | |
| tax2 | double | YES | | NULL | |
| tax3 | double | YES | | NULL | |
| address1 | varchar(500) | YES | | NULL | |
| address2 | varchar(500) | YES | | NULL | |
| address3 | varchar(500) | YES | | NULL | |
| notes | varchar(500) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
21 rows in set (0.01 sec)
我想这就是你的数据没有显示的地方
<td width="600"><input class="highlight" type="text" name="Name" id="name"><?php print $name;?></input></td>
假设您的查询是正确的,它可能不会显示在您的输入字段中
用这个来显示它
<td width="600"><input class="highlight" type="text" name="Name" id="name" value="<?php echo $name;?>"></td>
对不起,我犯了个愚蠢的错误。在测试期间,我更改了数据库中的clientid。在设置了正确的clientid之后,我得到了数据。非常感谢。我投票结束这个问题。谢谢大家
mysqli_num_rows()的行为取决于使用的是缓冲还是非缓冲结果集。对于未缓冲的结果集,在检索结果中的所有行之前,mysqli_num_rows()不会返回正确的行数。
。尝试使用获取所有行,然后对它们进行计数,或者使用缓冲结果。如果您确实没有给自己留出空间来检查查询中的错误,请使用mysqli_error()显示查询中是否有错误!!首先,您是否通过查询返回数据?如果你这样做了,那么我认为这是你在输入字段中显示代码的问题
<td width="600"><input class="highlight" type="text" name="Name" id="name" value="<?php echo $name;?>"></td>
<td width="600"><input class="highlight" type="text" name="Name" id="name" value="<?=$name?>"></td>
if($result = mysqli_query($link, $sql)){
//Block of code in OP question
}
else{
echo "Error: ". mysqli_error().". The Query was <br /><pre>$sql</pre>";
}