Connect PHP script to MySQL database on free host

Website URL:

http://quickstock.free.nf/ch10/quick/pages/catalog.php

I have read all the articles in the knowledge base and similar topics. Couldn’t find the answer.

I am using the free hosting for the website and created a database to connect to my PHP script. That’s still possible in the free hosting, right? I’m not using any external application to fetch the data from the database.

I configured an “.inc” file with all the necessary data to connect to the database:

<?php
$dbLocation = "sql208.infinityfree.com";
$dbUsername = "if0_35696018";
$dbPassword = "XXXXXXXXXXXXX"; (not changed from original)
$dbName = "if0_35696018_quickstock";
?>

The “connectToDatabase.php” script makes the connection to the database:

<?php

include ("......inc"); //I won't disclose the path for the file, but it is right.

if (!isset($dbLocation))
{
    echo "Database location is missing.<br>
        Connection script now terminating.";
    exit(0);
}

if (!isset($dbUsername))
{
    echo "Database username is missing.<br>
        Connection script now terminating.";
    exit(0);
}

if (!isset($dbPassword))
{
    echo "Database password is missing.<br>
        Connection script now terminating.";
    exit(0);
}

$db = mysqli_connect($dbLocation,
                    $dbUsername,
                    $dbPassword,
                    $dbName);
if (mysqli_connect_errno() || ($db == null))
{
    printf("Database connection failed: %s<br>
    Connection script now terminating.");
}
?>

There is a script for listing the category of products from the database on the final page, called “displayListOfCategories.php”:

<?php

$query = "SELECT * FROM Ref_Product_Categories
                    ORDER BY department_name DESC";
$categories = mysqli_query($db, $query);
$numRecords = mysqli_num_rows($categories);
$categoryCount = 0;
$currentDepartment = "";
echo 
"<table><tr><td><ul>";
for ($i=1; $i<=$numRecords; $i++)
{
    $row = mysqli_fetch_array($categories, MYSQLI_ASSOC);
    if ($currentDepartment != $row['department_name'])
    {
        if ($currentDepartment != "") echo "</ol></li>";
        if ($categoryCount > $numRecords/2)
        {
            echo "</ul></td>\r\n<td class='AlignToTop'><ul>";
            $categoryCount = 0;
        }
        $currentDepartment = $row['department_name'];
        echo "<li>$currentDepartment<ol>";
    }
    $prodCatCode = urldecode($row['product_category_code']);
    $prodCatDesc = $row['product_category_description'];
    $categoryURL = "pages/category.php?categoryCode=$prodCatCode";
    echo "<li><a href='$categoryURL'>$prodCatDesc</a></li>\r\n";
    $categoryCount++;
}
echo
"</ol></li></ul></td></tr></table>";
mysqli_close($db);
?>

And finally the last “catalog.php” page to display the product’s categories:

<?php
//catalog.php
include("../common/document_head.html");
?>

<body>
    <header>
        <?php
        include("../common/banner.php"); 
        include("../common/menus.html"); 
        include("../scripts/connectToDatabase.php"); 
        ?>
    </header>
    <main>
        <article class="CategoryList">
            <h4 class="ProductHeader">Complete List of Product Categories</h4>
            <?php
            include("../scripts/displayListOfCategories.php");
            ?>
        </article>
    </main>
    <footer>
        <?php include("../common/footer_content.html"); ?>
    </footer>
</body>

</html>

The funny thing is that I experimented with this same database in the localhost and it works. However the page uploaded to the server shows everything, except the data from the database. No errors are presented.

P.S.: There is no CSS style for this webpage yet.

Any ideas?
Thank you in advance!

Use .PHP instead and see if that fixes anything

4 Likes

Thank you for the detailed post, it helps a lot.

I don’t see any obvious issues in the code. Did you try running the SQL query in phpMyAdmin too? The only thing I can’t see here are the contents of the database, so could you double check that the right data is loaded?

IIRC the include directive just takes the contents of the file and treats it as PHP code. So you could use it to put PHP code in a file that’s not PHP.

However, it would be a dangerous thing to do, especially for database credentials, because if anyone would know that URL and tried to access it, they would just see the raw source code, including any secrets contained within.

3 Likes

Yes, I tried the SQL query directly at phpmyadmin and noticed that my table was empty :sweat_smile: (my bad!!!)

I have two versions of this database and one of them had an empty table (exactly the one that I was trying to fetch).

Thank you so much for your help and fast answer! I never thought it would be that fast!!!

And I want to thank all the Infinityfree team for providing this opportunity to developers by using this wonderful free platform. In my case I’m a new developer and Infinityfree has been giving me a great chance to learn how to use real servers and to improve my fullstack skills (without any costs).

Soon I’ll be experimenting with Python and Node.js and will be certainly going for the Premium plans (which are also super fair).

Thank you once again and I wish you a great day!!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.