Home  >  Article  >  Backend Development  >  请教下php的Resource问题?

请教下php的Resource问题?

WBOY
WBOYOriginal
2016-06-23 14:02:011003browse

本帖最后由 xuzuning 于 2014-02-20 10:40:28 编辑

php Resource

<?phpheader("Content-type: text/html; charset=utf-8");   if(!empty($_POST['username'])){  $name=$_POST['username'];}  if(!empty($_POST['pwd'])){  $pwd=$_POST['pwd'];}    function sqlDql($name,$pwd){  $conn=mysql_connect("localhost","root","root");  if(!$conn){  die("连接失败".mysql_error());  }  mysql_select_db("test",$conn) or die(mysql_error());  //设置字符集    mysql_query("set names utf8");  $sql="select * from zhuche;";    $res=mysql_query($sql,$conn) or die(mysql_error());    while($row=mysql_fetch_row($res)){          if($name==$row[1]){       	echo "用户名已经存在<br/>";       	echo "<a href='login.php'>返回注册页面</a>";        // die(mysql_free_result($res)."连接".mysql_close($conn));        mysql_free_result($res);          mysql_close($conn);        exit();    	          }       }    $sql2="insert into zhuche (Username,password) values ('".$name."','".$pwd."');";    $res2=mysql_query($sql2,$conn) or die(mysql_error());    echo "<table border='1px solid black'>";    echo "<tr><td>序号</td><td>用户</td><td>密码</td></tr>";    while($row=mysql_fetch_row($res)){     	echo "<tr>";     	foreach($row as $key=> $val){     	 echo "<td>--".$val."</td>";     	 }     	echo "</tr>";     	}    echo "</table>";	     mysql_free_result($res);      mysql_close($conn);  }  sqlDql($name,$pwd);?>
如果在第一个while没有退出 为什么下面的$res取不出数据?SQL语句出的问题没问题。
输出结果是


回复讨论(解决方案)

33 行应为
while($row=mysql_fetch_row($res2)){
而不是
while($row=mysql_fetch_row($res)){

$res2是一个boolean值没有结果集呀

噢,看错了

但是 $res 经 while($row=mysql_fetch_row($res)){ 后已经指向结果集的尾部了
你需要用 mysql_data_seek($res, 1); 回绕结果集 
不过请注意:即便回绕了,也不会输出 mysql_query($sql2,$conn) 插入的值

很感谢,结账

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn