pdo - About fwrite in php -


i creating file in php using fwrite. need attach database index.php doubt can print below line on fwrite? have tried shows error. there way this?

$fd =fopen("database.php","w");  fwrite($fd, "$dbhost='localhost';     $dbuser='$user';     $dbpass='$pass';     $dbname='$email';     $dbh = new pdo('mysql:host=localhost;dbname=$email', $dbuser, $dbpass);");  fclose($fd); 

it's showing errors because variables aren't escaped. fix issue, either swap out quote markes single quote marks or add backslash () infront of $.

$fd=fopen("database.php","w");  fwrite($fd,"\$dbhost='localhost'; \$dbuser='\$user'; \$dbpass='\$pass'; \$dbname='\$email';  \$dbh=newpdo('mysql:host=localhost;dbname=\$email',\$dbuser,\$dbpass);");  fclose($fd); 

or quote mark fix:

fwrite($fd, '$dbhost="localhost"; $dbuser="$user"; $dbpass="$pass"; $dbname="$email";  $dbh = new pdo("mysql:host=localhost;dbname=$email", $dbuser, $dbpass);'); 

both should work.

if want pass variables text (like $user, $pass, $email), don't escape them.

$fd=fopen("database.php","w");  fwrite($fd,"\$dbhost='localhost'; \$dbuser='$user'; \$dbpass='$pass'; \$dbname='$email';  \$dbh=newpdo('mysql:host=localhost;dbname=$email',\$dbuser,\$dbpass);");  fclose($fd); 

quote version:

fwrite($fd, '$dbhost="localhost"; $dbuser="' . $user . '"; $dbpass="' . $pass . '"; $dbname="' .$email . '";  $dbh = new pdo("mysql:host=localhost;dbname=' . $email . '", $dbuser, $dbpass);'); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -