Php 从数组中获取Mysql字段
我有以下脚本来显示目录中的文件Php 从数组中获取Mysql字段,php,mysql,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-
<?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>";
?>
但它不起作用,我认为问题在于数组$new\u数组[$i][0]
,因为我尝试编写了一个文件名filename='com.name.app3\u 2.2-1\u iphoneos-arm.deb'
,并且我在“获取信息部分”中获得了此文件的统计信息,因此您必须初始化数组:
##################################################
# 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]'
当$i
为0
时
你应该做的是使用以下命令:
$mysqli=newmysqli(“example.com”、“user”、“password”、“database”);
$query=“从下载中选择stats,其中filename=?”;
$stmt=$mysqli->prepare($query);
$stmt->bind_param('s',$new_数组[$i][0]);
$stmt->execute();
$stmt->bind_结果($stats)//默认情况下,stats会变成一个引用。
而($stmt->fetch()){/$stats现在包含统计信息
$line.=''.htmlentities($stats)。'';
}
要解决您刚才描述的缺少单元格的问题,您可以尝试:
$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>'
}
$mysqli=newmysqli(“example.com”、“user”、“password”、“database”);
$query=“从下载中选择stats,其中filename=?”;
$stmt=$mysqli->prepare($query);
$stmt->bind_param('s',$new_数组[$i][0]);
$stmt->execute();
$stmt->bind_结果($stats)//默认情况下,stats会变成一个引用。
$fetched=false;
而($stmt->fetch()){/$stats现在包含统计信息
$line.=''.htmlentities($stats)。'';
$fetched=true;
}
如果(!$fetched){
$line.=''
}
您在代码中的哪里初始化$new\u数组?在中获取信息
部分?这并没有解决问题,它仍然是相同的输出。我已经编辑了我的答案,添加了一个新的部分,解决了其他问题。谢谢!它可以工作,还有一件事要问你,如果一个文件还没有下载,这意味着它还没有包含在mysql表中,我怎么能显示一个空白单元格而不是这个呢?太棒了,谢谢你的帮助!
$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>'
}