我的情况: 文件夹结构为 /resources/website/inc/ (/resources/ 是存储配置的位置,/website/ 保存 index.php 和 /inc/
我已经在 config.php / resources 文件夹中定义了数据库连接常量。
define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'my_username' );
define ( 'DB_PASSWORD', 'my_password' );
define ( 'DB_NAME', 'my_database' );
在index.php中访问数据库并显示数据。
我得出的结论是,它试图将 config.php 中定义的常量与 index.php 中的 connect 语句一起使用,但常量没有传递。
这是我的连接声明:
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or
die("MySQL Error: " . mysql_error());
$db = mysql_select_db(DB_NAME) or die("MySQL Error: " . mysql_error());
当我将这两部分添加到 config.php 中时,它可以完全工作,但我想在各个区域进行连接和关闭连接,我不想再次定义变量
您需要在可通过 Web 服务器访问的每个其他文件中要求(包含)配置文件。
仅此几行:
config.php
define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'my_username' );
define ( 'DB_PASSWORD', 'my_password' );
define ( 'DB_NAME', 'my_database' );
public/index.php
require_once __DIR__ . '/../config.php';
// The rest of you code here
public/contact.php
require_once __DIR__ . '/../config.php';
// The rest of you code here
<div class="container text-center">
<input type="text" name="email" id="email" placeholder=""> <br><br>
<input type="text" name="password" id="password" placeholder=""> <br><br>
<button type="submit" name="dugme" id="dugme" class="btn btn-sm ">signin</button>
<div id="answer"></div>
</div>
</body>
</html>
<script src="js/index.js"></script>
index.js
$(document).ready(function(){
$("#dugme").click(function(){
let unetEmail=$("#email").val();
let unetaLozinka=$("#lozinka").val();
$.post("login.php",{email: unetEmail, lozinka: unetaLozinka}, function(response){
$("#odgovor").html(response)
})
})
})
login.php
<?php
session_start();
if(isset($_POST['email'])&&isset($_POST['lozinka'])&&$_POST['email']!=""&&$_POST['lozinka']!=""){
$email=$_POST['email'];
$lozinka=$_POST['lozinka'];
mysqli_report(MYSQLI_REPORT_OFF);
$db=@mysqli_connect("localhost","root","","pva_k2");
if(!$db){
die("greska prilikom prijave na bazu, greska je ".mysqli_connect_errno().": ".mysqli_connect_error());
}
$upit="SELECT * from korisnici where email='$email' and lozinka='$lozinka'";
$rez=mysqli_query($db,$upit);
if(mysqli_error($db)!=""){
echo "greska pri izvrsavanju upita ".mysqli_errno($db).": ".mysqli_error($db);
}else{
if(mysqli_num_rows($rez)==1){
while($red=mysqli_fetch_assoc($rez)){
$_SESSION['ime'] = $red['ime'];
$_SESSION['prezime'] = $red['prezime'];
$_SESSION['status']=$red['status'];
$_SESSION['korisnikid']=$red['id'];
}
echo '<script>window.location.href = "posts.php";</script>';
exit();
}else{
echo "<div class='alert alert-danger'>Uneti podaci ne odgovaraju ni jednom korisniku</div><br>";
}
}
mysqli_close($db);
}else echo "<div class='alert alert-danger'>Svi podaci moraju biti uneti</div><br>";
?>