全局通常被認爲是不好的做法。我不會在你高談闊論一番,但看看這篇文章:http://blog.case.edu/gps10/2006/07/22/why_global_variables_in_php_is_bad_programming_practice
可以使用超全局$GLOBALS
訪問已在全球範圍內(docs)中定義的任何變量。因此,在您的示例代碼中,僅使用$GLOBALS['mysql_db1']
就相當於具有行global $mysql_db1;
,然後使用$mysql_db1
。
我不能強調這個計劃有多糟糕(沒有強調)。在你開發的整個過程中,你可能都會很滿意,但可憐的可憐的Johnny Nextguy,如果你包含一個也使用全局變量的第三方腳本,可能會有代碼之神拯救你......並且存在變量衝突名。現在你在爲它而努力!
如上所述,您最好將數據庫功能封裝在一個類中。如果你使用靜態類,你仍然擁有全局變量的所有好處,沒有受到污染的範圍或覆蓋的危險。
這裏是一個示例數據庫類,隨着使用沿着:
// put this in a library file or some place that all scripts include
require_once('database_class.php');
$db = new db(array(
'host'=>'localhost'
'user'=>'db_user_name'
'password'=>'db_password',
'database_name'=>'my_database'
));
// now anywhere in code you want to use it
$array = db::getRows('SELECT id, name FROM users');
$field = db::getField('SELECT name FROM users WHERE id=10');
<?php
// database_class.php
class db {
static protected $resource_link = null;
function __construct($args=false) {
if ($args===false || !is_array($args))
return false;
if (
!isset($args['host']) ||
!isset($args['user']) ||
!isset($args['password']) ||
!isset($args['database_name'])
)
return critical_error('Missing database configuration data.');
self::$resource_link = @mysql_connect($args['host'],$args['user'],$args['password']);
if (!self::$resource_link)
return critical_error('Error connecting to database 2001. MySQL said:<br>'.mysql_error());
@mysql_select_db($args['database_name'], self::$resource_link);
return;
}
// return a single-dimmension array of fields as string
static public function getFields ($sql=false, $field=false) {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj) {
$res = array();
while ($this_row = mysql_fetch_array($query_obj)) {
if ($field !== false && isset($this_row[$field]))
$res[] = $this_row[$field];
else
$res[] = $this_row[0];
}
} // end :: if query object is not null
} // end :: if $sql is not false
return $res;
}
// return a single-dimmension array of fields as string with keyfield as key
static public function getKeyFields ($sql=false, $key_field='id', $list_field='id') {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj) {
while ($this_row = mysql_fetch_array($query_obj)) {
if (isset($this_row[$key_field]))
$res[$this_row[$key_field]] = $this_row[$list_field];
}
} // end :: if query object is not null
} // end :: if $sql is not false
return $res;
}
// return a single field as string from the first row of results
static public function getField ($sql=false) {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj) {
$this_array = mysql_fetch_array($query_obj);
if (is_array($this_array))
return $this_array[0];
} // end :: if query object is not null
} // end :: if $sql is not false
return $res;
}
// return a single row as an array
static public function getRow ($sql=false) {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj)
$res = mysql_fetch_assoc($query_obj);
} // end :: if $sql is not false
return $res;
}
// return an array of rows as arrays of strings
static public function getRows ($sql=false) {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
$res = array();
if ($query_obj) {
while ($this_row = mysql_fetch_assoc($query_obj)) {
$res[] = $this_row;
}
} // end :: if query object is not null
} // end :: if $sql is not false
return $res;
}
// return an array of rows as arrays of strings, using specified field as main array keys
static public function getKeyRows ($sql=false, $key_field='id', $include_key_in_results=true) {
$res = null;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj) {
$res = array();
while ($this_row = mysql_fetch_assoc($query_obj)) {
if (isset($this_row[$key_field])) {
$res[$this_row[$key_field]] = $this_row;
if ($include_key_in_results == false)
unset($res[$this_row[$key_field]][$key_field]);
} // end :: if checking for key field in result array
} // end :: while looping query obj
} // end :: if query object is not null
} // end :: if $sql is not false
return $res;
}
// do an update query, return true if no error occurs
static public function update ($sql=false) {
$res = false;
if ($sql!==false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj && strlen(mysql_error()) < 1)
$res = true;
}
return $res;
}
// do an insert query, return the auto increment ID (if there is one)
static public function insert ($sql=false) {
$res = null;
if ($sql !== false) {
$query_obj = mysql_query($sql, self::$resource_link);
if ($query_obj)
$res = mysql_insert_id(self::$resource_link);
}
return $res;
}
}
?>
我知道這些問題,並知道第三方mod將會發生衝突。我正在努力節省時間,而我正在寫東西,並發現自己嘆了口氣,每次我忘了添加全局功能。我完全忘記了全局變量,它可以通過這種方式訪問。謝謝你的答案。 – PsyKzz