-2

What would be the correct way to connect to database ? I don't get errors but maybe my connect isn't used correctly I'm using mysqli Here is my code

 <?php 
 require_once("configur.php");

 $mysqli = new mysqli(localhost, root, password, user);

 $query_image = 'INSERT INTO fun_table (images1, images2, images3, images4) 
values( "' . $_FILES['file1']['name'] . '",
        "' . $_FILES['file2']['name'] . '",
        "' . $_FILES['file3']['name'] . '",
        "' . $_FILES['file4']['name'] . '"
   )';


   if ($mysqli->query($query_image) === TRUE) {

   echo "<script language='javascript'>\n";
   echo "alert('Upload successful!')";
   echo "</script>\n";
 } else {
  echo "Error updating record: " . $conn->error;
 }

    $mysqli->close();

   ?>
user7368271
  • 73
  • 1
  • 9

2 Answers2

1
$mysqli = new mysqli(localhost, root, password, user);

You need to define those or put them in quotes.

like:

$servername = 'localhost';
$username = 'user123';
$password = 'helloworld';
$dbname ="testdatabase";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $dbname);
$conn->set_charset($charset);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Marcel Schmid
  • 424
  • 2
  • 15
  • 1
    The OP commented that he replaced the values for security reasons when posting here. – aynber Jan 31 '17 at 14:53
  • Yeah I read that after posting, but since he replaced the values without quotes I could still imagine that´s the problem. But I will delete the answer if it isn´t. – Marcel Schmid Jan 31 '17 at 14:55
0

Or you can use PDO for the connection, for example:

<?php
$db = new PDO("mysql:host=localhost;dbname=db_name;charset=utf8mb4", "user", "password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345