有没有办法让我的图片通过mysql或phpmyadmin显示在我的php页面上?

iyfamqjs  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(366)

我想知道我是否可以运行php页面,因为我试图从phpmyadmin和mysql中的数据库加载图像文件。下面是我正在编写的代码行:

CREATE DATABASE WORLD;

use WORLD;

CREATE TABLE COUNTRY (
    country_Order INT AUTO_INCREMENT NOT NULL,
    continent_Name VARCHAR(225),
    country_Name VARCHAR(225),
    img BLOB,
    country_Region VARCHAR(225),
    country_Population INT,
    country_Info VARCHAR(225),
    PRIMARY KEY (country_Order)
)  ENGINE=INNODB;

INSERT INTO COUNTRY (country_Order, continent_Name, country_Name, img, country_Region, country_Population, country_Info)
VALUES (1, "Asia", "Afghanistan", LOAD_FILE('\Users\sammyabdulkader\desktop\World-Data\img\Afghanistan.png'), "Central Asia", 38928346,"Afghanistan is a country located in Central Asia. It is a mountainous landlocked country.");

我想手动插入我自己的图片,为什么从我的文件夹路径。这是在我的macbook pro上完成的。以下是我的php代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
  <body>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>World Database</title>
        <style type="text/css">
        h1 {
            color: #42963d;
            font-family: 'Open Sans', sans-serif;
            font-size: 34px;
            font-weight: 300;
            line-height: 40px;
            margin: 0 0 16px;
            text-align: center;
        }

        h2 {
            font-family: 'Open Sans', sans-serif;
            font-weight: 300;
            text-align: center;
            color: #50d248;
        }

        p.footer {text-align: center}
        table.output {font-family: Ariel, sans-serif}

        table {
            border-collapse: collapse;
            width: 100%;
            color: #4ee44e;
            font-family: monospace;
            font-size: 25px;
            text-align: center;
        }

         th {
            background-color: #0b7208;
            color: white;
        }

        tr:nth-last-child(even) {background-origin: #f2f2f2}

         hr {
            height: 12px;
            border: 0;
            box-shadow: inset 0 12px 12px -12px rgba(0, 0, 0, 0.5);
        }

        a {
            color: red;
            font-family: helvetica;
            text-decoration: none;
            text-transform: uppercase;
        }

         a:hover {
            text-decoration: underline;
        }

        a:active {
            color: black;
        }

        a:visited {
            color: purple;
        }
        </style>
    </head>
   <body>
<?php
// Get connection
//$Conn = odbc_connect('database name','database user','database user & password');
$Conn = odbc_connect('WORLD', 'WORLD-User', 'WORLD-User+password');

// Test connection
if (!$Conn)
{
    exit("ODBC Connection Failed: " . $Conn);
}
// Create SQL statement
$SQL = "SELECT * FROM COUNTRY";

$RecordSet = odbc_exec($Conn, $SQL);

// Test existence of recordset
if (!$RecordSet)
{
    exit("SQL Statement Error: " . $SQL);
}

?>
                        <!--  Page Headers -->
    <h1>List of countries from around the global.</h1>
        <hr />
    <h2>All Country's Alphabetical Order</h2>
<?php
// Table headers 
echo "<table class='output' border='1'>
        <tr>
        <th>Country Order</th>
        <th>Continent Name</th>
        <th>country Name</th>
        <th>Country Flag</th>
        <th>Continent Region</th>
        <th>Country Population</th>
        <th>Country Info</th>
        </tr>";

// Table data
while ($RecordSetRow = odbc_fetch_array($RecordSet))
{
    echo "<tr>";
    echo "<td>" . $RecordSetRow['country_Order'] . "</td>";
    echo "<td>" . $RecordSetRow['continent_Name'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Name'] . "</td>";
    echo "<td>" . $RecordSetRow['img'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Region'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Population'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Info'] . "</td>";
    echo "</tr>";
}
echo "</table>";

// Close connection
odbc_close($Conn);
?>
        <br />
         <hr />
        <p class="footer"><a href="../World-Data/index.html">Return to World Data</a></p>
       <hr />
  </body>
</html>

我的问题是,当我使用手动方式时,我的php表会得到空值。另一个问题是在phpmyadmin上执行会使它看起来更糟。相反,它返回奇怪的字符。
有没有一种可能的方法来解决我的问题,以便我可以在我的数据库表中得到我的图像并运行?phpmyadmin方式从联机数据库插入图像手动方式从mysql插入

esbemjvw

esbemjvw1#

这只是一个如何做到这一点的例子。
其思想是为每个图像创建一个唯一的文件名。一种简单快速的方法是md5散列。然后我们检查文件是否存在,如果不存在,我们就创建它。这种缓存提高了重新加载时的性能,并且不需要再次创建映像。
使用 IMG 标记我们只显示(新创建的)文件。
注意:您可能需要使用css设置样式,并将图像放置到一个目录中以便更好地组织。

while ($RecordSetRow = odbc_fetch_array($RecordSet))
{
    $filename = md5($RecordSetRow['img']) . '.png';
    if(!file_exists($filename)) {
          file_put_contents($filename, $RecordSetRow['img']);
    }

    echo "<tr>";
    echo "<td>" . $RecordSetRow['country_Order'] . "</td>";
    echo "<td>" . $RecordSetRow['continent_Name'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Name'] . "</td>";

    // Use HTML image tag with that file
    echo "<td><img src='{$filename}' alt=''></td>";

    echo "<td>" . $RecordSetRow['country_Region'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Population'] . "</td>";
    echo "<td>" . $RecordSetRow['country_Info'] . "</td>";
    echo "</tr>";
}
echo "</table>";

相关问题