doufei2662 2015-04-16 21:27
浏览 29
已采纳

从数据库中读取数据的PHP问题[重复]

This question already has an answer here:

I've try to read data from my database but I don't know what it don't work and show me that error

Notice: Array to string conversion in C:\Users\VladxD\Desktop\Xampp\htdocs\Web(workspace)\Test\show_animation.php on line 65

Here is my code:

MySQL_connect:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpassword = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpassword, "portofoliu_database") or die ("<div id='err'>Could not connect:</div> ");

Code:

<?php session_start(); ?>
<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Directory Contents</title>
    <link rel="stylesheet" href="show_folder.css">
    <link rel="stylesheet" href="NewFile.css">
</head>
<body>
<div id=wapper>
    <?php include 'Menu.php';
    include 'MySQL_connect.php'; ?>

    <div class="box">
        <div id="container">

            <table class="sortable">
                <thead>
                <tr>
                    <th>File name</th>
                    <th>Uplaod time</th>
                </tr>
                </thead>
                <tbody>
                <?php
                // Opens directory
                $user = @$_SESSION ['account'];
                $dir = @$_SESSION ['account'] . '/image/';
                if (is_dir($dir) == true) {
                    $myDirectory = opendir($dir);
                    // Gets each entry
                    while ($entryName = readdir($myDirectory)) {
                        $dirArray [] = $entryName;
                    }
                    // Closes directory
                    closedir($myDirectory);
                    // Counts elements in array
                    $indexCount = count($dirArray);
                    // Sorts files
                    sort($dirArray);
                    // Loops through the array of files
                    for ($index = 0; $index < $indexCount; $index++) {
                        // Allows ./?hidden to show hidden files
                        if ($_SERVER ['QUERY_STRING'] == "hidden") {
                            $hide = "";
                        } else {
                            $hide = ".";
                        }
//-------------------------------------------------------------------------------------------------
                        //$modtime=date("M j Y g:i A", filemtime($dir.$dirArray[$index]));
                        $modtime_test = mysqli_query($conn, "SELECT file_upload_time FROM users WHERE file_name ='$dirArray[$index]'");
                        $modtime = mysqli_fetch_assoc($modtime_test);
                        $timekey = date("YmdHis", filemtime($dir . $dirArray[$index]));
//-------------------------------------------------------------------------------------------------
                        if (substr($dirArray [$index], 0, 1) != $hide) {
                            // Gets File Names
                            $name = $dirArray [$index];
                            // Print 'em
                            echo "
                                 <tr class='file'>
                                     <td><a href=#' onclick='showImage(\"$index\")'>$name</a></td>
                                     <td sorttable_customkey='$timekey'><a href='#'>$modtime</a></td>
                                 </tr>";

                        }
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
        <div id="imageHolder">

            <?php if (is_dir($dir) == false)
                echo("<div id = 'noFile'>Your folder it's empty, upload file</div>");
            ?>

            <script>
                var array = [
                    <?php
                        foreach ($dirArray as $item)
                           {
                            echo "\"$dir{$item}\",";
                        }
                    ?>
                ];
                function showImage(index) {
                    var url = array[index];
                    url = "<img src='" + url + "'>";
                    document.getElementById("imageHolder").innerHTML = url;
                }

            </script>
        </div>
    </div>

</div>
</body>
</html>

I want to get my data time from my database and show it on my folder

Here is my element from database: http://i.imgur.com/kYciFQJ.png

and here are my site: http://i.imgur.com/HeEv1yG.png If you see below "Upload time" I have this text "Array" but I want to get me data time from my database and put there.

</div>
  • 写回答

2条回答 默认 最新

  • douwen7603 2015-04-16 21:31
    关注

    As mysqli_fetch_assoc returns an array, not a single value, even if there is a single field being returned... try changing

    $modtime = mysqli_fetch_assoc($modtime_test);
    

    to

    $results = mysqli_fetch_assoc($modtime_test);
    $modtime = $results["file_upload_time"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集