Melvin
Melvin

Reputation: 339

PHP can't find mySQL database

I've got the following markup and PHP. It's for a gallery - to upload and display images. I followed through this tutorial, but for some reason the mysqli_connect_db can't find the DB. what am I missing?

<?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>

and the 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'>";
		
}

?>

Upvotes: 0

Views: 1150

Answers (2)

Melvin
Melvin

Reputation: 339

Creating a new user with all the rights in mySQL specifically for this database solved the problem for me.

Upvotes: 0

nerdlyist
nerdlyist

Reputation: 2857

Also, this is missing what you are selecting

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

Should be (add *):

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

Upvotes: 1

Related Questions