Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-09
13
Medium Priority
?
1,602 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 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