/    Sign up×
Community /Pin to ProfileBookmark

Php Errors And Their Meanings

Php Folks,

Is this ok or do you have a better way of writing them ?

[code]
if(mysqli_connect_error())
{
echo “Connection Error!” . mysqli_connect_error();
echo “Connection Error Number!” . mysqli_connect_errno();
}
[/code]

Not sure if I asked you these or opened a thread about them but which do you use and can you rank these 3 so I know your rankings:

[code]
<?php

$link = mysqli_connect(“localhost”, “root”, “test”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. ” . mysqli_connect_error());
}

// Print host information
echo “Connect Successfully. Host info: ” . mysqli_get_host_info($link);

mysqli_close($link);

?>
[/code]

[code]
<?php

mysqli_connect(“localhost”, “root”, “”, “GFG”);

if(mysqli_connect_error())
echo “Connection Error.”;
else
echo “Database Connection Successfully.”;

?>
[/code]

[code]
<?php

$link = mysqli_connect(“127.0.0.1”, “my_user”, “my_password”, “my_db”);

if (!$link) {
echo “Error: Unable to connect to MySQL.” . PHP_EOL;
echo “Debugging errno: ” . mysqli_connect_errno() . PHP_EOL;
echo “Debugging error: ” . mysqli_connect_error() . PHP_EOL;
exit;
}

echo “Success: A proper connection to MySQL was made! The my_db database is great.” . PHP_EOL;
echo “Host information: ” . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

?>
[/code]

Or, if you have a better code then show me.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 21.2020 — Do not display error information to the world. Write it to a log file (see error_log(), for instance), and if needed then display some generic, user-friendly message to the end user that does not reveal database names, etc. to potentially malicious users.
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — Folks,

What is the difference between the 2 ?
<i>
</i>MYSQLI_REPORT_ERROR(); what goes in the params ?
mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);


I was meant to type the 2nd line in my notepad but accidently mistyped to the 1st line which you see above and it turned blue. Text blue. That means, Notepad++ recognised the 1st line above as a valid php. That is why I asking you what is the difference between the 2 as I know the 2nd one to be a valid php.
Copy linkTweet thisAlerts:
@NogDogJul 21.2020 — There is no mysqli_report_error() function in PHP.
Copy linkTweet thisAlerts:
@VITSUSAJul 22.2020 — @developer_web#1621063 I agree with NogDog, you do not need to display error information publically.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2020 — @NogDog#1621081

Show me how you write it to the log file then so I can learn from you.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2020 — @VITSUSA#1621091

Show me how you write it to the log file then so I can learn from you.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2020 — @NogDog#1621066

NogDog,

The 3 different error codes you see, I got them from 3 different tutorials. Last one from php manual.

Q1. Why tutorials teach to display error messages when experts like you advise not to because it would give hackers a structure of my website ? Others advise the same as you. Why these stupid tutorials, even the manual, teach as bad ways ?

Q2. Also, others advise not to close stmt or close conenction as mysqli would do it anyway. But tutorials teach to close them. Look:

https://www.php.net/manual/en/mysqli-stmt.bind-result.php

What is your take on this last question ?
Copy linkTweet thisAlerts:
@NachfolgerJul 30.2020 — > @developer_web#1621500 Show me how you write it to the log file then so I can learn from you.

I encourage you to learn how to do things on your own without your hand being held the entire time. Legitimately 95% of the questions you ask have been explored in great detail throughout the web--There's no reason you need @NogDog to explain how the error_log function works.

The PHP manual is a beautiful place, and it just so happens that they cover every PHP function, which includes [error_log](https://www.php.net/manual/en/function.error-log.php)

> @developer_web#1621508 Q1. Why tutorials teach to display error messages when experts like you advise not to because it would give hackers a structure of my website ? Others advise the same as you. Why these stupid tutorials, even the manual, teach as bad ways ?

This is basic logic, and frankly merits no time explaining. TUTORIALS utilize code that report errors for the sake of testing and explanation. It's not a bad thing to report errors to the public--**When _the public_ is ONLY YOU.**

> @developer_web#1621508 Q2. Also, others advise not to close stmt or close conenction as mysqli would do it anyway. But tutorials teach to close them.

The connection to a database is closed when the script is exited. Tutorials typically close the connection incase the code provided is used in conjunction with other code that continues beyond database operations. No one should be advising you NOT to close the connection.

I want to reiterate, **all of these questions asked could have been answered by existing resources (GOOGLE)**. As a developer it's expected that you have the capability to utilize the plethora of researching resources available to you. You're asking basic, and frankly stupid, questions constantly. I'm not trying to discourage your use of this amazing website, I'm simply asking that you think twice before wasting time needlessly asking vague questions. Do your own research, there's no need for members of this forum to regurgitate well known information just so you can be lazy.

I'll leave it at that.
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — @Nachfolger#1621513

Googling brings up tutorials which programmers at stackoverflow.com and other places claim are outdated and have bad code practices. Outdated tutorials waste our times. Hence, I prefer to bug the forums, like this one, so seniors like you who have learnt bad coding practices from tutorials or outdated tutorials and then faced troubles and then overcame troubles either by yourselves or learnt it how to overcome from your seniors and are now sitting with knowledge that is good practice coding. I just bug guys like you so you share the knowledge that you learnt the hard way so I don't have to go through the same troubles you folks went through by learning all the bad coding practices. Ok ? Now that is not too hard for you to understand. When I saw your Username in the above post, I thought, "Ah! I forgot about him. He gonna give some good practice coding advice. To my disappointment, I read whining."

Don't bother replying about this.

Anyway, I was pointed to a good practice tutorial on mysqli. pdodelusions.com.

But still, when I don't find what I am looking for then I will bug this forum. Ofcourse.

Might try @NachFolger from time to time. ;) Lol!

Tell me your expertise field, so when I deal with that part of the field, then I bug you addressing questions @NachFolger.

Say, do you have a website ? Might aswell check how good your coding is.
×

Success!

Help @developer_web 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,
)...