簡體   English   中英

將圖像列添加到現有的mysql表中?

[英]Add image column into an existing mysql table?

我想在現有的mysql表中添加一個名為image的新列。

類型為BLOB ,屬性為BINARY和NO NULL

我應該在PhpMyAdmin中使用什么代碼?

在PhpMyAdmin中,您可以使用表編輯器通過單擊表的結構來添加新列。

相反,mysql命令將是:

ALTER TABLE table_name ADD image MEDIUMBLOB NOT NULL

MEDIUMBLOB的最大大小為16MB,請使用LONGBLOB(最大4GB)存儲更大的內容

如果您在上傳BLOB時遇到問題,請檢查max_allowed_pa​​cket的值http://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html

不建議使用BLOB保存圖像。

將文件保存在Web服務器上的某個位置,例如... / htdocs / images / picture.jpg(假設您使用xampp)。 然后在數據庫中為圖像名稱的字符串創建一個簡單的列。 向您展示為什么我將使用PHP。 在您的... / htdocs / index.php

然后,您可以執行以下操作:

<?php
//enter you PhpMyAdmin details here.
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT imageName
        FROM   MyTable
        ;

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    ?>
    <img src="images/<?=$row["userid"]?> <!-- this will get repeted for all the rows in you database -->
    <?php
}

mysql_free_result($result);

?>

這是我從以下位置獲得示例代碼的地方: PHP Doc我剛剛對其進行了一些修改..但是php文檔很棒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM