Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 $

share|improve this question

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
if i remove that line, how can i get data? –  blasteralfred Ψ Dec 30 '10 at 9:19
    
You are already getting the data in the line while($info3 = mysql_fetch_array($result3)){. –  Spiny Norman Dec 30 '10 at 9:20
    
can u modify the script for me?? i am stuck.. :P –  blasteralfred Ψ Dec 30 '10 at 9:21
    
thanks in advance :) –  blasteralfred Ψ Dec 30 '10 at 9:22
    
yea its working well... thanks dude... :) –  blasteralfred Ψ Dec 30 '10 at 9:32

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.

share|improve this answer
    
if i remove that line, how can i get data? –  blasteralfred Ψ Dec 30 '10 at 9:19
    
You are retrieving data in your while loop: while($info3 = mysql_fetch_array($result3)){ –  Jan Hančič Dec 30 '10 at 9:22

Try also this:

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

print_r($arrData);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.