Jump to content

Recommended Posts

Okay. I have been out of programming a long time and was told that the website I was going to be upgraded to PHP8. I converted all my database queries to PDO. I assumed that the rest of the PHP would be fine. It apears not. It seems the problem is with my PDO which worked fine under PHP 5.4. I know this is not going to be a topic you would not want to get into but if I had some pointers at this level  could work the rest out. Seems it does not like ...

    $pdo = connectDB();
    $stmt = $pdo->query("SELECT * FROM  Bank_config");
    $row = $stmt->fetch();
    return $row;
This was actualy working under PHP 5.4

 

Website.jpg.c3df10be8f97d5c9f8fc55a000897288.jpg

SecureFunctionsBank.txt

Statement.txt

Edited by Paul-D
Link to comment
https://forums.phpfreaks.com/topic/317486-problems-with-upgrde-php-54-to-php8/
Share on other sites

If you're encountering issues with the code under PHP 8, there might be some compatibility issues. However, let's check a few things:

1. **Check for PDO Extension:**
   Ensure that the PDO extension is enabled in your PHP configuration. You can do this by checking your `php.ini` file or by creating a PHP file with `phpinfo();` and checking if PDO is listed.

2. **Error Handling:**
   Check if there are any error messages that might give you more information about what's going wrong. You can add error handling to your PDO code:

   ```php
   try {
       $pdo = connectDB();
       $stmt = $pdo->query("SELECT * FROM Bank_config");
       $row = $stmt->fetch();
       return $row;
   } catch (PDOException $e) {
       echo "Error: " . $e->getMessage();
   }
   ```

   This will print any errors that occur during the execution of the query.

3. **Function Implementation:**
   Ensure that the `connectDB()` function is correctly implemented and returns a PDO instance. If there's an issue with this function, it might affect the query execution.

4. **Table and Column Names:**
   Make sure that the table name (`Bank_config`) and column names in your query match the actual structure of your database. In PHP 8, there might be stricter adherence to correct syntax.

5. **PHP Short Tags:**
   Ensure that your PHP files are using the full `<?php` tag rather than short tags (`<?`). Although this is less likely to be an issue, it's a good practice for compatibility.

6. **PHP Version Mismatch:**
   Double-check that your web server is using PHP 8. Sometimes, there might be multiple PHP versions installed, and the web server could be configured to use a different version.

If you still face issues, please provide any error messages you receive, and I'll do my best to assist you further.

Your code seems to be a mix of old and new PHP practices. The main issue you're facing could be due to the deprecated `mysql` functions. You should switch to using `PDO` consistently throughout your code, as you've started doing in some places.

Here's an updated version of your code using `PDO`:

<?php
define('OTHER', 1);
define('SHOPS', 10);
define('ONLINE', 11);
define('CASH', 12);
define('EBAY', 15);
define('ARGOS', 16);

// ... (other constants and functions)

function GetBalance() {
    $pdo = connectDB();
    $stmt = $pdo->query("SELECT * FROM Bank_config");
    $row = $stmt->fetch();
    return $row;
}

function GetAllData($StartDate) {
    $pdo = connectDB();
    $sqlAllData = "SELECT * FROM Bank_Data WHERE EntryDate > :StartDate AND EntryDate <= DATE_ADD(:StartDate, INTERVAL  6 WEEK) ORDER BY EntryDate ASC, Output";

    if ($_SESSION['CounterValue'] == 'Total') {
        $sqlAllData = "SELECT * FROM Bank_Data ORDER BY EntryDate ASC, Output";
    }

    if ($_SESSION['CounterValue'] == 'Database') {
        $sqlAllData = "SELECT * FROM Bank_Data ORDER BY EntryDate ASC, Output";
    }

    $stmt = $pdo->prepare($sqlAllData);
    $stmt->execute(['StartDate' => $StartDate]);

    return $stmt;
}

// ... (other functions)

function DeleteOldData() {
    $pdo = connectDB();
    $Date = time();
    $Month = date('m', $Date);
    $Year = date('Y', $Date);
    $Month -= 1;
    if ($Month == 0) {
        $Month = 12;
        $Year -= 1;
    }
    $Date = $Year . "-" . $Month . "-01";
    $sqlOldData = "DELETE FROM `Bank_Data` WHERE EntryDate < :Date";
    $stmt = $pdo->prepare($sqlOldData);
    $stmt->execute(['Date' => $Date]);
}
?>

Please note that you should replace `connectDB()` with your actual database connection function. This code assumes that `connectDB()` returns a valid PDO connection. Update other parts of your code similarly to use PDO consistently.

17 minutes ago, Olumide said:

hpin

Here is the PHP info on the website server. Don't understand that part. Can you check that this part is atleast correct please.

 

As for

 Ensure that the `connectDB()` function is correctly implemented and returns a PDO instance. If there's an issue with this function, it might affect the query execution.;'

This worked fine under PHP 5.4 so it is calling the database correctly and supplying the data.

phpInfo.jpg

