/    Sign up×
Community /Pin to ProfileBookmark

database functions

hi all,
I am working on a website and kind of stuck cos my database functions are not working.
connection to the server is not working.
also when hosting the website the connection is on the server so how do code the correct server(localhost)
Any help ? Pleeeeaaase.Thanks

/**
* connects to database server and selects database
*
@return bool
*/

function db_connect()
{
$connection = new mysqli(”,’username’,’password’);

if(!$connection)
{
return false;
}
if(!mysql_select_db(‘database’))
{
return false;
}

return $connection;

}

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 30.2019 — Passing an empty string for the database host is not going to work. If it's on the same host as your application, then 'localhost' or null should work, but not an empty string. If it's on another host, then you need to provide the correct hostname or IP address (and port number if it's not the default MySQL port number). See http://php.net/mysqli_connect#refsect1-mysqli.construct-parameters for more info.
Copy linkTweet thisAlerts:
@ginerjmJan 30.2019 — Besides nogdog's excellent post, you really should NOT get into the habit of using the @ on php code. It does nothing for you except hide the errors that your code may be producing during development.

And just what does a first line of code such as "return bool" mean to you? I'm sure it means nothing to the php interpreter! Even though you have it commented out at the moment, one has wonder why you even wrote a line such as it.
Copy linkTweet thisAlerts:
@nanasafoauthorJan 30.2019 — @NogDog#1600536

thanks @NogDog#1600536 ,

actually I took the localhost out. actually on my localhost or pc it works fine but when I load the files on a domain (server) it doesnt connect to the database. seems I need help with that.. i have already created the database, tables user password on the server the only thing left is to connect to the server so if you help edit the code I put out the I will be grateful.
Copy linkTweet thisAlerts:
@nanasafoauthorJan 30.2019 — @ginerjm#1600537

Thanks @ginerjm#1600537 for responding

The bool is commented out and i kind of label the codes to show what it does in the script for easy debugging and reference but the bool means if a connection is established then it sends displays the items in the database on the browser.

Thanks
Copy linkTweet thisAlerts:
@nanasafoauthorJan 30.2019 — @NogDog#1600536 thanks for the link..will go through it. appreciated
Copy linkTweet thisAlerts:
@NogDogJan 30.2019 — @nanasafo#1600538

Odds are then that your live web server uses a separate server for the database (which is very common), so you need to find out from your domain's control panel or whatever other resources you have what the database server name or IP is.
Copy linkTweet thisAlerts:
@ginerjmJan 30.2019 — "@return bool" returns nothing at all useful.
Copy linkTweet thisAlerts:
@NogDogJan 30.2019 — > @ginerjm#1600544 "@return bool" returns nothing at all useful.

That's within a multi-line comment, and is what [PHPDoc](https://phpdoc.org/) uses as special keywords within such comments (started with the double asterisk in the /** start of the block). I usually add a '*' on each line (and many php-friendly editors will do that automatically) to make it clear, e.g.:
[code=php]
/**
* This is a function for doing something amazing
* @return bool
* @param string $bar A description of the Bar
*/
function foo($bar)
{
//
}
[/code]

The usefulness of this is somewhat diminished in the latest versions of PHP if you are making full use of type-hinting -- and if your IDE understands it.
Copy linkTweet thisAlerts:
@SiriusGDJan 31.2019 — $dbhost = 'db1234567.db.godaddy.com';

$dbname = 'db1234567;

$dbuser = 'dbo1234567';

$dbpwd = 'dbpassword';

$connection = new mysqli($dbhost, $dbuser, $dbpwd, $dbname);

*Those are all fictitious values. You need to have that information from your domain's control panel with the password you set up for it.
×

Success!

Help @nanasafo 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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