Reputation: 273
As far as I know, the only way to get mysqli error info is:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
///RUN QUERY HERE, and if error:
$error=mysqli_error($con);
However (thinking I was smart) my whole software is programmed with a function that gets con info, as there are multiple databases and tables. So my queries look more like this.
$query=mysqli_query(CON("USER_DATABASE"),"INSERT INTO users (column) VALUES ("VALUE") ");
run_query($query); ///In this function I either run the query,
or record a failed query. However cannot record the specific error.
Is there any way at all to somehow still capture a mysqli_error(); without having the $con in the mysqli_error() function? Perhaps with the $query variable?
Upvotes: 0
Views: 196
Reputation: 157892
as there are multiple databases and tables.
The number of tables doesn't matter.
As if the databases, your application should have one connection variable per database(however, I would rather say that you are doing something wrong if you need multiple databases in a simple application).
Either way, there is a way to get Mysqli error without a connection variable. To make it possible , you should tell Mysqli to start throwing exceptions. Just add this line
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
So the simplest solution for you would be to make con() function static and return the same connection instance every time it is called.
But you should reconsider the architecture, and make it better organized, using just one database unless it's absolutely necessary and also use OOP to get rid of static functions.
Upvotes: 1
Reputation:
However (thinking I was smart) my whole software is programmed with a function that gets con info, as there are multiple databases and tables. So my queries look more like this.
Don't do that. Not only does it make it more difficult to retrieve error information, but -- more importantly! -- it means that your application will make a completely new database connection for every query. This will make your application significantly slower.
If your application really needs to connect to multiple databases, consider passing a symbolic name for the database as a parameter, and caching connections to all databases that have been used in a static variable.
Upvotes: 2