Edited by Paul-D
2 minutes ago, Paul-D said:

Here is the PHP info on the website server. Don't understand that part. Can you check that this part is atleast correct please.

phpInfo.jpg

 

 

 

Here's the modified version of your code for the second attachment which I was unable to download earlier.

 

<?php
// Banking Version 2.0.0   31-03-2023  Desmond O'Toole.
include("secure/SecureFunctionsBank.php");
include("../secure/SecurePDO.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Start the session
session_start();

$page = "Bank Login";
Session_Init();

if (!isset($_SESSION["Pk"])) {
    header('Location: index.php');
    exit;
}

$Pk = $_SESSION["Pk"];

if (KeyCheckX($Pk) == 0) {
    header('Location: index.php');
    exit;
}

$EndTime = KeyTestX($Pk, 0);
$_SESSION["current_page"] = $page;

$qBalance = GetBalance();
$Date = $qBalance['EntryDate'];
$Value = $qBalance['BalanceValue'];

$stamp = strtotime($Date);
$StartDate = date('D d-M-Y', $stamp);
$stamp = $stamp + (WEEKS * 6) + (DAYS * 1);
$EndDataDate = date('D d-M-Y', $stamp);

$curValue = $Value;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Bank Home 2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="imagetoolbar" content="no">
    <meta http-equiv="refresh" content="60">
    <link rel="stylesheet" href="stylesheets/Library.css" type="text/css">
    <link rel="stylesheet" href="stylesheets/LibraryMenu.css" type="text/css">
</head>
<body>
<div id="PageTitle">
    <img src="images/LibraryBanner.jpg" alt="">
</div>

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

<div id="PageContent">
    <form action="menu.php" method="post">
        <span style="color:blue;"><b>Est: 18-July-2015</b></span>
        <h4><?= "Displayed data up to " . $EndDataDate ?><?= "<br>Log on ends on " . $EndTime ?>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="DeleteOldData.php"><span style="color:blue">Clear old data</span></a>
            <br>
        </h4>

        <div style="width:850px;padding:0px;float:left;">
            <!-- ... Rest of your HTML code ... -->
            <?php
            $stmt = GetAllData($Date);
            while ($row = $stmt->fetch()) {
                // ... Rest of your loop code ...
            }
            ?>
            <!-- ... Rest of your HTML code ... -->
        </div>

        <div style="width:670px;height:50px;padding:0px;float:left;">
            <input class="MyButton" type="submit" name="direction" value="Main Menu">
            <br>
        </div>
    </form>
</div>
</body>
</html>

 

you are seeing the raw php code in the browser because of the short opening <? tag. always use full opening <?php tags.

 

Quote

$pdo = connectDB();

you should not make a new database connection in every function. your main code should make one database connection, then supply it as a call-time parameter to any function that needs it. if doing this requires too much restructuring of the existing code, the connectDB() function should define a static variable to hold the connection, then  test for and only make a new connection if there is not already one.

the default setting in php8+ for PDO errors is to use exceptions for all the database statements that can fail. you should only catch and handle database exceptions for user recoverable errors, such as when inserting/updating duplicate or out of range data. in all other cases, simply let php catch and handle any database exceptions, where php will use its error related settings to control what happens with the actual error information, via an uncaught exception error (database statement errors will 'automatically' get displayed/logged the same as php errors.)

 

I am not ussing a secure type server if that is what you mean. I could not see any diffrences between your updated code on the first two functions except you removed the // error number and placed the opening brace at the end of the first line. It still does not work with that code.

 

As for  "secure/SecurePDO.php file that contains your PDO connection". I do have a file with all the database connection setiings in a seperate file which has worked under PHP 5.4. This functioned fine. So something dosn't work under PHP8. As for the mix of code mysql. I beleive all that has now been replaced with correct PDO codeing.

mac_gyver pointed out that I am not using <?php in the statment.php I have corrected this as for opening the connection every time. I was told to do this . . . connections.txt

And the web page has changed now because of <?php added

 

Still does not like fetch() that is the bug in the system  think. I was helped 6 months ago when trying to change the site to PDO. I am not mixing mysql with PDO here and it did work under PHP 5.4. I think it my webserver host has done something here.

Website-2.jpg

Connection.txt

Edited by Paul-D
6 minutes ago, Paul-D said:

mac_gyver pointed out that I am not using <?php in the statment.php I have corrected this as for opening the connection every time. I was told to do this . . .

And the web page has changed now because of <?php added

 

Still does not like fetch() that is the bug in the system  think.

Website-2.jpg

Connection.txt 689 B · 2 downloads

The provided code (connection.txt file) appears to be error-free. However, the success of the code depends on the context and how the functions are used in the rest of your script.

NB:

It's crucial to keep sensitive information like database login credentials secure.

all the PHP code together in one file. I can't see anything that is wrong with the PDO code here. It seems like the call to $stmt = GetAllData($Date); FAILS but it should not fail. It didn't under PHP 5.4

 

My-PDO connections .txt

Edited by Paul-D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.