2014-02-17 30 views
1

好吧,所以我还是有点新的这个我有一个字段叫做字段,当一个新的字段被创建,将值存储在数据库中。我想要做的是将数据返回并将会话数组命名为字段的名称,以便它们自动生成。如何在mysql中使用变量来获取行数据?

这里是我的代码

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

当我回声$ F IT回声不过,我想它不会读取该行 – user3317235

+1

哪里是“MySQL的”字段名? – TimWolla

+0

if(mysqli_connect_errno()) echo'无法连接到MySQL:'。 mysqli_connect_error(); } $ result = mysqli_query($ con,'SELECT * FROM customers'); while($ row = mysqli_fetch_array($ result)) { while($ row = mysqli_fetch_array($ result)) { – user3317235

回答

1

您好我有同样的问题..我的解决它:使用WordPress

建立WPDB中不 使用session_start因为WordPress的触发器在session_start()函数;

任何问题请联系我:[email protected]

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

我想要做的是我们的数据从字段行到从其他行选择数据ie .... $ fieldname = $ row ['fieldnames'];然后我想用$ row = ['$ fieldname'];来调用更多的数据; – user3317235

相关问题