如何在浏览器上显示图像?它给了我错误

时间:2014-04-18 14:30:08

标签: javascript php jquery mysql

我已成功将我的图片上传到文件夹并成功将我的路径保存到数据库中,因为我试图将图片显示到浏览器中显示错误:

Warning: mysql_query() expects parameter 2 to be resource, object given in C:\Users\Raj\PhpstormProjects\image\upload_file.php on line 6

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\Users\Raj\PhpstormProjects\image\upload_file.php on line 7 File name not found in database

这是我的表格代码:

<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "photo";
$username_connect= "root";
$password_connect= "Bhawanku";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db($database_connect) or die (mysql_error());

if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

    if ($_FILES["file"]["error"] > 0)
    {
// if there is error in file uploading
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

    }
    else
    {
// check if file already exit in "images" folder.
        if (file_exists("images/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {  //move_uploaded_file function will upload your image.  if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
            if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))
            {
// If file has uploaded successfully, store its name in data base
                $query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
                if(mysql_query($query_image))
                {
                    echo "Stored in: " . "images/" . $_FILES["file"]["name"];
                }
                else
                {
                    echo 'File name not stored in database';
                }
            }
        }


    }
}
?>
<html>
<body>
<form action="upload_file.php" method="post"enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

这是我展示图片的代码:

<?php
$con=mysqli_connect("localhost","root","Bhawanku","photo");
// Check connection
$query_image = "SELECT * FROM acc_images";
// This query will show you all images if you want to see only one image pass acc_id='$id' e.g. "SELECT * FROM acc_images acc_id='$id'".
$result = mysql_query($query_image, $con);
if(mysql_num_rows($result) > 0)
{
    while($row = mysql_fetch_array($result))
    {
        echo '<img alt="" src="images/'.$row["image"].'">';
    }
}
else
{
    echo 'File name not found in database';
}
?>

2 个答案:

答案 0 :(得分:2)

在第二个脚本中,您将mysqli连接,然后使用mysql_querymysql_num_rowsmysql_fetch_array。 MySQLi和MySQL不可互换。

$result = mysqli_query($query_image, $con);
if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_array($result))
    {
        echo '<img alt="" src="images/'.$row["image"].'">';
    }
}
else
{
    echo 'File name not found in database';
}

您应该考虑将第一个脚本更改为MySQLi,并使用预准备语句而不是将变量连接到查询中。

答案 1 :(得分:0)

在第一行你使用mysqli扩展,但在所有其他 - mysql。 尝试改变:

<?php
$con=mysql_connect("localhost","root","Bhawanku");
mysql_select_db("photo", $con);
// Check connection
$query_image = "SELECT * FROM acc_images";
// This query will show you all images if you want to see only one image pass acc_id='$id' e.g. "SELECT * FROM acc_images acc_id='$id'".
$result = mysql_query($query_image, $con);
if(mysql_num_rows($result) > 0)
{
    while($row = mysql_fetch_array($result))
    {
        echo '<img alt="" src="images/'.$row["image"].'">';
    }
}
else
{
    echo 'File name not found in database';
}
?>