473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using mysql_result() Deletes Row From Result

Trying to do a pretty simple page where I display car parts on a page based on the category selected from a drop down list. Here's my tables setup:

categories
code CHAR(3)
description (25)

parts
category CHAR(3)
part_number CHAR(15)
years CHAR(9)
description CHAR(35)
price DECIMAL(6,2)
graphic CHAR(15)

What I try to do is pretty simple:

1)Query db to get items in category based on what is chosen in the drop down list
2) Write header of table for displaying data, including category name as title
3) Cycle through returned result using mysql_fetch_array().

Pretty simple. The problem I'm having is with getting the category name. I use one query to get everything:

SELECT parts.category, parts.part_number,parts.years, parts.description, parts.price, parts.graphic, categories.description AS Category";
FROM parts INNER JOIN categories on parts.category = categories.code
WHERE parts.category = '$parts_choice'

I then use mysql_result() to get the category name from the first row (since it will be the same in all rows). The problem I'm having is that
when I use mysql_result(), the data from the last row in the result set is not displayed, even though the HTML for that row is written out, and
the "No parts found for this category" message is written to the page (see below).
for ($i = 0;$i < $num_results2; $i++)
{
$row2 = mysql_fetch_array($result2);

if (!$row2)
{
echo "No parts found for this category.";
}
?>
<tr>
<td><?php echo $row2["part_number"]?></td>
<td><?php echo $row2["years"]?></td>
<td><?php echo $row2["description"]?></td>
<td>$<?php echo $row2["price"]?></td>
<?php
if (strlen($row2["graphic"]) 0)
{
?>
<td align="center"><img src="graphics/<?php echo $row2["graphic"].".jpg"?>" border="0"></td>
<?php
}
else
{
?>
<td>No picture available</td>
<?php
}
?>
</tr>
<?
}

So it's like for some reason, reading the Category name from the first row causes the last row from the result to be deleted. Has anyone seen
this before, or can someone explain why this might be happening?

Thanks.

Steve
Sep 10 '06 #1
8 1470
Steve wrote:
Trying to do a pretty simple page where I display car parts on a page
based on the category selected from a drop down list. Here's my tables
setup:

categories
code CHAR(3)
description (25)

parts
category CHAR(3)
part_number CHAR(15)
years CHAR(9)
description CHAR(35)
price DECIMAL(6,2)
graphic CHAR(15)

What I try to do is pretty simple:

1)Query db to get items in category based on what is chosen in the drop
down list
2) Write header of table for displaying data, including category name as
title
3) Cycle through returned result using mysql_fetch_array().

Pretty simple. The problem I'm having is with getting the category
name. I use one query to get everything:

SELECT parts.category, parts.part_number,parts.years, parts.description,
parts.price, parts.graphic, categories.description AS Category";
FROM parts INNER JOIN categories on parts.category = categories.code
WHERE parts.category = '$parts_choice'

I then use mysql_result() to get the category name from the first row
(since it will be the same in all rows). The problem I'm having is that
when I use mysql_result(), the data from the last row in the result set
is not displayed, even though the HTML for that row is written out, and
the "No parts found for this category" message is written to the page
(see below).
for ($i = 0;$i < $num_results2; $i++)
{
$row2 = mysql_fetch_array($result2);

if (!$row2)
{
echo "No parts found for this category.";
}
?>
<tr>
<td><?php echo $row2["part_number"]?></td>
<td><?php echo $row2["years"]?></td>
<td><?php echo $row2["description"]?></td>
<td>$<?php echo $row2["price"]?></td>
<?php
if (strlen($row2["graphic"]) 0)
{
?>
<td align="center"><img src="graphics/<?php echo
$row2["graphic"].".jpg"?>" border="0"></td>
<?php
}
else
{
?>
<td>No picture available</td>
<?php
}
?>
</tr>
<?
}

So it's like for some reason, reading the Category name from the first
row causes the last row from the result to be deleted. Has anyone seen
this before, or can someone explain why this might be happening?

Thanks.

Steve
Steve,

