Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very Strange Problem

Status
Not open for further replies.

johnrg1

Programmer
Jan 24, 2003
38
0
0
GB
Hi All,

I'm very new to PHP, I'm an ASP guy traditionally.

I have come across a very strange problem when extracting data from a MySQL DB.

It seems to "skip" bits of data. I have used the following code:

echo "Serial Number: $row[SerialNo]
echo "Repair Type: ";
echo DisplayRepairType( $row[Type] );
echo &quot;<br>Product Status: &quot;;
echo DisplayLocation( $row[Location ] );
echo &quot;<br><br>&quot;;

result:

Serial Number:
Repair Type:Unknown
Product Status: Requested

Serial Number: XX-XXX-01sd
Repair Type:Unknown
Product Status: Requested


On the first loop through the data, the serial number is omitted, but it appears on all the other entries.

I can change the sort order in the SQL, and the serial numbers displayed change, but the first one is still ommitted.

I'm really puzzled by this and I can see no reason for it not to work.

Anyone ever come across this before?

Thanks

John
 
There are way to many variables in your question to be able to answer.

The problem could be in the way your code loops through the retured records.

The problem could be maked by default behaviors in your user-defined functions DisplayRepairType() and DisplayLocation(). What do they do when they don't have data to display?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Hi sleipnir,

the two functions read the integer values from the database and replay with a text string depending on the value. these are always in the database as they are required fields. i have just check and these values are not being retunrd from the database either :|

This is the code i am using in full:

$query_ProductDetails = &quot;SELECT * FROM tbl_product WHERE tbl_product.rma_id = $vRMA_ID ORDER BY line_ID DESC&quot;;
$ProductDetails = mysql_query($query_ProductDetails);
$row_ProductDetails = mysql_fetch_assoc($ProductDetails);

do
{
echo &quot;<i>Serial Number:</i> $row[SerialNo] <br> <i>Repair Type:</i>&quot;;
echo DisplayRepairType( trim($row[Type]) );
echo &quot;<br>Product Status: &quot;;
echo DisplayLocation( $row[Location ] );
echo &quot;<br><br>&quot;;
}
while ($row = mysql_fetch_array($ProductDetails));

I'm really puzzled by this... Any ideas?

John
 
I don't know whether this'll change anything, but I usually write out my $row variables as such: $row[&quot;Type&quot;], with quotes.
 
Oh, I see, the quotes are omitted, hence... ok. (embarassed smile).

Anyway: obviously nothing is being printed the first time, as $row hasn't been initialized yet! Instead of a do...while, make a while... loop. Ie:
Code:
while $row = mysql_fetch_array($ProductDetails)
{ 
 echo &quot;<i>Serial Number:</i> $row[SerialNo] <br> <i>Repair Type:</i>&quot;;
 echo DisplayRepairType( trim($row[Type]) );
 echo &quot;<br>Product Status: &quot;;
 echo DisplayLocation( $row[Location ] );
 echo &quot;<br><br>&quot;;
}

A second thing is, you'd better go for a mysql_fetch_assoc, as mysql_fetch_array will probably return output similar to this:
Code:
[0]=>&quot;XLIUHIU&quot;
[1]=>&quot;Finished&quot;
.
.
.
instead of
Code:
[SerialNo]=>&quot;XLIUHIU&quot;
[Location]=>&quot;Finished&quot;
etc..
 
Thank you guys,

Can i ask you just one more question, howdo you connect to your database?
I use the following code:

connect($dbhost, $dbuser, $dbpass, $dbname);
$query_ProductDetails = &quot;SELECT * FROM tbl_product&quot;;
$ProductDetails = mysql_query($query_ProductDetails);
$row_ProductDetails = mysql_fetch_assoc($ProductDetails);

but is does not work with the while at the top of the loop. It loops through ( but omitts the first entry ) with this connection with the do...while loop.

Can you think of anything else i might be doing wrong now?

Thanks Guys.

John
 
Fixed that problem! there where no brackets in the While statement :|

BUT... but he says... it is only pulling out 1 entry, then there should be 2. i can see them in the database, and extract them out using SQL on the database itself, but the page is only displaying 1 row. I can change wht is displays by changing the ORDER BY but it still returns 1 row, just a different one...

Long live ASP.NET :) damn this new job.....

Help :)
 
The first two steps are alright, but the fetch has to happen in the loop, at the top. Also, your fetching strings from a database, it is wise to use the stripslashes and add slashes if the data can contain single quotes or other system chars that could change the meaning of a command.

I typically cheat when it comes to the loop and getting the results. I use list, so that the code in the loop is smaller, and that way I don't have to worry about whats in the array. It looks like in your bit of code that your not looping at all so your getting the first one in the result and stopping. The bellow is how I would do it, but there are as many styles as people, eh?
Code:
// loop to display
while(list($SerialNo,$Type, $Location) = mysql_fetch_row($ProductDetails))
{
 echo &quot;<i>Serial Number:</i> &quot; .  $SerialNo . &quot;<br>&quot;;
 echo<i>Repair Type:</i>&quot; . DisplayRepairType(trim($Type)) . &quot;<br>;
 echo &quot;Product Status: &quot; . DisplayLocation($Location) . &quot;<br><br>&quot;;
}

ASP is good, but PHP is better... :p You'll learn to love it. A good resource you may want on to get on hand is: O'Rilly's PHP Pocket Reference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top