elu
elu

Reputation: 7

Select MySQL entry with arrays

I got this code, which creates a table listing all the files of my directory $filedir. I also got a SQL database which lists the names and custom IDs of every file uploaded. For some reason, it only links me to the subpage with the cusom ID on the first file. The other ones link me to my xampp dashboard. I'd appreciate any kind of help/tips.

<?php

$myDirectory = opendir($filedir);
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}
$dirArray2 = array_diff($dirArray, array('.', '..'));
array_splice($dirArray2, 1, 0);
closedir($myDirectory);
$indexCount= count($dirArray2);
echo("<TABLE id=myTable class=table-fill>\n");
echo("<thead><TR><th width=100px>---</th><th width=740px>Name</th><th width=160px>Last Modified</th></TR></thead>\n");
$j = 1;
echo "<tbody id=table class=fixed>";

if (count($dirArray2) == 0) {
    echo("<TR>");
    echo("<td> -- </td>");
    echo("<td>No files uploaded yet</td>");                                 
    echo("<td> -- </td>");
    echo("</TR>\n");
} else {
    for ($i = 0; $i < $indexCount; $i++) {
        echo("<TR>");
        echo("<td><center><b>" . $j++ . "</b></center></td>");
        $filename = $dirArray2[$i];
        $owner = $user_data['username'];
        $result = mysql_query("SELECT `url` FROM `uploads` WHERE `name` = '$filename' AND `owner` = '$owner'") or die(mysql_error());
        $row =  mysql_fetch_array($result);
        echo("<td class=contextMenu><a  href=/" . $row[$i] . ">" . $dirArray2[$i] . "</a></td>");
        echo("<td>" . date("m/d/Y h:ia", filemtime($filedir . $dirArray2[$i])) . "</td>");
        echo "</tr>";
    }

}
echo "</tbody>";
echo("</TABLE>\n");
?> 

Note that I am quite new to PHP and SQL, so be kind please :)

Upvotes: 0

Views: 81

Answers (1)

user4702132
user4702132

Reputation:

I think there is something wrong with your query. Replace the "`" with "'". This sometimes happens if copy and pasting goes wrong. Also you need to put the variables in between the query, not in the string.

Like so:

"SELECT 'url' FROM 'uploads' WHERE 'name' = '".$filename."' AND 'owner' = '".$owner."'"

If that doesn't solve your problem see how many rows your query returns. You can just run it in your database program.

What I also recommend you do is run a 'select * from uploads', then looping the returned array with a foreach loop.

Something like this:

    $content = "";

    $result = mysql_query("SELECT * FROM 'uploads'");
    $row =  mysql_fetch_array($result);

    $content.= "<table>";
    $content.= "<th>Column1</th><th>Column2</th><th>Column3</th>";

    foreach($row as $index => $value){
      $content.= "<td>$value[$index][1]</td>";
      $content.= "<td>$value[$index][2]</td>";
      $content.= "<td>$value[$index][3]</td>";
    } 

    $content.= "</table>";
    print $content;

This should return a 3 column table with your data in. Not sure though, haven't tested it.

Upvotes: 1

Related Questions