Connection to mySQL db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cokelly
Forum Newbie
Posts: 2
Joined: Tue Jul 02, 2013 3:39 pm

Connection to mySQL db

Post by cokelly »

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

Code: Select all

<?php
$con=mysqli_connect("localhost","my_userid","my_password","my_db");
?>
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

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);
 ?> 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Connection to mySQL db

Post by AbraCadaver »

Maybe because you keeping referring to "config.php" but you are trying to include "config01.php"?

Also, while developing, turn on error reporting in php.ini or use:

Code: Select all

error_reporting(-1);
ini_set('display_errors', '1');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply