Warning: mssql_result() [function.mssql-result]: Bad column offset specified in

I have no idea why I would be getting this error

Warning: mssql_result() [function.mssql-result]: Bad column offset specified in c:\Inetpub\wwwroot\assist_manager\inventorydata.php on line 79

Warning: mssql_result() [function.mssql-result]: Bad column offset specified in c:\Inetpub\wwwroot\assist_manager\inventorydata.php on line 84

Line 79
    $m .= mssql_result($resulta, $counter, $key);
Line 84
$devicecounter = $devicecounter + mssql_result($resulta, $counter, $key);

It populates my data and then just stops
<?php include('conn.php') ?>  
<?php include('open.php') ?>  
<html>
<head>
	<script type="text/javascript" src="sortable.js"></script>
</head>
<body>
 
<?php
 
$user = $_SERVER['LOGON_USER'];
$usernew = substr($user, 10);
 
 
//declare the SQL statement that will query the database
 
$query = "select Distinct m.ID, m.description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' ";
 
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();
 
 
mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'id') ."'\n";
        $b[] = "<td><font face='verdana' size='1'><b>".mssql_result($result, $i, 'Description')."</b></font></td>"."\n";;
        $c[] =  mssql_result($result, $i, 'id');
 
}
 
$querya = "SELECT l.Location ," . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
  
$resulta = mssql_query($querya);
$ia = mssql_num_rows($resulta);
 
 
$ia = $ia -1;
$counter = 0;
$width = 350;
$m="";
 
while ( $counter <= $ia ) {
$countera = 0;
$devicecounter = 0;
$m .= '<tr>';
$m .= '<th><font face="verdana" size="1">';
$m .= mssql_result($resulta, $counter, 'location');
$m .= '</font></th>';
    $m .= "\n";
foreach($c as $key){
    $m .= '<td align="center"  width ="50"><font face="verdana" size="1">';
    $m .= '<a href="inventory2.php?location=';
    $m .= mssql_result($resulta, $counter, 'location');
    $m .= '&device=';
    $m .= mssql_result($result, $countera, 'Description');
    $m .= '">';
    $m .= mssql_result($resulta, $counter, $key);
    $m .= '</a>';
    $m .= '</font></td>';
    $m .= "\n";
$countera = $countera +1;
$devicecounter = $devicecounter + mssql_result($resulta, $counter, $key);
}
    $m .= '<td align="center"><font face="verdana" size="1">';
    $m .= $devicecounter;
    $m .= '</font></td>';
    $m .= '</tr>';
    $m .= "\n";
    $m .= "\n";
$counter = $counter +1;
}
$width = $width  + ($countera  * 50);
echo '<center>';
echo "\n";
echo '<Table class="sortable" border="1" width ="'.$width.'"';
echo "\n";
echo '<tr>';
echo '<th align="center" width ="300"><font face="verdana" size="1"><b>Location</b></font></th>';
echo "\n";
echo implode($b);
echo '<th align="center" width ="50"><font face="verdana" size="1"><b>Total</b></font></th>';
echo '</tr>';
echo "\n";
echo "\n";
echo  $m;
echo '</Table>';
echo "\n";
echo '</center>';
echo $counter;
 
//close the connection
mssql_close($dbhandle);
?>
</body>
</html>

Open in new window

adamn123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

profyaCommented:
There is a problem with the field: location
0
adamn123Author Commented:
Sorry I don't understand.
I would 3/4 of the way and stuffs
it does populate data and then just stops so it is reading the field
0
profyaCommented:
Means of code visualization is required, please add this:
echo "<pre>", print_r($key) , "<pre>";
to see what this array holds. Key array should contain field names, since the third argument of mssql_result method is the fieldname or number. You may need to revise this line:
$c[] =  mssql_result($result, $i, 'id');
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

adamn123Author Commented:
I added echo "<pre>", print_r($key) , "<pre>"; at the bottom under

echo '</center>';
echo "<pre>", print_r($key) , "<pre>";

//close the connection
mssql_close($dbhandle);

It outputs
321
0
profyaCommented:
I am sorry, I meant to print_r ($c);
just before the line (45 I think):
$querya = "SELECT l.Location ," . implode(", ", $a) .......

