English 中文(简体)
我sql_fetch_array
原标题:mysql_fetch_array problem
  • 时间:2010-07-07 12:45:11
  •  标签:
  • php
  • mysql

i am trying to do a guestbook in php but i am having some problems with mysql_fetch_array function. I don t understand why. I try to debug by putting die("Error ".mysql_error()) but nothing prints out. I guarantee that all my variables are correctly initialized. Here is my code :

<?php

 $nbmessagesPP = 10;
 mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
 mysql_select_db(DBNAME) or die ("Unable to select database!"); 

 .......

 if(isset($_GET[ page ])){
    $page = $_GET[ page ];
  } else {
    $page = 1;
  }
  $first_msg = ($page - 1) * $nb_of_Page;
  $query =  Select * from livredor ORDER BY id DESC LIMIT  .$first_msg. ,  .$nbmessagesPP;
  $rep = mysql_query($query) or exit("Error in query".mysql_error());

  $v = true;
  while($v){
      $v = ($data = mysql_fetch_array($rep) or die ("Error fetching the data : ".mysql_error()));
      echo "<p>id -> ".$data[ id ]."</p>";    
      echo "<p>pseudo ->".$data[ pseudo ]."</p>";
      echo "<p>messages ->".$data[ message ]."</p>";
      echo "<hr/>";
  } 
  mysql_close();
?> 

一个人能够帮助我;

最佳回答

你的法典没有正确地处理错误或最后一行。 当v 是假的时,它仍在印刷一些数据。 最好改写如下:

while (($data = mysql_fetch_array($rep))) {    
  echo   
  ... 
}

这需要在开始印刷之前对单体进行评价。

问题回答

问题在于,你再次试图获取没有结果的部分。 mysql_fetch_array> 回归一个固定阵列,并附有立体指数。 您想要的是mysql_fetch_assoc,该编码回归一个联系阵列。

<><>Edit>: 你们也有克里斯特的问题,没有正确处理最后一行。

一般来说,如果你再次收到一个错误的话,即“应用论点不是一种有效的MySQL结果资源”,就意味着MySQL的查询失败了,那就没有归还有效的结果资源。

在通过<代码>mysql_query(<>/code>)发送之前,请填上$query,然后尝试将重复的质询带入phpMyAdmin,看看它是否恢复任何结果。

Ok i have found the problem. The problem was that in another page i had a mysql_connection and in that page i was creating a new one. I just catch the return value of mysql_connect function and then close it with mysql_close function at the end. Like this :

  <?php
     $link = mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
     mysql_select_db(DBNAME) or die ("Unable to select database!"); 
     .....

     while($data = mysql_fetch_array($rep)) {//i do something here}

     mysql_close($link);    
 ?>

感谢您的答复:





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签