PHP无法找到mySQL数据库

时间:2016-06-07 19:35:16

标签: php mysql database mysqli

我有以下标记和PHP。这是一个画廊 - 上传和显示图像。我按照this教程进行了操作,但由于某种原因,mysqli_connect_db无法找到数据库。我错过了什么?

<?php
include('upload.php');
?>

<form action="index.php" method="POST" enctype="multipart/form-data">

Choose File: <input type="file" name="file">
Title: <input type="text" name="nam">
<input type="submit" name="submit">

</form>

和PHP:

<?php

$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to DB");
mysqli_select_db($con, "galerie") or die ("no database");

if(isset($_POST['submit'])){

$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$location = 'uploads/';
$target = 'uploads/' .$name;

	if(move_uploaded_file($tmp_name,$location.$name)){
		
		echo "file uploaded";
		
		$nam = $_POST['nam'];
		$query = mysqli_query($con , "INSERT INTO images(img_name,img_title)VALUES('".$target."','$nam')");
		
	} else {
		
		echo "file not uploaded";
			
	}

}

$result = mysqli_query($con, "SELECT FROM images");
while($row = mysqli_fetch_array($result)){
	
	echo "<img src=".$row['img_name']." &nbsp; class='addClass'>";
		
}

?>

2 个答案:

答案 0 :(得分:1)

此外,这是您错过的选择

$result = mysqli_query($con, "SELECT FROM images");

应该是(添加*):

$result = mysqli_query($con, "SELECT * FROM images");

答案 1 :(得分:0)

专门为此数据库创建具有mySQL所有权限的新用户解决了我的问题。