Php 5 tp PHP 7

Pls I need help in making this database work on php 7 Tx

<?php


// database connection config
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'courier_db';

$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());

function dbQuery($sql)
{
	$result = mysql_query($sql) or die(mysql_error());
	
	return $result;
}

function dbAffectedRows()
{
	global $dbConn;
	
	return mysql_affected_rows($dbConn);
}

function dbFetchArray($result, $resultType = MYSQL_NUM) {
	return mysql_fetch_array($result, $resultType);
}

function dbFetchAssoc($result)
{
	return mysql_fetch_assoc($result);
}

function dbFetchRow($result) 
{
	return mysql_fetch_row($result);
}

function dbFreeResult($result)
{
	return mysql_free_result($result);
}

function dbNumRows($result)
{
	return mysql_num_rows($result);
}

function dbSelect($dbName)
{
	return mysql_select_db($dbName);
}

function dbInsertId()
{
	return mysql_insert_id();
}
?>

Hello!
You need to use PDO or mqsqli.
Documentation for mqsqli: https://www.php.net/manual/en/book.mysqli.php

Im new to php and sql. I don really know how to. I Its a free script that i got from sourceforge. Pls I need help as my hosting only supports php 7

All of that code is crap that someone thought they could show how smart they were by abstracting something that is actually tightly bound. All of it is a pointless waste of time.

Programming help forums are for getting help with code you have written. We are not here to write code for you or to fix code someone else has written.

You would want to skip trying to use the mysqli extension, it is not a good choice, even when converting old mysql_ code.

Use the PDO extension, use prepared queries when supplying external/unknown data to an sql query statement, and use exceptions to handle database errors.

I have tried adding the mysqli extension but it seems not to work. The script works in php5

I think this should be asked on all of these threads… Are you interested in investing quite a a lot time learning how to program or are you only after a quick fix for this script? If it’s the latter then either find another script or pay someone to fix it for you.

1 Like

Wanting a quick fix and willing to learn programming

Are there a lot of queries? You will have to fix all of them to upgrade to a more modern programming standard.

is there a way I can attach the whole script

How about just posting the sourceforge url?

Some points for the converting the current code -

  1. Remove the or die() error handling and use exceptions.
  2. The query function needs the database connection and should have an optional call-time parameter added and conditional logic to support prepared queries.
  3. Both the affectedRows and InsertId functions should have the connection as a call-time parameter.
  4. If the selectDb function is actually used in the application, it needs to be changed to a USE … sql query.
<?php // database connection config $dbHost = 'localhost'; $dbUser = 'root'; $dbPass = ''; $dbName = 'courier_db'; $dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error()); mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error()); function dbQuery($sql) { $result = mysql_query($sql) or die(mysql_error()); return $result; } function dbAffectedRows() { global $dbConn; return mysql_affected_rows($dbConn); } function dbFetchArray($result, $resultType = MYSQL_NUM) { return mysql_fetch_array($result, $resultType); } function dbFetchAssoc($result) { return mysql_fetch_assoc($result); } function dbFetchRow($result) { return mysql_fetch_row($result); } function dbFreeResult($result) { return mysql_free_result($result); } function dbNumRows($result) { return mysql_num_rows($result); } function dbSelect($dbName) { return mysql_select_db($dbName); } function dbInsertId() { return mysql_insert_id(); } ?>

All this code is out of date, repetitive, in-line, bespoke built, and isn’t worth trying to learn anything from.

It is only calling the dbQuery(), dbFetchAssoc(), and dbNumRows() functions, so you would only need to update those. I would delete the rest of the functions.

There’s actually not that much database related code, so just replacing the existing calls with PDO based statements would end up taking about the same amount of effort.

The user security needs an exit; statement after each header() redirect to prevent the rest of the code from running.

Pls I need ur help. How do i do that?

You open up the database.php - there you find all the relevant functions. In each function you see some mysql_ ... function that yu have to lookup in the manual - go to php.net and insert the functions name into the search bar. Then you have have to find the corresponding function from this page:

https://www.php.net/manual/de/book.mysqli.php

that replaces the old function. you may want to set the connection global - that’s horrible for any relevant code, but this application is drastically outdated anyway. And that connection you have to set global in any function and provide it to the new mysqli_ ... function.

A tool like this may help you:

Sponsor our Newsletter | Privacy Policy | Terms of Service