0

I am busy with a school project to learn MVC. But I know very little of php. I have an dbconnection file and it looks like this

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "filmopdrachtdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connet_error);
}

echo "Connected SuccessFully";

I have a login page that looks like this:

<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
    <form action="../Controllers/UserController.php" method="post">
        Gebruikersnaam <input type="text" name="naam">
        Wachtwoord <input type="password" name="wachtwoord">
        <input type="submit" value="Login">
    </form>
    </body>
</html>

And I have an User Controller that looks like this:

<?php
include_once ("../Includes/DbConnection.php");
include_once ("../Models/User.php");

$gebruiker = new User();
$naam = $gebruiker->setGebruikersnaam($_POST["naam"]);
$wachtwoord = $gebruiker->setWachtwoord($_POST["wachtwoord"]);

$stmt = "SELECT gebruikersnaam, wachtwoord FROM klanten";


var_dump($stmt);

How do I run the $stmt query. I don't understand what I have to do

$stmt = "SELECT gebruikersnaam, wachtwoord FROM klanten";

EDIT: I want the query $stmt to run in the UserController. Not in the DbConnection file.

2
  • 2
    You can find details about your request in the official documentation, check this link : php.net/manual/en/mysqli.quickstart.statements.php Commented Dec 17, 2019 at 9:11
  • 1
    Tip: I would recommend using PDO instead of mysqli. PDO gives you a more verbose API. making it easier to work with. Commented Dec 17, 2019 at 9:14

2 Answers 2

1

If you use stm, you can execute with that code:

$stmt=$conn->prepare("SELECT gebruikersnaam, wachtwoord FROM klanten WHERE user=?");
$stmt->bind_param('s',$naam);
$stmt->execute();

OR

$conn->query($stmt);
Sign up to request clarification or add additional context in comments.

2 Comments

The first one would throw an error since $stmt is a string and not a statement.
Yes but we dont know if he want use statement or not. i will correct
0

In your DbConnection.php file you have a $conn variable which contains your connection to the database. From that variable you can execute your queries on the database.

From the mysqli PHP documentation, modified to work with your code:

$results = $conn->query($stmt);

Please note that this method should not be used with a dynamically generated query string:

//BAD practice, leads to SQL injection
$results = $conn->query("SELECT * FROM MyTable WHERE myColumn = $search LIMIT 10");

For dynamically generated queries use prepared queries.

I could also suggest to use PDO which is a more versatile database interaction libraray in PHP, which you will probably not be able to do for that code as it is for school, but for your own projects :)

3 Comments

But I want to use the $conn variable in the UserController not in the connection file. Can I just use it like your first line of code or do I need to do something else
Use function include or require_once for call controller to your page.
Your user controller already includes your DbConnection.php file :) include_once ("../Includes/DbConnection.php");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.