Hack Forums
[PHP] Mysql Help - Hooking up php to mysql - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Mysql Help - Hooking up php to mysql (/showthread.php?tid=5102322)



Mysql Help - Hooking up php to mysql - NegativeRep - Dec 20, 2015

Just need some help with setting up mysql. I've got the hosting, server, website, bot, database.. only need aid with getting mysql properly hooked up.

If anyone can help please pm.

This is for a csgo jackpot website.


* - NON- - Dec 20, 2015

What do you mean "hooked up"? It should already work. All you'll need is your database user and password.
Code
<?php
$username = "x";
$password = "y";
$hostname = "z";

$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
?>
That will connect to the database. Then you need more code on the bottom to further interact with it.


* - PloxSource - Dec 23, 2015

(Dec 20, 2015 10:51 PM)NON- Wrote:  What do you mean "hooked up"? It should already work. All you'll need is your database user and password.
Code
<?php
$username = "x";
$password = "y";
$hostname = "z";

$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
?>
That will connect to the database. Then you need more code on the bottom to further interact with it.

That is one approach but mysql_ is deprecated, use mysqli_ or PDO (PHP Data Objects).

For PDO:

PHP Code

<?php 
$dbh 
= new PDO('mysql:host=localhost;dbname=test', $user, $pass); 

PDO Docs: http://php.net/manual/en/pdo.connections.php


* - Lightning827 - Dec 24, 2015

the way i like to do it is create a db.php so if the details of youre database everchange, all you have to do is edit this file, not the rest.

in db.php this is the code

Code
<?php
$db = mysqli_connect("host", "username", "password", "database");
?>


Now on all youre pages you want youre database to connect to just add

Code
include_once("db.php")