/    Sign up×
Community /Pin to ProfileBookmark

Mysql Connection Error Reporting Issue

Folks,

I see tutorials teach something like this:

[code]
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
if(!$conn = mysqli_connect(“localhost”,”root”,””,”buzzoff”))
{
die(‘Something went wrong. Please try again later!’);
}
$stmt = mysqli_stmt_init($conn);
$sql_count = “SELECT COUNT(id) FROM domains WHERE domain = ? OR domain_email = ?”;

if(!mysqli_stmt_prepare($stmt,$sql_count))
{
$_SESSION[‘error_report’] = __LINE__ .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error: ‘ .mysqli_error($conn) .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error Number: ‘ .mysqli_errno($conn) .”n”;
[/code]

Note the:
1st Code

[code]
mysqli_error() //empty inside brackets.
mysqli_errno() //empty inside brackets.
[/code]

But should not it be something like this instead ?
2nd Code

[code]
mysqli_error($conn) //NOT empty inside brackets.
mysqli_errno($conn) //NOT empty inside brackets.
[/code]

Here is the context …
3rd Code

[code]
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
if(!$conn = mysqli_connect(“localhost”,”root”,””,”buzzoff”))
{
die(‘Something went wrong. Please try again later!’);
}
$stmt = mysqli_stmt_init($conn);
$sql_count = “SELECT COUNT(id) FROM domains WHERE domain = ? OR domain_email = ?”;

if(!mysqli_stmt_prepare($stmt,$sql_count))
{
$_SESSION[‘error_report’] = __LINE__ .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error: ‘ .mysqli_error($conn) .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error Number: ‘ .mysqli_errno($conn) .”n”;
[/code]

Which code should I stick to ?
The 3rd one showed error the other night but tonight no errors.
Strange.
Hence, enquiring.

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @sempervivum

Do you have a little time at hand ? Do you mind chiming in ?
Copy linkTweet thisAlerts:
@SempervivumAug 19.2022 — @novice2022#1646054

Unfortunately not, I'm going to an event this evening.
Copy linkTweet thisAlerts:
@NogDogAug 19.2022 — Why not just try both ways and see what happens? Or even quicker, look at the prototype in the manual page, and note that for procedural style it shows it as having one required parameter, which must be of type mysqli (and also tells you it will return a string).

https://www.php.net/mysqli_error

> Procedural style
>
> mysqli_error(mysqli $mysql): string


If instead it had a default value, the the parameter would be optional, e.g. if it said...

mysql_error(mysql $mysal=null): string

...or something like that
Copy linkTweet thisAlerts:
@NogDogAug 19.2022 — PS: Or use a good code editor with PHP syntax validation that would highlight the error if you left out a required parameter.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @NogDog#1646057

And which code editor you know does exactly that ? I use NotePad++.

You once suggested an editor. I had trouble installing it. Can't remember the name now.

It required me to install so many plugins. Like one for php, one for this. One for that. And the plugins were out of date. And so, the editor did not work. Which one you suggest now that won't mess about like the other one ?
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — I did check manual.

But I do not understand the function syntax they talk about such as this:
<i>
</i> mysqli_error(mysqli $mysql): string

Now from your reply, I am now putting 2 and 2 together and guessing, anything after ":" means what it will return.

But what does the bit before the ";" mean ? I do not understand this part, what it means:
<i>
</i> (mysqli $mysql)

This is why I do not understand the manual. Now you lot understand why I evade the manual.
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — You're not showing any ; character!
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @Sempervivum#1646055

Ok. Just, do not get drunk and becareful no gold digging blondie drugs your drink to seduce ya and then scream rape later on and blackmail you out of money or drag you to court! Lol! ;)
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646060

What you mean ? Remember, I am self learning at home bugging people like you. I do not have any blondie class teacher to spoon feed me. Like you had in your college days.

Now, do not give a haughty reply.

Be constructive , my man! 🤣
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646060

Maybe you show some character then by telling me the ANSWER.

ChEeRs! (Now is this not some character ?) ;)
Copy linkTweet thisAlerts:
@NogDogAug 19.2022 — mysqli_error(mysqli $mysql): string says that the function mysali_error has one argument, which it is calling $mysql here just to give it a meaningful name. the mysqli that precedes it (with no punctuation between it and and the variable name) is a "type hint" saying that whatever is submitted to that function must be of type mysqli.

If you look at the procedural prototype for the mysqli_connect() function (https//php.net/mysqli_connect), you'll see at the end that it returns something (an object in this case) of type mysqli (or Boolean false if it fails):
[code=php]
mysqli_connect(
string $hostname = ini_get("mysqli.default_host"),
string $username = ini_get("mysqli.default_user"),
string $password = ini_get("mysqli.default_pw"),
string $database = "",
int $port = ini_get("mysqli.default_port"),
string $socket = ini_get("mysqli.default_socket")
): mysqli|false // <<---- return type
[/code]


As far as editors go, I use [VS Code](https://code.visualstudio.com/) and have the "PHP Intelephense" extension added to it. It's certainly not the only editor that has that sort of thing, but it is very popular. However, there will be some learning curve just because it offers much more than a "plain" editor (such as the ability to add a real-time syntax checker and explainer for PHP).

https://imgur.com/UrOrIYB
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — > @novice2022#1646059 Now from your reply, I am now putting 2 and 2 together and guessing, anything after ":" means what it will return.

> But what does the bit before the ";" mean ? I do not understand this part, what it means:


I'm asking you about the semi you mentioned in this quote from your previous real post.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646069

A common man! That was a typo.

I'm middle aged now and need glasses to read text on paper. But I aint bothered going to doc for glasses. For some reason can see text on pc and fone screens clearly. Maybe the issue on screens is starting right now. But most likely it was a typo. Was suppsoed to be colon. Ok ?
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — The part before the colon IS THE USE OF THE FUNCTION.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @NogDog#1646068

Thanks for the indepth explanation. I appreciate your time & effort.

I think it was VS Code that I was having trouble with. Nevermind. Downloading again.

And, I am sticking to these 2 codes. I hope they are ok and upto your standard:

Prepared Statement Error Reporting Code

DEV MODE CODE
<i>
</i>if(!mysqli_stmt_prepare($stmt,$sql_count))
{
echo 'Mysqli Statement Error: ' .mysqli_stmt_error(); //Nothing inside braces.
echo '&lt;br&gt;';
echo 'Mysqli Statement Error No: ' .mysqli_stmt_errno(); //Nothing inside braces.
echo '&lt;br&gt;';
die('Registration a Failure!');
}


Mysql Connection Error Reporting Code

DEV MODE CODE

require(conn.php)
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");

echo 'Initial Character Set is: ' .mysqli_character_set_name($conn); //Something inside braces.
mysqli_set_charset($conn,'utf84mb');
echo 'Current Character Set is: ' .mysqli_character_set_name($conn); //Something inside braces.

if(mysqli_connect_errno()) //Nothing inside braces.
{
printf("Mysqli Connection Error: %s",mysqli_connect_error()); //Nothing inside braces.
}


Is everything up there ok ?

Remember they are DEV mode code. We will get to User mode later.

Do note what is inside the braces. Whether empty or not.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646078

Cheers. But NogDog gave indepth explanation:

https://forum.webdeveloper.com/d/400417-mysql-connection-error-reporting-issue/12
Copy linkTweet thisAlerts:
@novice2022authorAug 20.2022 — @NogDog

Is my 2 code above ok in my previous reply to you? I can't see any reason why the code is buggy.
×

Success!

Help @novice2022 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...