数据库记录没有完全连续放置

时间:2013-09-27 05:28:31

标签: php mysql

我有这个代码,我的数据库中的主要代码是。

<input class="textbox1" tabIndex="8" maxLength="49" size="30" name="n_counter" value='
<?php
   $result = mysql_query("SELECT * FROM procurement WHERE counter != ' '");
   while ( $row = mysql_fetch_array( $result ) ) {
      $value = $row['counter'];
   }
   $i = "0";
   while ( $i <= $value ) {
      $i++;
   }
   $value2 = strlen( $i );
   if ( $value2 == 1 ) {
      echo "00000" . $i;
   } else if ( $value2 == 2 ) {
      echo "0000" . $i;
   } else if ( $value2 == 3 ) {
      echo "000" . $i;
   } else if ( $value2 == 4) {
      echo "00" . $i;
   } else if ( $value2 == 5 ) {
      echo "0" . $i;
   } else {
      echo $i;
   }
?>'>

循环语句运行正常,但是在我从数据库中删除记录后会出现问题。

enter image description here

当我删除000001记录并添加另一条记录时,该记录接近000000,我想要的是000010应该放在000009旁边。在我的其他项目中工作正常。可能是什么问题?

2 个答案:

答案 0 :(得分:0)

每件事都没问题

只需更新此

$result = mysql_query("SELECT * FROM procurement WHERE counter != ' ' ORDER BY counter ASC");

强烈推荐您应该使用 mysqli 而不是mysql

您还必须更新connection,然后再更新Query execution

$connection = ($GLOBALS["___mysqli_ston"] = mysqli_connect( "db_server",  "db_user",  "db_password","database" )) or die( ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) ) ;

$sql = "SELECT * FROM procurement WHERE counter != ' ' ORDER BY counter ASC";
$result = mysqli_query($GLOBALS["___mysqli_ston"],  $sql ) or die( ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) ) ; 

答案 1 :(得分:0)

在sql查询中添加“ORDER BY counter ASC”。

$result = mysql_query("SELECT * FROM procurement WHERE counter != ' ' ORDER BY counter ASC");