views:

114

answers:

6

It must be Monday, the heat or me being stupid (prob the latter), but for the life of me I cannot get a simple php function to work.

I have a simple query

$sql = mysql_query("SELECT * FROM table WHERE field_name = '$input'");

Which I want to run through a function: say:

function functionname($input){

    global $field1;
    global $field2;
    $sql = mysql_query("SELECT * FROM table WHERE field_name = '$input'");
    while($row = mysql_fetch_array($sql)) :
    $field1[] = $row['field1'];
    $field2[] = $row['field2'];
    endwhile;   
    mysql_free_result($sql);

}

So that I can call the function in numerious places with differeing "inputs". Then loop through the results with a foreach loop.

Works fine the first time the function is called, but always gives errors there after.

As said "It must be Monday, the heat or me being stupid (prob the latter)".

Suggestions please as I really only want 1 function to call rather than rewrite the query each and every time.

This is the error message

Fatal error: [] operator not supported for strings in C:\xampp\htdocs\functions.php on line 270
A: 

To understand your issue, we need the error, however, are you sure you are going about this in the right way?

Why do you need to call the function multiple times if you are just changing the value of the input field?

You could improve your SQL statement to return the complete result set that you need the first time.. i.e. SELECT * FROM table GROUP BY field_name;

Not sure if that approach works in your scenario, but in general you should aim to reduce the number of round trips to your database.

A: 

I don't know, i right or not. But i advise to try this:

function functionname($input){

    global $field1;
    global $field2;
    $sql = mysql_query("SELECT * FROM `table` WHERE `field_name` = '" . $input . "'");
    while($row = mysql_fetch_assoc($sql)) :
    $field1[] = $row['field1'];
    $field2[] = $row['field2'];
    endwhile;   
    mysql_free_result($sql);

}
Alexander.Plutov
Thanks Alex, but same result cannot loop through the arrays other than once ; e.g. <?php foreach($field1 as $field1): echo field1; endforeach; ?>
<?php foreach($field1 as $field) { echo $field; } ?>
Alexander.Plutov
+1  A: 

The problem is that you so called arrays are strings!

global $field1;
global $field2;
var_dump($feild1,$feild2); //Will tell you that there strings

Read the error properly !

[] operator not supported for strings

And the only place your using the [] is withing the $feild - X values

GLOBAL must work because the error is telling you a data-type, i.e string so they must have been imported into scope.


another thing, why you selecting all columns when your only using 2 of them, change your query to so:

$sql = mysql_query("SELECT feild1,feild2 FROM table WHERE field_name = '$input'");

another thing is that your using mysql_fetch_array witch returns an integer indexed array, where as you want mysql_fetch_assoc to get the keys.

while($row = mysql_fetch_assoc($sql)) :
    $field1[] = $row['field1'];
    $field2[] = $row['field2'];
endwhile;

What I would do

function SomeFunction($variable,&$array_a,&$array_b)
{
    $sql = mysql_query("SELECT field1,field2 FROM table WHERE field_name = '$variable'");
    while($row = mysql_fetch_assoc($sql))
    {
        $array_a[] = $row['field1'];
        $array_b[] = $row['field2'];
    }
    mysql_free_result($sql);
}

Then use like so.

$a = array();
$b = array();
SomeFunction('Hello World',&$a,&$b);
RobertPitt
They have to be "arrays" to be able to run a loop - any suggestions then on how to get the array/s out of the function as global doesn't work
when you use globalization on variables inside a function any edits done on the values affect the variables outside directly, so aslong as the variables are in the root scope, you should be able to call the global in there!
RobertPitt
i know there strings because of this !! [] operator not supported for strings
RobertPitt
Updated with another error for the arrays witch might be causing it !
RobertPitt
So explain to me why php tells the user that `[] operator not supported for strings`?
RobertPitt
@Gordon, tell me about it, its not my spelling its my typing speed and lack of reviewing it, I just type fast and submit, Sorry about that then.
RobertPitt
+1  A: 

it seems that somewhere the $field1 or $field2 are converted to strings and you cant apply the [] to a string... i'd say that you have to do:

$field1 = array();
$field2 = array();

before the WHILE loop

pleasedontbelong
then that would make globalization pointless, he might aswell define them inside the function.
RobertPitt
yeap.. it's better to return the values and manage the global variable outside this function
pleasedontbelong
+1  A: 
function functionname($input){ 
    $sql = mysql_query("SELECT field1,field2 FROM table WHERE field_name = '$input'"); 
    $result = array('field1' => array()
                    'field2' => array()
                   );
    while($row = mysql_fetch_array($sql)) : 
       $result['field1'][] = $row['field1']; 
       $result['field2'][] = $row['field2']; 
    endwhile;    
    mysql_free_result($sql); 
    return $result;
} 
Mark Baker
Thanks Mark, like this way it's easy to anoted (see my comment to Col Shrapnel) hence you get my "vote" as they say.
+1  A: 

In my opinion, it's pretty unusual and even useless approach at all.
This function is too localized.
To make a general purpose function would be a way better.

<?
function dbgetarr(){
  $a = array();
  $query = array_shift($args);
  foreach ($args as $key => $val) {
    $args[$key] = "'".mysql_real_escape_string($val)."'";
  }
  $query = vsprintf($query, $args);

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbgetarr: ".mysql_error()." in ".$query);
    return FALSE;
  } else {
    while($row = mysql_fetch_assoc($res)) $a[]=$row;
  }
  return $a;
}

and then call it like this

$data = dbgetarr("SELECT field1,field2 FROM table WHERE field_name = %s",$input);
foreach ($data as $row) {
  echo $row['field1']." ".$row['field1']."<br>\n";
}
Col. Shrapnel