Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-07-09
13
1,513 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

861 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