简体   繁体   中英

If I connect to a mysql database in my index.php file, do I need to do it again in any linked html files with embedded php?

If I connect to a mysql database in my index.php file, do I need to do it again in any linked html files with embedded php that are parssed as php via .htaccess

So if I have an index.php which gets the header, body, and footer from external html documents, and those documents need to have header tags which need to change content depending on the database value. So do I need to connect the mysql database within the script in the linked html file or does it globally connect for that session if I do it the index.php

You just have to put your HTML code into index.php , and make another php file to connect your folder to MySQL. Then if you want to call data from MySQL to index.php, u have to type this code.

session_start();
include('connectdatabase.php');

If you connect to the database on index.php and run a query on a different page (eg query.php), it will fail - this is because the database connection is only active on index.php.

The recommended way is to create a file (eg global.php) and in it, connect to your database. Then, at the top of every page where you run PHP, simply add the following code:

<?php include('global.php'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM