本文介绍了使用 Include 连接时如何从 MySql 数据库中获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包含连接文件连接到数据库.我的挑战是如何从数据库中获取卡住的位置.

I'm using include connection file to connect to the database. My challenge is how do I fetch from the database this is where am stuck.

include 'connection.php';

$sql = 'SELECT * FROM country';
$results = mysqli_query($sql);

推荐答案

假设你的 connection.php 包含

  <?php
        $con = mysqli_connect("localhost","my_user","my_password","my_db");

        if (mysqli_connect_errno())
        {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
  ?>

所以在您的文件中,您使用 include 'connection.php' 来获取连接.通过使用 include现在就像单页一样.然后你必须像下面那样使用它

So the in your file, you're using include 'connection.php' to get the connection. By using include its act like single page now. Then you've to use it like below

  require_once 'connection.php';

  $sql= 'SELECT * FROM country';
  $results = mysqli_query($con, $sql); # add connection string to query 

说明

当您添加此 include 'connection.php'; 时,无论父 (connection.php) 文件(例如:变量、函数等..) 会传给孩子.

when you add this include 'connection.php'; then whatever the data on parent(connection.php) file (ex: variable, Functions, etc ..) will come to child.

参考链接

  1. 在 PHP 中,include()完全有效吗?
  2. PHP 包含路径是否相对到文件或调用代码?
  3. include、include_once、require 或 require_once?
  1. In PHP, how does include() exactly work?
  2. Are PHP include paths relative to the file or the calling code?
  3. include, include_once, require or require_once?

这篇关于使用 Include 连接时如何从 MySql 数据库中获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:27