php foreach loop only returns first character of e

2020-04-14 16:42发布

I have the below code. It is only returning the first charater of a string.

    $conn = Mage::getSingleton('core/resource')->getConnection('connection_write');
$str = 'something to search for';
$fields = 'content_field1, content_field2, content_field3, content_field4';
$idFields = 'id_field1, id_field2, id_field3, id_field4';
$tables = 'table1, table2, table3, table4';
$table = explode(', ', $tables);
$field = explode(', ', $fields);
$rowId = explode(', ', $idFields);

$i=1;
while ($i<4) {
    $f = $field[$i];
    $id = $rowId[$i];
    $sql = $conn->select()->from($table[$i], array($f, $id))->where($f . " LIKE ?", '%' . $str . '%');
    $result = $conn->fetchRow($sql);

    foreach ($result as $row) {
    var_dump($row[$id]);
    }

    $i++;
}

However, if I use var_dump($row); the entire string from both the id fields and the content fields are outputted.

Can anyone explain to me what I am doing wrong?

Thanks in advance.

4条回答
冷血范
2楼-- · 2020-04-14 17:18

However, if I use var_dump($row); the entire string from both the id fields and the content fields are outputted.

well, why do you use var_dump($row[$id]); which exactly equals to "take $id'th char from $row string" then?

查看更多
我只想做你的唯一
3楼-- · 2020-04-14 17:22
foreach ($result as $row) {

$result is a list of rows you selected from the table. Each $row is a string. $row[$id] simply selects the $id'th character from this row string.

查看更多
够拽才男人
4楼-- · 2020-04-14 17:37

I believe the problem has to do with the fact that $result is an array (possibly an associative array) of strings. (I'm not familiar with Mage, but I'm assuming that fetchRow() returns only a single row and not all of your rows.)

Therefore, what's happening is you've probably got something like this:

$result = array("tom","dick","harry");

When you run foreach($result as $row), $row looks like "tom", "dick", and "harry" on each iteration.

Because $row is a string, calling $row[$id] makes PHP attempt to get get a single character from the $row string. Because it's expecting $id to be an integer in this case, it's probably interpreting "id_field1" as 0, which will end up returning the first character of your string.

I'm not exactly sure what you're trying to get back, but most likely this can be solved by changing your foreach section to be something like...

foreach ($result as $resultColumn) {
   var_dump($resultColumn);
}
查看更多
贼婆χ
5楼-- · 2020-04-14 17:40

->fetch() is not needed when fetching data by using foreach statement like in your code (Please see examples below). If you use ->fetch() unnecessarily, this situation often occurs (I experienced this too..)

foreach statement and while statement have the following relationship. Please consider rewriting the code, referring to this:

<?php
$stmt = $pdo->query('SELECT * FROM Entry');

while ($row = $stmt->fetch()) {
  echo $row['title'], $row['content'];
}
//this while statement above is equivalent to the foreach statement below

foreach ($stmt as $row) {
//->fetch() is not needed for foreach statement!
  echo $row['title'], $row['content'];
}
查看更多
登录 后发表回答