从数组中获取Mysql字段

时间:2014-05-24 12:22:08

标签: php mysql

我有以下脚本来显示目录中的文件

<?PHP
# The current directory
$directory = dir("./");

# If you want to turn on Extension Filter, then uncomment this:
 $allowed_ext = array(".deb", ".ext", ".ext", ".ext", ".ext", ".ext"); 

$do_link = TRUE; 
$sort_what = 0; //0- by name; 1 - by size; 2 - by date
$sort_how = 0; //0 - ASCENDING; 1 - DESCENDING


# # #
function dir_list($dir){ 
    $i=0; 
    $dl = array(); 
    if ($hd = opendir($dir))    { 
        while ($sz = readdir($hd)) {  
            if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;  
        } 
    closedir($hd); 
    } 
    asort($dl); 
    return $dl; 
} 
if ($sort_how == 0) { 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return -1;  
        else return 1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return -1;  
        else return 1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return -1;  
        else return 1;  
    }  
}else{ 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return 1;  
        else return -1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return 1;  
        else return -1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return 1;  
        else return -1;  
    }  

} 

################################################## 
#    Getting The information
################################################## 

$i = 0; 
while($file=$directory->read()) { 
    $file = strtolower($file);
    $ext = strrchr($file, '.');
    if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
        {
            // dump 
        }
    else { 
        $temp_info = stat($file); 
        $new_array[$i][0] = $file; 
        $new_array[$i][1] = $temp_info[7]; 
        $new_array[$i][2] = $temp_info[9]; 
        $new_array[$i][3] = date("F d, Y", $new_array[$i][2]); 
        $i = $i + 1; 
        } 
} 
$directory->close(); 

################################################## 
# Sorting the information
################################################# 

switch ($sort_what) { 
    case 0: 
            usort($new_array, "compare0"); 
    break; 
    case 1: 
            usort($new_array, "compare1"); 
    break; 
    case 2: 
            usort($new_array, "compare2"); 
    break; 
} 

############################################################### 
#    Displaying the information
############################################################### 

$i2 = count($new_array); 
$i = 0; 
echo "<table class='CSSTableGenerator'> 
                <tr> 
                    <td width=290>File name (Download)</td>
                    <td align=center width=70>Downloads</td> 
                    <td align=center width=70>Depiction</td> 
                    <td align=center width=50>Size</td> 
                    <td align=center width=85>Modified</td>
                </tr>"; 
for ($i=0;$i<$i2;$i++) { 
if (!$do_link) { 
    $line = "<tr><td>" . $new_array[$i][0];
    $line .= '</td><td><a href="' . '../depictions/' . substr($new_array[$i][0], 0, strpos($new_array[$i][0], "_")) . '/index.php">Depiction</a></td>';
    $line .= "<td>" . number_format(($new_array[$i][1]/1024)) . " KB</td>"; 
    $line .= "<td>" . $new_array[$i][3] . "</td></tr>"; 

    }else{ 
$line = '<tr><td align=left ><A class="ex1" HREF="' .   
                        $new_array[$i][0] . '">' .  
                        $new_array[$i][0] .  
                        "</A></td>"; 
    $line .= '<td> </td>';
   $line .= '<td><a href="' . '../depictions/' . substr($new_array[$i][0], 0, strpos($new_array[$i][0], "_")) . '/index.php">Depiction</a></td>';
    $line .= "<td>" . number_format(($new_array[$i][1]/1024)) . " KB</td>"; 
    $line .= "<td>" . $new_array[$i][3] . "</td></tr>"; 
    } 
    echo $line; 
} 
echo "</table>"; 


?>

输出看起来像这样: enter image description here

我试图通过从mysql表中获取每个文件的下载计数来填充下载列

所以我在这里面临两个问题:

  1. 如何使用文件数组$new_array[$i][0]
  2. 获取统计信息
  3. 我如何在$line .= '<td> </td>';
  4. 中添加此mysql查询输出

    我尝试使用以下方法从表中获取统计信息:

    include("../config.php");
    $query = "SELECT stats FROM download WHERE filename='$new_array[$i][0]'";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $line .=  '<td>' . $row['stats'] . '</td>';
    }
    

    但它不起作用,我认为问题在于数组$new_array[$i][0],因为我尝试编写文件名filename='com.name.app3_2.2-1_iphoneos-arm.deb',我得到了此文件的统计信息

1 个答案:

答案 0 :(得分:2)

在“获取信息部分”中,您必须如此初始化数组:

################################################## 
#    Getting The information
################################################## 

$i = 0; 
$new_array = array();
while($file=$directory->read()) { 
    $file = strtolower($file);
    $ext = strrchr($file, '.');
    if (isset($allowed_ext) && (!in_array($ext,$allowed_ext)))
        {
            // dump 
        }
    else {
        $new_array[$i] = array();
        $temp_info = stat($file); 
        $new_array[$i][0] = $file; 
        $new_array[$i][1] = $temp_info[7]; 
        $new_array[$i][2] = $temp_info[9]; 
        $new_array[$i][3] = date("F d, Y", $new_array[$i][2]); 
        $i = $i + 1; 
        } 
} 
$directory->close(); 

################################################## 

另外,我注意到你的行:

$query = "SELECT stats FROM download WHERE filename='$new_array[$i][0]'";

会导致$query等于:

SELECT stats FROM download WHERE filename='Array[0]'

$i0时。

你应该做的是如此使用mysqli library

$mysqli = new mysqli("example.com", "user", "password", "database");
$query = "SELECT stats FROM download WHERE filename=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $new_array[$i][0]);
$stmt->execute();
$stmt->bind_result($stats);//stats turns into a reference by default.
while($stmt->fetch()) { //$stats now contains the stats
    $line .= '<td>'. htmlentities($stats).'</td>';
}

为了解决您刚才描述的丢失细胞的问题,您可以尝试:

$mysqli = new mysqli("example.com", "user", "password", "database");
$query = "SELECT stats FROM download WHERE filename=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('s', $new_array[$i][0]);
$stmt->execute();
$stmt->bind_result($stats);//stats turns into a reference by default.
$fetched = false;
while($stmt->fetch()) { //$stats now contains the stats
    $line .= '<td>'. htmlentities($stats).'</td>';
    $fetched = true;
}
if(!$fetched) {
    $line .= '<td></td>'
}