好的,所以我还是有点新手我有一个名为字段的字段,当创建一个新的字段来存储数据库中的值时。我想要做的是将数据带回来,并将会话数组命名为字段名称,以便它们自动生成。
这是我的代码
session_start();
$_SESSION['fields'][$y]=$row['fields'];
echo $_SESSION['fields'][$y];
echo"";
$f= $_SESSION['fields'][$y];
echo $f;
echo"";
$_SESSION[$f][$y]=$row[$f];
echo $_SESSION[$f][1]; ; $y++;
答案 0 :(得分:1)
嗨我有同样的问题..我通过以下方式解决了问题:
在函数wpdb中使用wordpress build 不要使用session_start,因为wordpress触发session_start();
有任何问题请与我联系:pimenteljchristopher@gmail.com
global $wpdb;
$table_name = $wpdb->prefix . "table_name";
$result = $wpdb->get_results ( "SELECT * FROM $table_name" );
foreach ( $result as $print ) {
echo $print->id;
echo $print->fields1;
echo $print->fields2;
}
答案 1 :(得分:0)
如果我理解正确,您只想将数据库中的行返回到会话中,以便稍后可以使用它来构建HTML元素:
session_start();
$mysqli = new mysqli('host', 'user', 'pass', 'database');
if($stmt = $mysqli->prepare('SELECT * from FIELDS where 1')):
$stmt->execute();
$fields = $stmt->get_result(); //so we can use fetch array and simplify this process
while($row = $stmt->fetch_array(MYSQLI_ASSOC)): //foreach result set, return an associative array
$_SESSION['fields'][] = $row; //push the array into your fields session
endwhile;
$stmt->close(); //close your statement
endif;
$myslqi->close(); //drop the mysqli connection, we're done
print_r($_SESSION['fields']); //lets see what fields looks like now
$ret = '';
foreach($_SESSION['fields'] as $row): //foreach row in sessions
$ret .= '<div>'; //parent container for all fields in this row
foreach($row as $idx => $field): //foreach field in the row## Heading ##
$ret .= '<span>'. $field . '</span>'; //create a div element to contain the field
endforeach;
$ret . = '</div>'; //close the parent container
endforeach;
echo $ret; //returned the constructed HTML for the fields