Log in

View Full Version : Help with database



taem
11-15-2015, 07:49 AM
Hello,

I'm struggling greatly connecting my php page to a database (this is my first time). I'm using c-panel and phpMyAdmin on my domain mmiles hosted by Arvixe.

1. First I went to MySql Databases and called it secure_login and the database has the name: mmiles_secure_login
2. Next I created user e*****s with a password of 1234****, then added user e*****s to the database
3. Then I logged into phpMyAdmin and on the left side, clicked mmiles_secure_login and created a table called User
4. On this table, I created several rows that can be seen in the structure for UserID, FirstName, LastName, Email, and Password

I have no idea where the mySql database saves, but I assume php knows where to look???

5. I add this blip of code to my index.php page:



<?php

$connection = mysql_connect("localhost","root","") or die("There was an error loading the database!");
mysql_select_db("mmiles_secure_login", $connection);
?>

I keep getting "There was an error loading the database!" So I tried changing 'root' to 'e*****s' and '1234****', but that also did not work. I've tried several other methods listed online but cannot even get past getting the php page to recognize a database even exists so usually can't even get past step one of every online resource I can find on the subject. Any ideas?

james438
11-15-2015, 07:06 PM
Try the following instead:

Hostname: 127.0.0.1
username: your_user
password: your_pass
database: sakila


$mysqli = new mysqli('127.0.0.1', 'your_user', 'your_pass', 'sakila');

reference (http://php.net/manual/en/mysqli.examples-basic.php)

Below is a more realistic example of how it looks for me and pulls and displays a title where the ID is "3" and the table name is "article". The hostname below is using random numbers instead of the actual numbers and the part that says "localhost" is renamed so as to protect the actual name of my database.


<?php
$conn = new mysqli("localhost.db.039850.hostedresource.com", "username", "password", "dbname");
mysqli_select_db($conn,"dbname") or die(mysqli_error());

$ID=3;
$get_addresses = "SELECT ID, title FROM article WHERE = $ID";
$get_addresses_res = mysqli_query($conn,$get_addresses);
$add_info = mysqli_fetch_array($get_addresses_res);
$title = $add_info['title'];
echo "$ID";
?>

Notice that mysqli() function is being used instead of the deprecated mysql().

taem
11-16-2015, 02:39 AM
Got it, thanks. Was actually the user name. The hosting site had the domain name appended to the front of the user name and once I took that into account, it worked fine.

james438
11-16-2015, 05:12 AM
I'm glad you were able to get it working, but I hope you moved away from the deprecated (outdated) mysql.

taem
11-16-2015, 05:14 AM
I'm glad you were able to get it working, but I hope you moved away from the deprecated (outdated) mysql.


Yes, I did, lol!

$conn = new mysqli($servername, $username, $password);