Jump to content

Error trying to convert from 5.x to 7.3.9


Kaz3367

Recommended Posts

As to the manual, "die" is an alias of "exit" which is a function, and there are examples in the manual which you should try on a standalone script first, so you learn how to call functions with the appropriate syntax.

https://www.php.net/manual/en/function.exit.php

Also i would recommend to not just randomly copy and paste function signatures into production code and hoping that they work as you expect, but actually learn how PHP works and what syntax is needed from the examples.

Edited by chhorn
  • Like 1
Link to comment
Share on other sites

3 hours ago, Kaz3367 said:

I wanted it to return me a string of the error if there's one

if you use exceptions for database statement errors and in most cases let php catch and handle the exception, php will use its error related settings to control what happens with the actual error information (database errors will 'automatically' get displayed/logged the same as php errors.)  you will be able eliminate the existing error handling, rather than to convert it. to enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

now the bad news, it is not enough to just update statements in old code, as one of the things that was removed in php5.4, magic_quotes, eliminated some protection that php provided against sql special characters in external string data from breaking the sql query syntax (which is how sql injection is accomplished.) if your code does not have specific protection for all external/unknown data, and for all the different data types, being put into sql queries, you will need to add it. the simplest, universal way of doing this is to use prepared queries, provided that you switch to use the simpler PDO extension. the prepared query interface for the mysqli extension is overly complicated and inconsistent 

  • Like 1
Link to comment
Share on other sites

On 11/13/2019 at 6:16 PM, mac_gyver said:

if you use exceptions for database statement errors and in most cases let php catch and handle the exception, php will use its error related settings to control what happens with the actual error information (database errors will 'automatically' get displayed/logged the same as php errors.)  you will be able eliminate the existing error handling, rather than to convert it. to enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

now the bad news, it is not enough to just update statements in old code, as one of the things that was removed in php5.4, magic_quotes, eliminated some protection that php provided against sql special characters in external string data from breaking the sql query syntax (which is how sql injection is accomplished.) if your code does not have specific protection for all external/unknown data, and for all the different data types, being put into sql queries, you will need to add it. the simplest, universal way of doing this is to use prepared queries, provided that you switch to use the simpler PDO extension. the prepared query interface for the mysqli extension is overly complicated and inconsistent 

Thanks a lot for your indepth answer.

I will try to get more into this matter then.

 

Also another question, we used an excel export code, now that I converted most of the code I came along this following part of code which does no longer exist in PHP 7.

I searched for alternatives and I am not sure how to implement them in this specific example, I hope im not asking too much but Im kinda lost..

image.png.036f2dbfe9dc26f0c026deb3d4846193.png

mysql_field_name 

 

Tia

Link to comment
Share on other sites

If your problem is how to output tab separated data with column names

/*
SAMPLE DATA
+-------+-------+---------+------------+
| empid | fname | lname   | dob        |
+-------+-------+---------+------------+
|     1 | Peter | Smith   | 1985-01-26 |
|     2 | Paul  | Hartley | 1973-12-02 |
|     3 | Mary  | Baker   | 1980-04-11 |
|     4 | Jane  | Doe     | 1990-11-28 |
+-------+-------+---------+------------+ */

$res = $db->query("SELECT empid
                        , fname
                        , lname
                        , dob
                   FROM employee;
                  ");
echo '<pre>';
$row = $res->fetch(PDO::FETCH_ASSOC);
echo join("\t", array_keys($row)) . "\n";              // headings
do {
    echo join("\t", $row) . "\n";                      // data
} while ($row = $res->fetch());
echo '</pre>';

Which gives

image.png.93f63159541b10be2adbdb77d2f346c3.png

Similarly, if you want to write it to a csv file for export to Excel, then

$res = $db->query("SELECT empid
                        , fname
                        , lname
                        , dob
                   FROM employee;
                  ");
$fp = fopen('AAA.csv', 'w');
$row = $res->fetch(PDO::FETCH_ASSOC);
fputcsv($fp, array_keys($row), "\t");                         // headings
do {
    fputcsv($fp, $row, "\t");                                 // data
} while ($row = $res->fetch());

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.