Because this array contains the field numbers $key represents. Send me the output.
0
adamn123Author Commented:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 10 [6] => 15 [7] => 16 [8] => 17 [9] => 18 [10] => 19 [11] => 20 [12] => 21 [13] => 22 [14] => 23 [15] => 25 [16] => 26 [17] => 27 [18] => 28 [19] => 31 [20] => 32 )
0
adamn123Author Commented:
I get outputs for [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 10 [6] => 15 [7] => 16 [8] => 17 [9] => 18 [10] => 19 [11] => 20 [12] => 21

It dies giving me resaults for
[13] => 22 [14] => 23 [15] => 25 [16] => 26 [17] => 27 [18] => 28 [19] => 31 [20] => 32
0
profyaCommented:
As you can see now from the result, 32 is not a valid field index, from the query I see, your fields maybe 5 or six, that's why the error happens may be in the fifth round of the loop. Look at 5] => 10 [6] => 15 [7] => 16 [8] => 17 which tells that $key number 5 is 10 and number 6 is 15 and so forth.

If you have five fields then $key value must be between 1 and 5. We need to change the way $c is filled with. Now comment this line:
//$c[] =  mssql_result($result, $i, 'id');
and add the code attached just after the closing bracket of the loop.
Your $c should contain a number between 1 and the number of $a elements +1
for($i=0; i<=count($a); $i++)
{
	$c[]=$i+1;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adamn123Author Commented:
Almost have it
It is printing the data but I am getting

Warning: mssql_result() [function.mssql-result]: Bad row offset (21) in c:\Inetpub\wwwroot\assist_manager\inventorydata.php on line 81

Warning: mssql_result() [function.mssql-result]: Bad column offset specified in c:\Inetpub\wwwroot\assist_manager\inventorydata.php on line 83

Warning: mssql_result() [function.mssql-result]: Bad column offset specified in c:\Inetpub\wwwroot\assist_manager\inventorydata.php on line 88
<?php include('conn.php') ?>  
<?php include('open.php') ?>  
<html>
<head>
	<script type="text/javascript" src="sortable.js"></script>
</head>
<body>
 
<?php
 
$user = $_SERVER['LOGON_USER'];
$usernew = substr($user, 10);
 
 
//declare the SQL statement that will query the database
 
$query = "select Distinct m.ID, m.description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew'  ";
 
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();
 
 
mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'id') ."'\n";
        $b[] = "<td><font face='verdana' size='1'><b>".mssql_result($result, $i, 'Description')."</b></font></td>"."\n";;
 
//$c[] =  mssql_result($result, $i, 'id');
 
}
for($i=0; $i<=count($a); $i++)
{
	$c[]=$i+1;
}
$querya = "SELECT l.Location ," . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
  
$resulta = mssql_query($querya);
$ia = mssql_num_rows($resulta);
 
 
$ia = $ia -1;
$counter = 0;
$width = 350;
$m="";
 
while ( $counter <= $ia ) {
$countera = 0;
$devicecounter = 0;
$m .= '<tr>';
$m .= '<th><font face="verdana" size="1">';
$m .= mssql_result($resulta, $counter, 'location');
$m .= '</font></th>';
    $m .= "\n";
foreach($c as $key){
    $m .= '<td align="center"  width ="50"><font face="verdana" size="1">';
    $m .= '<a href="inventory2.php?location=';
    $m .= mssql_result($resulta, $counter, 'location');
    $m .= '&device=';
    $m .= mssql_result($result, $countera, 'Description');
    $m .= '">';
    $m .= mssql_result($resulta, $counter, $key);
    $m .= '</a>';
    $m .= '</font></td>';
    $m .= "\n";
$countera = $countera +1;
$devicecounter = $devicecounter + mssql_result($resulta, $counter, $key);
}
    $m .= '<td align="center"><font face="verdana" size="1">';
    $m .= $devicecounter;
    $m .= '</font></td>';
    $m .= '</tr>';
    $m .= "\n";
    $m .= "\n";
$counter = $counter +1;
}
$width = $width  + ($countera  * 50);
echo '<center>';
echo "\n";
echo '<Table class="sortable" border="1" width ="'.$width.'"';
echo "\n";
echo '<tr>';
echo '<th align="center" width ="300"><font face="verdana" size="1"><b>Location</b></font></th>';
echo "\n";
echo implode($b);
echo '<th align="center" width ="50"><font face="verdana" size="1"><b>Total</b></font></th>';
echo '</tr>';
echo "\n";
echo "\n";
echo  $m;
echo '</Table>';
echo "\n";
echo '</center>';
 
//close the connection
mssql_close($dbhandle);
?>
</body>
</html>

Open in new window

0
adamn123Author Commented:
I did this
echo implode($c);
and I get
12345678910111213141516171819202122
I should only have a total of 21
I am 1 to many that is why I am getting the errors
0
profyaCommented:
Ok, this this line:
for($i=0; i<=count($a); $i++)

to

for($i=0; i<count($a); $i++)

i<= become i<

I hope it works fine after this.
0
adamn123Author Commented:
Great work my friend!!!!!!!!!!!
0
profyaCommented:
You are welcome :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.