简体   繁体   中英

php - mysql fetch array (LIKE%)

I have a php file which fetch and print data from my mysql table. here is the code..

<?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>

The problem is, The query is skipping (or not giving) data from the first row. Everything else works fine..

I am using

Apache/2.2.13 (Win32) PHP/5.3.0

MySQL client version: mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $

It's because of the line:

($info3 = mysql_fetch_array($result3));

You are fetching the first row and not doing anything with it :)

Also, the { and } around your first block of PHP don't do anything.

EDIT:

If it was working before, except for the first row, you should be ok if you change the first PHP block to:

<?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);
?>

There are some other strange things going on (for example, you never use the $username variable), but at this should give the correct output at least.

It's becouse you are fetching the first row with this line:

($info3 = mysql_fetch_array($result3));

And this moves the array pointer to the second item.

Also you don't have to execute the query twice to get number of rows and the data. Just do something like this:

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

I would also advise you to not use the MySql extension, use the new MySqli or PDO .

Try also this:

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

print_r($arrData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM