Jump to content

Connecting To Mysql Database


milan

Recommended Posts

Hi eveyone!

 

I am using the following code to connect to my mySQL database:

 

<?php
// Create connection
$conn = mysqli_connect('localhost', 'database_user_name', 'database_password');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_select_db($conn,"database_name");
?>

 

I tried several variants of this, replacing parameters:

mysqli_connect('tommy.heliohost.org', 'database_user_name', 'database_password');

mysqli_connect('localhost', 'database_name', 'database_password');

mysqli_connect('tommy.heliohost.org', 'database_name', 'database_password');

 

None of those seem to work. Could you please help me out figure out which host, username and password I should use to make this work. To note, my website is currently hosted on tommy.heliohost.org. Is this also true for mySQL database?

 

In the next step I will be trying to connect remotely and it would be cool if you also let me know what the hostname for the external world looks like, and how do i log onto it.

 

Thank you very much!

 

Best,

Milan

 

 

 

 

 

Link to comment
Share on other sites

To connect from a script running on the server:

$con = new mysqli('localhost', 'milanan_user', 'bestpasswordever', 'milanan_database');
To connect from a remote script just use tommy.heliohost.org as the host instead of localhost.
  • Like 1
Link to comment
Share on other sites

Hey, Krydos, thanks! It worked on the local domain!

When I try to connect remotely I get the following error:

XMLHttpRequest cannot load [path to my php file connect file]

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

 

Any advice on how to go from there?

 

Thanks a lot!

 

Best,

Milan

Link to comment
Share on other sites

Dear Krydos,

 

Here's the error once again with the actual path to my local php file:

 

XMLHttpRequest cannot load file:///C:/Users/milan/JavaScript/MySQL_posting/heliohost_mySQL_connectMethod_remote/userInfo.php. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

 

Best,

Milan

Link to comment
Share on other sites

That's a security error. Read this https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local

Imagine if that is allowed and a webapp whereby the author of the page uses something like load('file://C:/users/user/supersecret.doc') and then upload the content to their server using ajax etc.

Basically, if the file is hosted via https/http/etc you can assume the owner of the file understands that it's being shared. If it's a path like file://c:/ that means it could be ANYTHING on your harddrive, which you don't necessarily want someone on the internet being able to access.
Link to comment
Share on other sites

Hi Krydos,

 

Thanks for your explanation, that is very helpful. I tried some of the followup suggestions from the stackoverflow post: running a local server using python, or using the npm http-server in windows command line, but those servers do not support the ajax post method. I also tried opening chrome with --allow-file-access-from-files but that doesn't seem to change anything.

 

Do you have any recommendations/pointers for a simple solution?

 

Thanks a lot!

 

Milan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...