Php connection error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Php connection error?

This code deceiving me by not getting execute this code shows error as well as connected....show me the right way of doing this...or pls tell why? https://code.sololearn.com/wJUJRtpmkuRl/?ref=app

5th Feb 2019, 7:06 PM
R H
R H - avatar
10 Answers
+ 1
if you want to prevent error from being shown just put this line on top level of your code: error_reporting(~E_ALL);
5th Feb 2019, 8:14 PM
MO ELomari
+ 1
try this on your local but not in sololearn playground: <?php $db = []; $db['db_host'] = 'localhost'; $db['db_user'] = 'root'; $db['db_pass'] = ''; $db['db_name'] = 'cms'; foreach ($db as $key => $values){ define(strtoupper($key), $values); } $connection = mysqli_connect( DB_HOST, DB_USER, DB_PASS, DB_NAME ); if($connection ){ echo "connected"; } ?>
5th Feb 2019, 7:45 PM
MO ELomari
+ 1
What is wrong on mine...just the empty declaration of db 😕??
5th Feb 2019, 7:46 PM
R H
R H - avatar
+ 1
you're not explicitly declared "$db" variable
5th Feb 2019, 7:48 PM
MO ELomari
+ 1
But why it is then shows connected on my local texteditor
5th Feb 2019, 7:49 PM
R H
R H - avatar
+ 1
you're not getting any kind of errors ?
5th Feb 2019, 7:54 PM
MO ELomari
+ 1
"Notice undefined variable db and so so."...and then at last shows me connected
5th Feb 2019, 7:55 PM
R H
R H - avatar
+ 1
Actually i am using the post method to display dynamically on my browser...but due to that notice error on connection time i am misleading...
5th Feb 2019, 8:02 PM
R H
R H - avatar
+ 1
and the reason why it's always show connected because mysqli_connect function always return an object of type mysqli: http://php.net/manual/en/mysqli.construct.php#refsect1-mysqli.construct-returnvalues
5th Feb 2019, 8:07 PM
MO ELomari
+ 1
👍
5th Feb 2019, 8:11 PM
R H
R H - avatar