Php 如何在mysql中使用变量获取行数据?

Php 如何在mysql中使用变量获取行数据?,php,Php,好的,所以我还是有点陌生,我有一个字段叫做fields,当创建一个新字段时,它将值存储在数据库中。我要做的是将数据带回来,并命名会话数组字段的名称,以便它们自动生成 这是我的密码 session_start(); $_SESSION['fields'][$y]=$row['fields']; echo $_SESSION['fields'][$y]; echo""; $f= $_SESSION['fields'][$y]; echo $f; echo""; $_SESSION[$f

好的,所以我还是有点陌生,我有一个字段叫做fields,当创建一个新字段时,它将值存储在数据库中。我要做的是将数据带回来,并命名会话数组字段的名称,以便它们自动生成

这是我的密码

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++;

嗨,我也有同样的问题。。我可以通过以下方式解决它:

使用wordpress内置函数wpdb 不要使用会话启动,因为wordpress触发会话启动

如有任何问题,请与我联系: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;
}

如果我理解正确,您只需要将该行从数据库返回到会话中,以便以后可以使用它来构造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

当我回显$f时,它回显我想要的字段名,但是它不会获取行MySQL在哪里?如果mysqli_connect_errno{echo'无法连接到MySQL:'.mysqli_connect_error;}$result=mysqli_query$con,'SELECT*FROM customers';虽然$row=mysqli\u fetch\u array$result{而$row=mysqli\u fetch\u array$result{您是否希望使用数据库来存储会话数据?请看一看,我试图做的是使用字段行中的数据来选择其他行中的数据,即…$fieldname=$row['fieldnames'];然后我想调用更多数据,比如$row=['$fieldname'];