Jump to content

Mysql_function error


dlyles

Recommended Posts

I am having a problem getting the records from one records based on the records from another table. I'm getting the following warning:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 4 in /var/opt/data/rgbdata/apache/htdocs/inventory.php on line 36

Here's the code
[code]
$query="select * from parts";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$nquery="select * from groups where GroupID = '" .$row['GroupID']. "'";
$nresult=mysql_query($nquery);

$group=mysql_result($nresult,$i,"GroupName");
$model=mysql_result($result,$i,"ModelID");
$Part=mysql_result($result,$i,"PartName");
$amount=mysql_result($result,$i,"balance");


echo "<tr><td>$group</td><td>$model</td><td>$Part</td><td>$amount</td></tr>";

$i++;
[/code]
This is line 36 $group=mysql_result($nresult,$i,"GroupName")
Any help is greatly appreciated.
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/
Share on other sites

You should always check for mysql errors when doing mysql queries with generated queries. Also, I would use mysql_fetch_assoc() instead the method you're currently using since it is more efficient.

You're problem is that you're not getting the record from the first query to use in the second.

[code]<?php
$query="select * from parts";
$result=mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
while ($row = mysql_fetch_assoc($result) {

        $nquery="select * from groups where GroupID = '" .$row['GroupID']. "'";
        $nresult=mysql_query($nquery) or die("Problem with nquery: $nquery<br>" . mysql_error());
        $rw = mysql_fetch_assoc($nresult);
        $group = $rw['GroupName'];
        $model= $rw['ModelID'];
        $Part= $rw['PartName'];
        $amount= $rw['balance'];

        echo "<tr><td>$group</td><td>$model</td><td>$Part</td><td>$amount</td></tr>";
}
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132208
Share on other sites

Ok, thanks.  Now the problem is that it won't display the records from the first query.  The situation is as such.  table "groups" has the group name and groupID.  table "Parts" has part name and the respective GroupID.  I'm trying to display all the data in the "parts" table except instead of the GroupID, which is the related field in "parts" I want it to show the actual part name, which is in the "groups" table.  Am I making sense?
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132221
Share on other sites

Sorry, thought I did.
[code]
mysql_select_db("inventory",$db);
$query="select * from parts";
$result=mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
while ($row = mysql_fetch_assoc($result)) {

    $nquery="select * from groups where GroupID = '" .$row['GroupID']. "'";
        $nresult=mysql_query($nquery);
        $rw = mysql_fetch_assoc($nresult);

        $group = $rw['GroupName'];
        $model= $rw['ModelID'];
        $Part= $rw['PartName'];
        $amount= $rw['balance'];

        echo "<tr><td>$group</td><td>$model</td><td>$Part</td><td>$amount</td></tr>";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132233
Share on other sites

here try this
[CODE]
<?php
/*make sure u have ur database info here*/
mysql_select_db("inventory",$db);
$query="select * from parts";
$result=mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    $id = $row['GroupID'];
    $nquery="select * from groups where GroupID = '$id'";
    $nresult=mysql_query($nquery) or die("Problem with the query: $query<br>" . mysql_error());
    $rw = mysql_fetch_assoc($nresult);
    $group = $rw['GroupName'];
    $model= $rw['ModelID'];
    $Part= $rw['PartName'];
    $amount= $rw['balance'];
    echo "<tr><td>$group</td><td>$model</td><td>$Part</td><td>$amount</td></tr>";
}
?>
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132236
Share on other sites

My mistake.

change
[code]<?php
        $model= $rw['ModelID'];
        $Part= $rw['PartName'];
        $amount= $rw['balance'];
?>[/code]

to
[code]<?php
        $model= $row['ModelID'];
        $Part= $row['PartName'];
        $amount= $row['balance'];
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132237
Share on other sites

oh oh gotcha he wants part, model, and amount from the first section
[CODE]
<?php
/*make sure u have ur database info here*/
mysql_select_db("inventory",$db);
$query="select * from parts";
$result=mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    $id = $row['GroupID'];
    $nquery="select * from groups where GroupID = '$id'";
    $nresult=mysql_query($nquery) or die("Problem with the query: $query<br>" . mysql_error());
    $rw = mysql_fetch_assoc($nresult);
    $group = $rw['GroupName'];
    $model= $row['ModelID'];
    $Part= $row['PartName'];
    $amount= $row['balance'];
    echo "<tr><td>$group</td><td>$model</td><td>$Part</td><td>$amount</td></tr>";
}
?>
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132245
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.