SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)

    PHP isn't connecting to MySQL DB

    Hey All,

    I've been developing a PHP / MySQL database-driven website locally on XAMPP. Everything works perfectly fine and as expected, so, with some data in the database I decided it was time to finally upload the website files and DB to my web host MediaTemple on the Grid-Server service.

    Unfortunately the PHP isn't connecting to the MySQL DB, for some reason.

    Please see the following database-driven PHP pages which aren't connecting to the MySQL DB:

    http://huffier.com/library/
    http://huffier.com/library/title/?slug=dracula
    http://huffier.com/library/read/?slu...ula&pageSlug=1

    As you can see from the links - No error messages and also no data from the MySQL DB either. My DB connection is as follows (the details aren't the real details, but they are correct when they are the real details):

    PHP Code:
    <?php
    try
    {
    $pdo = new PDO('mysql:host=internal-db.s00000.gridserver.com;dbname=db00000_huffier''db00000''password');
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');
    }
    catch (
    PDOException $e)
    {
    $output 'Unable to connect to the database server.';
    include 
    'error.html.php';
    exit();
    }
    The (mt) support guy tested the connection with an mttest database user without error and found that the database server itself is working and accepting connections. He also mentioned that the DB connection script that I've pasted above was configured correctly so this is likely not an issue, which means it must be something to do with the other PHP files I've got.

    This index.php file is located in the "library" folder:

    PHP Code:
    <?php

    include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbconnection.inc.php';

    try
    {
        
    $sql 'SELECT bookTitle, bookSlugName FROM books';
        
    $result $pdo->query($sql);
    }
    catch (
    PDOException $e)
    {
        
    $error 'Error fetching books: ' $e->getMessage();
        include 
    'error.html.php';
        exit();
    }

    foreach (
    $result as $row)
    {
        
    $books[] = array(
        
    'name' => $row['bookTitle'],
        
    'slug' => $row['bookSlugName']
    );
    }

    include 
    'library.html.php';
    ?>
    And this is the library.html.php file, also located in the "library" folder:

    HTML Code:
    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/huffier.com/includes/helpers.inc.php'; ?>
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<meta name="description" content="Browse through all of the eBook titles available in the Huffier digital library on this page.">
    		<meta name="viewport" content="width=device-width, initial-scale=1.0">
    		<title>Library &ndash; Huffier</title>
    		<base href="http://www.huffier.com/"/>
    		<!--[if lt IE 9]>
    		<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    		<![endif]-->
    		<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    		<link rel="stylesheet" media="screen" href="../css/stylesheet.css">
    	</head>
    	
    	<body>
    		<?php include_once("../analyticstracking.php") ?>
    
    		<nav>
    			<?php include_once("../includes/nav.php") ?>
    		</nav>
    
    		<article>
    			<header>
    				<h1>Library</h1>
    			</header>
    			
    			<div id="content">
    				<ul id="title-listing">
    					<?php foreach ($books as $book): ?>
    					<li>
    						<a href="title/?slug=<?php htmlout($book['slug']); ?>">
    						<img src="../images/covers/<?php htmlout($book['slug']); ?>.png" alt="<?php htmlout($book['name']); ?>"/>
    						</a>
    					</li>
    					<?php endforeach; ?>
    				</ul>
    
    			</div>
    		</article>
    	</body>
    </html>
    And the "error.php" files, also located in the "library" folder just replaces the PHP containers in the "library.html.php" file with <?php echo error; ?>.

    Any bright ideas as to what has gone woefully wrong with my scripting?

    Any pointers in the right direction (or a solution) would be greatly appreciated!

    Thanks!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Okay... so what is the error you're getting? White screen? No data? How did you "upload" your database? did you actually import the data?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm just getting into this stuff and it looks like you are not preparing anything.
    I have,
    Code:
    		$stmt = $pdo->prepare($q);
    		$stmt->execute($params);
    My print_r looks like
    params to execute Array
    (
    [:name] => o'neil
    )

    INSERT INTO customers (name) VALUES (colon)name)

    I made a class that makes writing PDO dirt simple and this is the debug info.

    Its tricky to learn but it appears that you are not preparing your query.
    What I lack in acuracy I make up for in misteaks

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Usually on a live server error reporting and showing are not implemented. This makes the scripts run OK even with warnings.

    Try adding these two lines to the top of your script:
    PHP Code:
     error_reporting(-1);
     
    ini_set('display_errors'true); 
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($result as $row

        
    $books[] = array( 
        
    'name' => $row['bookTitle'], 
        
    'slug' => $row['bookSlugName'
    ); 
    This bit is a likely suspect, you're trying to loop through a result set but you've not yet grabbed the result set from the db, so the foreach loop doesn't find an array that it expects to work with so you're getting an error.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Guru bronze trophy AndrewCooper's Avatar
    Join Date
    Sep 2008
    Location
    Manchester, UK
    Posts
    631
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Ok - Thanks for your help guys, but it seems that it was a problem with MediaTemple. I spun up a Rackspace Cloud Server and a Cloud Database, uploaded the SAME files with no lines of code changed, besides the hostname for the DB connection file...And it worked perfectly fine. I've included responses to your replies below.

    Quote Originally Posted by StarLion View Post
    Okay... so what is the error you're getting? White screen? No data? How did you "upload" your database? did you actually import the data?
    I wasn't getting any errors StarLion. If you checked out the links you'd see it was just no data. I did actually import the database because, well, it was there. Plus the (mt) would have told me I was a numpty for trying to connect to a non-existing database on the server

    Quote Originally Posted by lorenw View Post
    I'm just getting into this stuff and it looks like you are not preparing anything.
    I have,
    Code:
    		$stmt = $pdo->prepare($q);
    		$stmt->execute($params);
    My print_r looks like
    params to execute Array
    (
    [:name] => o'neil
    )

    INSERT INTO customers (name) VALUES (colon)name)

    I made a class that makes writing PDO dirt simple and this is the debug info.

    Its tricky to learn but it appears that you are not preparing your query.
    Well, I don't really know about preparing stuff. The code I've developed this site with is -pretty much- taken from Kevin Yank's PHP & MySQL Novice to Ninja book, and it works...

    Quote Originally Posted by John_Betong View Post
    Usually on a live server error reporting and showing are not implemented. This makes the scripts run OK even with warnings.

    Try adding these two lines to the top of your script:
    PHP Code:
     error_reporting(-1);
     
    ini_set('display_errors'true); 
    Thanks for this tip John, this will be helpful in the future for debugging on a testing server!

    Quote Originally Posted by SpacePhoenix View Post
    PHP Code:
    foreach ($result as $row

        
    $books[] = array( 
        
    'name' => $row['bookTitle'], 
        
    'slug' => $row['bookSlugName'
    ); 
    This bit is a likely suspect, you're trying to loop through a result set but you've not yet grabbed the result set from the db, so the foreach loop doesn't find an array that it expects to work with so you're getting an error.
    Likely suspect, but not the culprit, it was the MediaTemple servers...Or possibly database, most likely.

    It works now anyway, but thanks for your help Appreciate it!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •