Sunday 15 September 2013

php - mysqli_connect() expects parameter 1 to be string, array given -



php - mysqli_connect() expects parameter 1 to be string, array given -

i have issue mysqli_connect expecting parameter string , apparently array given

i have php file writes mysqli_connect info file variables "userconnection.txt"

'localhost','root','','forum'

then have standard php connection.php file added userconnection.txt file host user , password.

<?php $connectionlink = file('userconnection.txt'); $connection = mysqli_connect($connectionlink); if (!$connection) { die('connect error: ' . mysqli_connect_error()); } ?>

it spits out error:

warning: mysqli_connect() expects parameter 1 string, array given in /applications/xampp/xamppfiles/htdocs/forum/install files/connection.php on line 4 connect error:

this not work , security risk.

security

the txt file might accessibe website via url. attacker know url (not hard @ all) can file , has login credentials. save file outside webroot or alter it's extension , contents arent publcly avaiable. maybe php file?

error

you have 2 problems. first 1 file returns lines of file individual lines , not 1 string. utilize file_get_contents instead.

anyways, if this, still have 3 parameters in 1 single variable of type string. mysqli_connect function needs 3 seperate variables , not one. can prepare explode.

quick , dirty solution

$filecontents = file_get_contents('userconnection.txt'); $connectionvars = explode("," $filecontents); $connection = mysqli_connect($connectionvars[0], $connectionvars[1], $connectionvars[2]); if (!$connection) { die('connect error: ' . mysqli_connect_error()); }

better more complex solution

create php file connection variables:

<?php $dbhost = "host"; $dbuser = "user"; $dbpass = "pass";

change connection file this:

<?php require("connectionsettings.php"); $connection = mysqli_connect($dbhost, $dbuser, $dbpass); if (!$connection) { die('connect error: ' . mysqli_connect_error()); }

php arrays string mysqli

No comments:

Post a Comment