Solved

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

Posted on 2009-07-09
13
1,502 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:adamn123
  • 7
  • 6
13 Comments
 
LVL 14

Expert Comment

by:profya
ID: 24815701
There is a problem with the field: location
0
 

Author Comment

by:adamn123
ID: 24815799
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
 
LVL 14

Expert Comment

by:profya
ID: 24815898
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:adamn123
ID: 24815939
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
 
LVL 14

Expert Comment

by:profya
ID: 24815993
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
 

Author Comment

by:adamn123
ID: 24816123
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
 

Author Comment

by:adamn123
ID: 24816190
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
 
LVL 14

Accepted Solution

by:
profya earned 125 total points
ID: 24816250
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
 

Author Comment

by:adamn123
ID: 24816584
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
 

Author Comment

by:adamn123
ID: 24816717
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
 
LVL 14

Expert Comment

by:profya
ID: 24816837
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
 

Author Comment

by:adamn123
ID: 24817009
Great work my friend!!!!!!!!!!!
0
 
LVL 14

Expert Comment

by:profya
ID: 24817021
You are welcome :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question