我遇到了一個很奇怪的問題。陣列在php中不能正常工作
我有一個函數,它打印來自MySql的給定數據庫表的列名稱。
function getColoumn() {
//replace it with your host normally it could be localhost
$hostname='localhost';
//mysql user name
$username='admin_datauser';
//mysql user password
$password='iCoq4KrJM8';
//connect to the mysql server
$ss = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
//select a database on the mysql server
//please change as you like the database name
mysql_select_db('admin_data');
//SHOW COLUMNS FROM TABLENAME
$query=mysql_query('SHOW COLUMNS FROM user') or die(mysql_error());
foreach($fields as $key=>$field){
echo '"'.$field->Field.'", '; // print each field name
}
}
輸出類似於如下─
"id", "first_name", "last_name", "email", "address", "country", "city", "state", "phone_number", "fax", "image", "datecreated", "dateupdated", "Company", "token",
我有另一個數組變量在當前的靜態值就像如下─
$searchArray = array("first_name","last_name","email","address","country","city","state","phone_number","fax","image");
我需要使用該函數的變量在這$ searchArray。我試着在arrya中調用這個變量,但它不能以任何方式工作。
請幫忙嗎?
請不要使用[mysql_'數據庫擴展](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),它 已棄用(已不存在永遠在PHP7) 特別是如果你只是學習PHP,花你的精力學習'PDO'數據庫擴展。 [從這裏開始](http://php.net/manual/en/book.pdo.php) – RiggsFolly
運行查詢並創建結果集的'mysql_query()'之後。你必須從結果集中讀取類似於'$ fields = mysql_fetch_object()'的行;''通常用'while(){}'循環寫入 – RiggsFolly
這個函數看起來不應該起作用。 '$ fields'是未定義的,除非我遺漏了一些東西。 –