2

I use the following function to select data from a MYSQL database:

if (!function_exists('select_array_where')) {
function select_array_where($what, $from, $where){

$mysqli = new mysqli("host", "user", "password", "db");
if ($mysqli->connect_errno)
{
    echo "<br>Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "<br>";
}

$result = $mysqli ->query("SELECT $what FROM $from WHERE $where");

$array = Array();

while ($row = mysqli_fetch_assoc($result)) {
  $array[] =  $row[$what];
}

print_r ($array);
return $array;
}
}

Which works perfectly well if $what has only one value:

$name_array = Array();
    $name_array =& select_array_where("name", "inter_company", "transaction_type='interest'");

But it does not work in the following case:

  $company_int_array = array();
  $company_int_array =& select_array_where("amount, perc", "inter_company", "transaction_type='interest'");

I've a feeling it relates to punctuation and have tried movies comma's, " and ' around, in and out but nothing helps...

Anyone an idea what I'm doing wrong?

11
  • Anyway, could you please copy the error message you get? Adding a try..catch would be a good idea also Commented Jan 2, 2016 at 17:24
  • Notice: Undefined index: 'amount', 'perc' Commented Jan 2, 2016 at 17:26
  • 1
    "amount, perc", these would need to be separated "amount", "perc", Commented Jan 2, 2016 at 17:27
  • Doesn't work, believe that I can only have three things between " and " because of the way the function is defined ($what, $from and $where) Commented Jan 2, 2016 at 17:31
  • 1
    Try to use ticks ` around each word the amount, perc with your existing quotes, or wrapping the whole thing. Commented Jan 2, 2016 at 17:36

2 Answers 2

2

Your issue is that

 $array[] = $row[$what];

is evaluating to

 $array[] = $row['amount perc'];

which is invalid as you don't have a single column named 'amount, perc'.

The simplest solution is to change to

$array[] = $row;

which is equivalent to doing

$array[] = array('amount'=>$row['amount'], 'perc'=>$row['perc']);

but does not require you to know/specify the actual column names/keys

Sign up to request clarification or add additional context in comments.

1 Comment

Luvin' that green ;-) Good job Sean.
-1

Well, you have to replace the line: $array[] = $row[$what]; With the lines:

 $Whatsarr = explode ($what);
 Foreach ($what as $col)
 $aux[$col] = $row [$col];
 $array []= $aux:

5 Comments

How does this address the OP's issue on this line - array[] = $row[$what];? That would evaluate to array[] = $row['amount', 'perc'];, which is invalid.
Correct, this doesn't help
I have fixed my answer
Your edit is closer, but this would create a new array element for each column, each time. So if you had 2 columns and 3 rows in the database, then instead of 3 array elements with 2 key/value pairs in each, you will have 6 array elements with 1 key/value pair in each.
Did you execute it to know this?! Notice that the Foreach loop includes only the first line after it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.