まず、私はことを言うために強い感じ特にPDOStatement::fetchAll
があなたの望むものの大半を占めているので、PDOを使用する必要があります。 This tutorialは比較的まともです。
は、ここで再書き込みのビットだが、それはあなたがそれを望むすべて行います:第二のファイルで
// let yourself cache the connection. That will save you processing time
function getusers($connection, $dbname = DB_NAME)
{
if(!$connection)
{
return FALSE; // we can't do anything here.
}
// Selecting a db inside of a function can cause problems for others using
// your code (if you use $connection as a parameter). Instead explicitly
// use the database
$query = "SELECT user_name FROM `$dbname`.`userinfo`";
// you may want to make this cause a return of something other than FALSE,
// but you definitely want a way to have some form of early escape here.
$result = mysql_query($query, $connection) or return FALSE;
$final = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// append the current user_name to the $final array
$final[] = $row['user_name'];
}
// you'll need to close the connection elsewhere.
return $final;
}
:
include('usernames.php');
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$users = getusers($conn, $dbname) or die("MySQL Error: " . mysql_error());
// foreach... as... will loop through your array automatically.
foreach($users as $user)
{
// $user refers to each user_name fetched in getusers
getTax($user, $connection, $dbname);
}
// and here is where the cost saving of caching your connection is found:
function getTax($user, $connection, $dbname = DB_NAME)
{
if(!$connection)
{
return FALSE; // we can't do anything here.
}
$result = mysql_query("SELECT * FROM TAX_TABLE WHERE USER_NAME = '$user'",
$connection) or return FALSE;
// continue as normal
}
感謝を。このエラーが発生しました。「getusers()を再宣言できません。 – karto
関数getusers()が何度も宣言されています。コードを確認してください。 – Ostin