So I was following a tutorial to make an sql database and connect to it, I had a login system working on my personal device but when I went to put it on my domain I wasn't sure what to fill out in the file that creates a connection to the database. Here is the code:
Code:
<?php
$dbServername = "";
$dbUsername = "";
$dbPassword = "";
$dbName = "";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
Here's how I filled it out, I don't think this is right:
Code:
<?php
$dbServername = "https://server225.web-hosting.com:2083";
$dbUsername = "mycontrolpanelusername_nameofuserlinkedtotable";
$dbPassword = "passwordlinkedwithtableuser";
$dbName = "mycontrolpanelusername_nameofdatabase";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
Pretty sure your port is wrong. 3306 is the default port for MySQL.
Beyond that, you can add in Debug code to check the connection:
Code: // Create connection
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
This will tell you what exactly is failing and you'll be able to take appropriate steps. Adding debug code is incredibly helpful. I add debug code to my stuff then comment it out when it's not needed - aka when I've figured it out. I leave it commented out because that way if I ever need to debug again, I have the same points in the code to debug from. However, I would leave the above debug functional. But that's up to you.
And for the love of Cemetech, don't concatenate anything into your SQL query unless you are certain it is being sanitized.
So I was messing around with some different things and changed the port but still can't get it to work. Am I missing something obvious with the server name here? Again, I have never connected to a server-side database.
dankcalculatorbro wrote:
So I was messing around with some different things and changed the port but still can't get it to work. Am I missing something obvious with the server name here? Again, I have never connected to a server-side database.
Do you actually own that domain? You can always just use the ip address instead; it might be a DNS issue.
How did you get the URL for the MySQL server host? What if you try "localhost"? That's what I have in my MySQL config and I'm on shared hosting.
I tried "localhost" and it worked. Thank you everyone for your help.
So I wrote a piece of code that should use _get to detect if there is an error by checking the url of the website, and then output that error. The problem currently is that it always outputs the first error, no matter what the url says. When using === instead of ==, it doesn't do anything at all.
An example of what the url would look like is something like this: /signup.php?signup=empty
Code:
<?php
if (isset($_GET['signup'])=='empty') {
echo 'The sign up is empty';
} else
if (isset($_GET['signup'])=='nameinvalid') {
echo 'Names can only contain A-Z';
} else
if (isset($_GET['signup'])=='emailinvalid') {
echo 'Please enter a real email';
}
Examine your conditions closely and consider
what isset() returns.
Yes, I realize now what my mistake was. I will update this post soon with my fixed code for anyone else who finds it helpful to use once I correct everything.
EDIT: Below is the fixed code. I don't know why I thought I had to use isset(), it wasn't necessary for the task I was trying to accomplish.
Code:
<?php
if ($_GET['signup']=='empty') {
echo 'The sign up is empty';
} else
if ($_GET['signup']=='nameinvalid') {
echo 'Names can only contain A-Z';
}
To update data in MySql, I can use this piece of code, which works fine:
Code: $userid = 1;
$sql = "UPDATE user SET char_name='$name' WHERE user_id='$userid';";
mysqli_query($conn, $sql);
But when using this code (the change: $userid is set to a session variable), the database is not updated.
Code: $userid = $_SESSION['u_id'];
$sql = "UPDATE user SET char_name='$name' WHERE user_id='$userid';";
mysqli_query($conn, $sql);
Why is this the case?
What happens if you put an echo statement in to debug?
Code: $userid = $_SESSION['u_id'];
echo("The user ID is $userid<br>");
$sql = "UPDATE user SET char_name='$name' WHERE user_id='$userid';";
mysqli_query($conn, $sql);
I bet it's somehow returning no value at all. If that's the case you'll have to keep adding in various debug points to see where this issue is occurring. A good place to start would be when the UserID is selected. That said, do you have PHP error reporting enabled?
Yep, found the error. You were correct that it wasn't returning a value; I missed a session_start() somehow.
Alex wrote:
That said, do you have PHP error reporting enabled?
I will have to enable it. I do not believe I have it on currently.
dankcalculatorbro wrote:
I will have to enable it. I do not believe I have it on currently.
Code: error_reporting(E_ALL);
ini_set('display_errors', true);
Put that in your header.php or whatever file is your header. You can always comment it out later
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum