search

Home  >  Q&A  >  body text

Regarding the problem of custom methods in mysqli, please solve it?

QQ图片20170918145956.png

When using $mysqli in a custom function, an error is reported. The red line at the bottom of $mysqli indicates QQ图片20170918150003.png

Although $mysqli reports a syntax error, it is marked with a red line. , but the data is still queried, but the loading is super slow, taking dozens of seconds, which is obviously abnormalTZUMIA06_EE@A(DUY]()0OW.png

This is the database configuration

别闹i别闹i2709 days ago1798

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-09-18 16:16:28

    It is recommended to place require 'mysqli_connect.php'; outside the custom function, at the top of the program. Using the $mysqli variable in a function requires a global declaration. Write global $mysqli;

    at the top of the function body.

    reply
    2
  • 别闹i

    Warning: mysqli::query(): Couldn't fetch mysqli in D:phpStudyWWWmysqli_objmysqli_insert.php on line 52 //Thank you, I adopted your method, no syntax error was reported, and the above error was reported when loading

    别闹i · 2017-09-18 16:32:46
    别闹i

    global $mysqli;---unset($mysqli);//After defining the global variable, I destroyed it at the end of the method. Is this necessary?

    别闹i · 2017-09-19 14:41:08
    PHP中文网

    Just delete the line $mysqli->close(); and it will be fine.

    PHP中文网 · 2017-09-19 09:01:03
    PHP中文网

    The db connection has been closed after this first visit, so new connections cannot be generated.

    PHP中文网 · 2017-09-19 09:01:39
    PHP中文网

    You should destroy it when the page ends and should not be placed in a function, otherwise the database will be closed the second time you call the function.

    PHP中文网 · 2017-09-23 10:51:29
  • Cancelreply