I can’t access my database

Asked

Viewed 71 times

1

I have the following problem, I am trying to change the year of the dates I have in my database, and for that I am using:

mysqli_fetch_row

But I am not succeeding, it always returns failed. I also made a connection to the database and apparently it is working because it does not return with error. I would very much appreciate any assistance provided. If the question is not properly clear, please ask, and I will make the appropriate changes. Thank you

Code:

 <?php 
 //Create a database connection
     $dbhost = "localhost";
     $dbuser = "root";
     $dbpass = "";
     $dbname = "bd-aircaw-1";
     $connection =mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

    $query = "SELECT * ";
    $query .= "FROM voo ";
    $query .= "WHERE DataPartida = 2014-11-01 18:00:00";

    $result = mysqli_query($connection, $query);
    if(!$result) {
        die ("Database query failed.");
    }

    while($row = mysqli_fetch_row($result)){
        var_dump($row);
        echo "<hr />";
    }
  • Date values should be in single quotes.

  • I already modified the quotes for simple but no effect continues to give error "Database query failed."

2 answers

2

Your code with problem:

$query .= "WHERE DataPartida = 2014-11-01 18:00:00";

Add single quotes or double quotes with exhaust:

$query .= "WHERE DataPartida = '2014-11-01 18:00:00'";
ou
$query .= "WHERE DataPartida = \"2014-11-01 18:00:00\"";

1


Your connection is incomplete, see the example of W3schools: $con=mysqli_connect("localhost","my_user","my_password","my_db");

You should use quotes to search for datas and strings in the BD:

`$query .= "WHERE DataPartida = \"2014-11-01 18:00:00\"";`
  • I already changed the code, thanks for the suggestion, I didn’t really notice the missing parameters, but although I have corrected, changed and continues without running; the program.

  • What mistake is there now?

  • I think you have another problem in your SQL, I will edit my answer.

  • Gives the same error: "Database query failed." I think it is something related to the querys, they should not be finding any values, but I have checked the values and it seems to me that there are no mistakes.

  • Thanks was even that the problem :) Thank you very much for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.