So I'm attempting an assignment for uni through cloud9 and I am struggling to connect to my database. when I run the application this appears instead of connecting. what i see when running the application
I will post my current code, but it has all been approved by the teacher. and my code in my other .php files have been copied pasted from the teachers files (whose worked on her computer but when I cloned it (and also created a new workspace with it copied pasted) with her exact work the same picture appears.
<?php
require 'functions.php';
// Error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*MySQL connection settings */
$dbhost = "localhost";
$dbuser = "bmarino";
$dbpass = "";
$dbname = "movie_rentals";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//test if connection works
if (mysqli_connect_errno()) {
die ("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno . ")"
);
}
echo "connection successful";
?>
i was not the only person in my class to have this appear, and only one student was able to connect successfully (i'm not sure what they did)
Okay I figured it out. It was because I had my .php files in a folder, and for some reason when I took them out of the folder the preview loaded!
I would delete this question but I might leave it up in case anyone else encounters the same issue :)
Related
I have a local file (tf2stats.sq3) stored on my web server that includes name and points of players like so: https://i.imgur.com/u42zxha.png
My intention is to get the top 10 entries for POINTS and display them on a webpage as a leaderboard.
I can't find any examples on reading a .sq3 database without first connecting to a server.
Here's one of them:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
The problem is that the game server with the database does not have mySQL set up, so I planned on periodically downloading the file over FTP and reading it locally.
Right now I have the files stored on a directoy of my website: https://i.imgur.com/yPHbWIm.png
I have php-mysql and php7.0-mysql packages installed on my web server which is using php7.0 - so far every attempt I've made to read the db gives me "No such file or directory"
I'm very new to php and sql, any help is greatly appreciated.
Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');
We are trying to create a login screen for a WordPress website. I think the way to connect to the database is good. The code also seems to be good, we have a layout where someone types the username and password. Those are stored in variables and then it should connect to a database.
Before the following lines of code the ?>
TEST prints out TEST. However when you try to login the error 500 pops up, and no print of TEST. The error code 500 is very wide unfortunately.
We are working outside the code of WordPress in a different folder. WordPress has 3 folders on the server named wp-admin, wp-content and wp-includes. We just created a folder next to it and are trying to build it there. I'd like to find out the options why it is not working, some internet research here brought me to the wp-config. But that didn't work out for us yet.
$connection = mysql_connect("IP", "username", "password");
?>
<HTML><BODY>TEST</BODY></HTML>
<?php
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("db_name", $connection)
or die("no connection to database");
I can add the code of the login screen as well if its necessary, just comment if that is needed.
**** I used old functionalities of PHP and that is why it is not connecting. For WordPress do not use mysql_connect but mysqli_connect.
The best way to load only load the core functionality of WordPress is to use the wp-load.php.
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
include_once $path . '/wp-config.php';
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test the connection:
if (mysqli_connect_errno()){
// Connection Error
exit("Couldn't connect to the database: ".mysqli_connect_error());
}
the connection to database via PHP is as:
<?php
$DbHost = 'localhost';
$DbName = 'root';
$DbUser = 'root';
$DbPwd = '';
$dbConn = mysql_connect ($DbHost, $DbUser, $DbPwd)
or die ('MySQL connect failed. ' .mysql_error());
mysql_select_db($DbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>
Here I've hardcoded my hostname, username and password. Everytime I take my project to another machine, I've to change it.
How can I make this connection code dynamic?
You can put machine specific information in a settings.php file and use
require_once "settings.php";
to get hold of the machine settings. Do this on all your machines and put the file in a specific place so your project(s) is/are able to find it.
When you move your project to another machine, it will use the corresponding settings.php file. (Assuming you are not copying this file as well ;) )
I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>