【问题标题】:php - mysql fetch array (LIKE%)php - mysql 获取数组 (LIKE%)
【发布时间】:2012-12-25 01:33:37
【问题描述】:

我有一个 php 文件,它从我的 mysql 表中获取和打印数据。 这是代码..

<?php
{
$username = 'root';
$bookid = "GCK";
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("library", $con);

$query3 = "SELECT * FROM GCKcatalogue WHERE BookID LIKE'%$bookid%'";
$numresults=mysql_query($query3);
$numrows=mysql_num_rows($numresults);

$result3 = mysql_query($query3);
($info3 = mysql_fetch_array($result3));

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="en-us" http-equiv="Content-Language">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Results</title>

</head>
<body>

<table align="center" style="width: 80%" class="style4" cellpadding="1px" cellspacing="1px">
<tr>
<td class="style19">Resource ID</td><td class="style19">ISBN</td><td class="style19">Title</td><td class="style19">Author</td><td class="style19">Publisher</td><td class="style19">Year</td><td class="style19">Edition</td><td class="style19">Language</td><td class="style19">Type</td><td class="style19">Price</td><td class="style19">Location</td><td class="style19">Abstract</td><td class="style19">Availability</td><td class="style19">Last Issued</td><td class="style19">Last Received</td><td class="style19">Loan To</td>
</tr>

<?php
while($info3 = mysql_fetch_array($result3)){
$RID = $info3["BookID"];
$ISBN = $info3["ISBN"];
$Title = $info3["Title"];
$Author = $info3["Author"];
$Publisher = $info3["Publisher"];
$Year = $info3["Year"];
$Edition = $info3["Edition"];
$Language = $info3["Language"];
$Type = $info3["Type"];
$Price = $info3["Price"];
$Location = $info3["Location"];
$Abstract = $info3["Abstract"];
$Availability = $info3["Availability"];
$Issue = $info3["IssueDate"];
$Receive = $info3["ReceiveDate"];
$User = $info3["User"];


 echo '
 <tr>
 <td class="style20">' . $RID . '</td>
 <td class="style20">' . $ISBN . '</td>
 <td class="style20">' . $Title . '</td>
 <td class="style20">' . $Author . '</td>
 <td class="style20">' . $Publisher . '</td>
 <td class="style20">' . $Year . '</td>
 <td class="style20">' . $Edition . '</td>
 <td class="style20">' . $Language . '</td>
 <td class="style20">' . $Type . '</td>
 <td class="style20">' . $Price . '</td>
 <td class="style20">' . $Location . '</td>
 <td class="style20">' . $Abstract . '</td>
 <td class="style20">' . $Availability . '</td>
 <td class="style20">' . $Issue . '</td>
 <td class="style20">' . $Receive . '</td>
 <td class="style20">' . $User . '</td>
 </tr>
 ';
}

?>

</table>

</body>

</html>

问题是, 查询正在跳过(或不提供)第一行的数据。其他一切正常..

我正在使用

Apache/2.2.13 (Win32) PHP/5.3.0

MySQL 客户端版本:mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这是因为这条线:

    ($info3 = mysql_fetch_array($result3));
    

    您正在获取第一行并且没有对它做任何事情:)

    另外,您的第一个 PHP 块周围的 {} 不会做任何事情。

    编辑:

    如果它之前工作过,除了第一行,如果你将第一个 PHP 块更改为:

    <?php
    $username = 'root';
    $bookid = "GCK";
    $con = mysql_connect("localhost","root","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("library", $con);
    
    $query3 = "SELECT * FROM GCKcatalogue WHERE BookID LIKE'%$bookid%'";
    $numresults=mysql_query($query3);
    $numrows=mysql_num_rows($numresults);
    
    $result3 = mysql_query($query3);
    ?>
    

    还有一些其他奇怪的事情正在发生(例如,您从不使用 $username 变量),但至少应该给出正确的输出。

    【讨论】:

    • 如果我删除该行,我如何获取数据?
    • 你已经在while($info3 = mysql_fetch_array($result3)){这一行得到了数据。
    • 你能帮我修改脚本吗?我被卡住了..:P
    【解决方案2】:

    这是因为您正在使用这一行获取第一行:

    ($info3 = mysql_fetch_array($result3));

    这会将数组指针移动到第二项。

    此外,您不必执行两次查询来获取行数和数据。只需执行以下操作:

    $result = MySql_Query ( 'QUERY HERE' );
    $numOfRows = MySql_Num_Rows ( $result );
    while ( $row = MySql_Fetch_Array ( $result ) ) {
      ...
    }
    

    我还建议你不要使用 MySql 扩展,使用新的 MySqliPDO

    【讨论】:

    • 如果我删除该行,我如何获取数据?
    • 您在 while 循环中检索数据:while($info3 = mysql_fetch_array($result3)){
    【解决方案3】:

    也试试这个:

    $result = MySql_Query ('QUERY HERE'); $numOfRows = MySql_Num_Rows ( $result ); 而 ( $row = MySql_Fetch_Array ( $result ) ) { $arrData[] = $行; }

    print_r($arrData);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 2018-10-24
      • 2011-05-05
      • 1970-01-01
      • 2013-11-18
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多