Page 1 of 1

[SOLVED] can anyone tells me what is wrong with [] ?

Posted: Wed Oct 13, 2004 2:10 am
by rei
have been trying out for very long...

The following one goes well, but when i changed to read $sql statements from included file,
Fatal error: [] operator not supported for strings in /home/apache156/htdocs/cgitools/bargraph.php appears..

Source that works well:

Code: Select all

$con = mysql_connect($ServerName,$UserName,$PassWd);
mysql_select_db($DBName,$con);

$sql = "SELECT month(date_st) AS `mth` , SUM(cnt) AS `sum_cnt`
      FROM data_tbl
      WHERE year(date_st)=2004
      GROUP BY month(date_st)";

print "<br>sql=$sql<br>";


$list = mysql_query($sql, $con);

while($row = mysql_fetch_array($list)){
    $data[]=$row[sum_cnt];
    $leg[]=$row[mth];
}
Source that doesnt work:

Code: Select all

$con = mysql_connect($ServerName,$UserName,$PassWd);
mysql_select_db($DBName,$con);

include("if.php");

print "<br>sql=$sql<br>"; //$sql contains the correct statement

$list = mysql_query($sql, $con);

while($row = mysql_fetch_array($list)){
    $data[]=$row[sum_cnt];
    $leg[]=$row[mth];
}
my if.php:

Code: Select all

<?php
    if($data=="wages" && $period=="annual"){ //$data & $period was defined in the above script
        $sql = "
        SELECT month(date_st) AS `mth`, SUM(cnt) AS `sum_cnt`
        FROM data_tbl
        WHERE ope=1 AND
              account!=03 AND
              year(date_st)=$a_year
        GROUP BY month(date_st)";

    }
?>
The reason why i'm trying to read $sql from file is because i have too many if statements to be used in several php script. This might save time writing out the if statements.

Posted: Wed Oct 13, 2004 2:19 am
by Steveo31
Maybe if you define them as an array first...?

Posted: Wed Oct 13, 2004 2:31 am
by vigge89
small thing, are you using constants? if you're not, change those lines

Code: Select all

$data[]=$row[sum_cnt];
    $leg[]=$row[mth];
... into this ....

Code: Select all

$data[]=$row['sum_cnt'];
    $leg[]=$row['mth'];

wrapping lines

Posted: Wed Oct 13, 2004 2:43 am
by phpScott
I know that some times I have run into trouble when wrapping my lines like you did for the query.

try

Code: Select all

<?php
    if($data=="wages" && $period=="annual"){ //$data & $period was defined in the above script
        $sql = "SELECT month(date_st) AS `mth`, SUM(cnt) AS `sum_cnt` ";
        $sql.="FROM bs_billinglog_daily ";
        $sql.="WHERE ope=1 AND ";
        $sql.="account!=03 AND ";
        $sql.="year(date_st)=$a_year ";
        $sql.="GROUP BY month(date_st)";

    }
?>
and please try what vigge89 suggested.
other wise take your query that you are echoing out and run straing against the db as it might help tell you what might be happening with your query.

Posted: Wed Oct 13, 2004 2:53 am
by rei
vigge89,

i've tried that too, but to no avail..

phpScott,

$sql has no problem
i tried to echoed it and i got the correct sql statement..

only other []

Posted: Wed Oct 13, 2004 3:28 am
by phpScott
the only place that you use [] is here.

Code: Select all

<?php
while($row = mysql_fetch_array($list)){
    $data[]=$row[sum_cnt];
    $leg[]=$row[mth];
}
?>
try the following

Code: Select all

<?php
$a=0;
while($row = mysql_fetch_array($list)){
    $data[$a]=$row[sum_cnt];
    $leg[$a]=$row[mth];
$a++;
}
?>
as it might be getting stumped on the empty brackets.
I know you should be able to do what you are doing but it is just another possibility.
Does it give you a line number with the error?

Posted: Wed Oct 13, 2004 3:31 am
by rei
phpScott,
thanks for that.
i've found out where goes wrong!
notice that i have the variable of $data in if.php
and yet i used it as an array $data[] in my main php script.
There's the problem !

Thanks a lot for all suggestions and helping me to solve my problem!
Gave me encouragement to go on ^^
So sorry to take up so much of your time with my careless mistake.. (T_T)

Posted: Wed Oct 13, 2004 4:54 am
by phpScott
been there, done that :oops:
sometimes at work when we can't get something to work we will get another programmer to look over our shoulder as we run through what is wrong. More often then not they will notice something like that, or as we are discribing the problem it will leap out at us. That person that gets to be the 'cardboard programmer' for the day.

glad to hear that you will keep plugging away at it.

if you need more help that is what we are here for.

good luck.