dlyles Posted November 29, 2006 Share Posted November 29, 2006 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 36Here'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 More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 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 More sharing options...
dlyles Posted November 29, 2006 Author Share Posted November 29, 2006 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 More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 Post the code you're using.Ken Link to comment https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132224 Share on other sites More sharing options...
dlyles Posted November 29, 2006 Author Share Posted November 29, 2006 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 More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 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 More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 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 More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 ken in the first query he code he showed us it was not gettign the info from the first query it was only getting info from second one Link to comment https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132241 Share on other sites More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 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 More sharing options...
dlyles Posted November 29, 2006 Author Share Posted November 29, 2006 GREAT. Thanks guys. Unfortunately I'm a complete .php newbie used to working with .asp but now on a linux server. So bare with me as I may be posting more questions. Any ideas on the best online references and php books? Link to comment https://forums.phpfreaks.com/topic/28876-mysql_function-error/#findComment-132258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.