Connection to mySQL db
Posted: Wed Jul 10, 2013 7:00 am
hello all. I want to set up a single connection string in config.php against which my many php pages (page1.php, page2.php, page3.php , ...pageN.php) can connect.
Each pageX.php will call "config.php" (which contains username, password, db) without the need to include username, pw, db in each page.php
config.php looks like
The result of http://localhost/page1.php?ArticleID=XX is blank as page1.php is not reading the database content. Is it because "$con" is stored separately in the remote "config.php".
Can anybody offer the correct syntax for page1.php to correctly read the "config.php" parameters? Thanks very much
Each pageX.php will call "config.php" (which contains username, password, db) without the need to include username, pw, db in each page.php
config.php looks like
Code: Select all
<?php
$con=mysqli_connect("localhost","my_userid","my_password","my_db");
?>
Can anybody offer the correct syntax for page1.php to correctly read the "config.php" parameters? Thanks very much
Code: Select all
<?php
// fetch the userid, password and db connection parameters from config
include ("../admin/config01.php");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$ArticleID = $_GET['ArticleID'] ;
// get the results from the database containing various articles
$result = mysqli_query($con,"SELECT * FROM articles WHERE ArticleID='$ArticleID' ");
while($row = mysqli_fetch_array($result))
{
echo $row['ArticleID'] . " " . $row['Article'];
echo "<br>";
}
mysqli_close($con);
?>