The code you've posted shouldn't do that. Mind posting ALL of you
rcode? For instance - where do you fetch the data and retrieve the
category?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '06 #2
"Steve" <ra***********@hotmail.comwrote in
news:Xr******************************@comcast.com. ..
[...]
I then use mysql_result() to get the category name from the first row
(since it will be the same in all rows). The problem I'm having is that
when I use mysql_result(), the data from the last row in the result set is
not displayed, even though the HTML for that row is written out, and the
"No parts found for this category" message is written to the page (see
below).
for ($i = 0;$i < $num_results2; $i++)
{
$row2 = mysql_fetch_array($result2);
try this

$query = "SELECT parts.category, parts.part_number,parts.years,
parts.description, parts.price, parts.graphic, categories.description AS
Category";
FROM parts INNER JOIN categories on parts.category = categories.code
WHERE parts.category = '$parts_choice'";
$result2 = mysql_query($query) or die("mysql_error());
$rows2 = mysql_num_rows($query);
if ($rows2==0)
{
echo "No parts found for this category.";
}
else
{
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
{
echo "<tr>\n<td>", $row2["part_number"], "</td>\n",
"<td>", $row2["years"], "</td>\n",
"<td>", $row2["description"], "</td>\n",
"<td>\$", $row2["price"], "</td>\n"
}
}
?>

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Sep 10 '06 #3
Steve,

The code you've posted shouldn't do that. Mind posting ALL of you
rcode? For instance - where do you fetch the data and retrieve the
category?
Here you go.

<?
if (isset($_POST['parts_choice']))
{
$parts_choice = $_POST['parts_choice'];
if (substr($parts_choice,0,6) != "Choose")
{
if (db_connect())
{

$query2 = "select parts.category, parts.part_number,parts.years, parts.description, parts.price, parts.graphic, categories.description
AS Category";
$query2 = $query2." FROM parts INNER JOIN categories on parts.category = categories.code";
$query2 = $query2." where parts.category = '$parts_choice'";

//echo("<p class=\"text\" align=\"center\">\$query2= $query2</p>");

$result2 = mysql_query($query2) or die(mysql_error());
$num_results2 = mysql_num_rows($result2) or die(mysql_error());

if ($_POST['get_parts'] == "Display Parts")
{
$category_name = mysql_result($result2,0,6);
?>
<center>
<h2><?php echo $category_name ?></h2>
<table border="1" cellpadding="5">
<tr>
<th>Part Number</th>
<th>Years</th>
<th>Description</th>
<th>Price</th>
<th>Picture</th>
</tr>
<?
}
else
{
echo("<p class=\"text\" align=\"center\">There was an error.</p>");
}

for ($i = 0;$i < $num_results2; $i++)
{
$row2 = mysql_fetch_array($result2);

if (!$row2)
{
echo "No parts found for this category.";
}
?>
<tr>
<td><?php echo $row2["part_number"]?></td>
<td><?php echo $row2["years"]?></td>
<td><?php echo $row2["description"]?></td>
<td>$<?php echo $row2["price"]?></td>
<?php
if (strlen($row2["graphic"]) 0)
{
?>
<td align="center"><img src="graphics/<?php echo $row2["graphic"].".jpg"?>" border="0"></td>
<?php
}
else
{
?>
<td>No picture available</td>
<?php
}
?>
</tr>
<?
}
?>
</table>
<br>
<br>

<?
}
}
}
do_html_footer();
?>
Sep 10 '06 #4
I then use mysql_result() to get the category name from the first row
(since it will be the same in all rows). The problem I'm having is that
when I use mysql_result(), the data from the last row in the result set
is not displayed, even though the HTML for that row is written out, and
the "No parts found for this category" message is written to the page
(see below).
I went back and looked again, and discovered that it is actually the first row in the returned data that is not displayed, not the
last row. So it looks like what's happening is that mysql_result() is removing the data from the first row (the row I'm reading
the get the category name), but it looks like the row is actually there when mysql_fetch_array() is extracting the data, hence the
empty row displayed in the table.

Steve
Sep 10 '06 #5
Steve wrote:
>
>Steve,

The code you've posted shouldn't do that. Mind posting ALL of you
rcode? For instance - where do you fetch the data and retrieve the
category?

Here you go.

<?
if (isset($_POST['parts_choice']))
{
$parts_choice = $_POST['parts_choice'];
if (substr($parts_choice,0,6) != "Choose")
{
if (db_connect())
{

$query2 = "select parts.category,
parts.part_number,parts.years, parts.description, parts.price,
parts.graphic, categories.description AS Category";
$query2 = $query2." FROM parts INNER JOIN categories on
parts.category = categories.code";
$query2 = $query2." where parts.category = '$parts_choice'";

//echo("<p class=\"text\" align=\"center\">\$query2=
$query2</p>");

$result2 = mysql_query($query2) or die(mysql_error());
$num_results2 = mysql_num_rows($result2) or die(mysql_error());

if ($_POST['get_parts'] == "Display Parts")
{
$category_name = mysql_result($result2,0,6);
?>
<center>
<h2><?php echo $category_name ?></h2>
<table border="1" cellpadding="5">
<tr>
<th>Part Number</th>
<th>Years</th>
<th>Description</th>
<th>Price</th>
<th>Picture</th>
</tr>
<?
}
else
{
echo("<p class=\"text\" align=\"center\">There was an
error.</p>");
}

for ($i = 0;$i < $num_results2; $i++)
{
$row2 = mysql_fetch_array($result2);

if (!$row2)
{
echo "No parts found for this category.";
}
?>
<tr>
<td><?php echo $row2["part_number"]?></td>
<td><?php echo $row2["years"]?></td>
<td><?php echo $row2["description"]?></td>
<td>$<?php echo $row2["price"]?></td>
<?php
if (strlen($row2["graphic"]) 0)
{
?>
<td align="center"><img src="graphics/<?php echo
$row2["graphic"].".jpg"?>" border="0"></td>
<?php
}
else
{
?>
<td>No picture available</td>
<?php
}
?>
</tr>
<?
}
?>
</table>
<br>
<br>

<?
}
}
}
do_html_footer();
?>
Are you sure it's the last row you're not getting, and not the first?

I think your problem is you're trying to fetch the category with
mysql_result, then using mysql_fetch_array() to get the data.

From the PHP manual on mysql_result():

"Calls to mysql_result() should not be mixed with calls to other
functions that deal with the result set."

Perhaps you should get your category description after you fetch the
contents for the first row.

Another option would be to run a query to fetch just the category
description, rather than return it in every single row (just to save a
query). At some point the extra data returned will take longer than a
second select statement.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '06 #6
Steve wrote:
>
>I then use mysql_result() to get the category name from the first row
(since it will be the same in all rows). The problem I'm having is
that when I use mysql_result(), the data from the last row in the
result set is not displayed, even though the HTML for that row is
written out, and the "No parts found for this category" message is
written to the page (see below).


I went back and looked again, and discovered that it is actually the
first row in the returned data that is not displayed, not the last row.
So it looks like what's happening is that mysql_result() is removing the
data from the first row (the row I'm reading the get the category name),
but it looks like the row is actually there when mysql_fetch_array() is
extracting the data, hence the empty row displayed in the table.

Steve
Ah, now I see this update :-)

See my update above. No, you can't mix mysql_result() and
mysql_fetch_array() successfully.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '06 #7
Another option would be to run a query to fetch just the category
description, rather than return it in every single row (just to save a
query). At some point the extra data returned will take longer than a
second select statement.
I thought about doing that, but it seemed more efficient to try to get everything with one query.

Thanks for your help.

Steve
Sep 11 '06 #8
Steve wrote:
>Another option would be to run a query to fetch just the category
description, rather than return it in every single row (just to save a
query). At some point the extra data returned will take longer than a
second select statement.


I thought about doing that, but it seemed more efficient to try to get
everything with one query.

Thanks for your help.

Steve
Yep, you can do it in one query. Something like:

$category = null;

....

while ($row2 = mysql_fetch_array($result2)) {
if ($category == null)
$category = $row2['Category'];

... Rest of processing

}

BTW - this construct is better, IMHO, then getting the number of rows
and using a for loop.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 11 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lozarythmic | last post by:
Hello all, I'm having a slight problem with a script i created at work. All was fine until one day i decided i'd work on it at home, so i set up the same apache server (2.0.49) and php (4.3.3)...
6
by: bettina | last post by:
The program works local ok. The data were found in the datenbank and display by the program. When I wanted to test my program online, I get the following message: mysql_result(): supplied argument...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
3
by: monomaniac21 | last post by:
hi all im querying a db for two rows which are always returned. how can i reference each row and output its contents without using a while loop. ive tried: $row = mysql_fetch_array($result);...
3
by: WiseG1rly | last post by:
Hey everyone! I am completley new and I will start off by saying that I am not a programmer - figuring out this search took so long! I am debugging now and would appreciate any help :) ...
12
by: mrking | last post by:
Man, I need help. :D I have a database and the table has 4 fields. ident, chroma, hue, value I have the script to display ALL the data into the table. But what I am looking to do is to set a...
6
by: ashraf02 | last post by:
can some one please help me i am getting this error but i cant seem to figure out wat is wrong on line 3 everything looks fine <?php $conn = mysql_connect("localhost","root","") or die...
6
by: BOMEz | last post by:
So i've recently been starting to program PHP in an object oriented way, but I'm running into some difficulties in from a design stand point and from an object oriented stand point: Issue 1: In my...
5
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.