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());
}
Related
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 :)
I realise there are a lot of questions like this here, but none of them seem to help me. The problem is as follows:
I have a website with an SQL Database, and now I am developing an application that reads the data. I've read that the best way to do that is a web service, so I'm in the process of writing one now. First, I'm trying to get all the data to display on a webpage, for testing purposes. I have the following code now:
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$db = "mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $db, 3306);
// Check connection
if (!$conn){echo "Not connected";} else {echo "connected";}
$query = "SHOW TABLES FROM mydata";
$result = mysqli_query($query) or die(" but not executed" . mysqli_error());
?>
When I save this and go to mywebsite/test.php, it displays the following:
connected but not executed. That means that it connects right, but the mysqli_query() returns false. However, mysqli_error is empty.
This is the entire test.php file. Am I missing something?
"Am I missing something?"
Yes, db connection arguments to both mysqli_query() and mysqli_error().
RTM's
http://php.net/manual/en/mysqli.query.php
http://php.net/manual/en/mysqli.error.php
It's all in there.
Btw, this is a community wiki. No rep should come of this.
If that still doesn't work, then you may have to remove the port , 3306.
Your $query appears to say
SHOW TABLES FROM mydata
However, your database is actually called mydatabase, as demonstrated on the $db line.
I just installed MAMP and have created a MYSQL database. I can access it via PHPMYADMIN.
In my php page I have this, pasted directly from the MAMP webstart page--
$user = 'root';
$password = 'root';
$db = 'local_db';
$host = 'localhost';
$port = 3306;
$link = mysql_connect(
"$host:$port",
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
The resulting page stops at this point, won't print anything below these instructions.
I've tried changing the port in the MAMP preferences. I also included or die("Could not connect"); after the first line, but still don't get any text after the link data in the page.
I checked online, and others with the problem at least see the die text. I don't get that.
I haven't changed any passwords or data other than mess with the port number.
Any help would be appreciated!
Please give the following a try, I have developed and tested it locally, functionality within has been documented to help you understand what is going on in every step.
/**
*
* Modern method of connecting to a MySQL database and keeping it simple.
*
* If you would like to learn more about PDO,
* please visit http://php.net/manual/en/book.pdo.php
*
*/
//Set up database connection constants, so they cannot be changed.
define('DBHOST','127.0.0.1'); //Change this to the ip address of your database
define('DBNAME','test'); // Change this to the database name you are trying to connect to.
define('DBUSER','databaseuser'); // Insure this user is not the root user!!!!
define('DBPASS','databasepassword'); // Insure this is not the root password!!!!
//Let's try to connect to the database first.
try {
//Initiate a new PDO object called $MYDB and pass it the proper information to make
//the connection
$MYDB = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME."", DBUSER, DBPASS);
//If we are successful show it :D for the test page, if this is for production you should not show this.
echo "Database connection was successful.";
//If this does not worth catch the exception thrown by PDO so we can use it.
} catch(PDOException $e) {
//Show that there was an issue connecting to the database. Do not be specific because,
//user's do not need to know the specific error that is causing a problem for security
//reasons.
echo "Oh, sorry there was an issue with your request please try again.";
//Since we had an issue connecting to the database we should log it, so we can review it.
error_log("Database Error" . $e->getMessage());
}
//Since this is 100% php code we do not need to add a closing php tag
//Visit http://php.net/manual/en/language.basic-syntax.phptags.php for more information.
If you have any issues with this please attempt to break it up into smaller pieces while reviewing the PDO documentation.
I'm very new to PHP and mySQL and I've been trying to create a logon via xampp with apache and mySql server. I keep on getting this error can someone explain what the error actually means? I've seen some people ask this question but the answer usually only pertains to their code especially. I'll provide my code as well. I've been watching this series of youtube tutorials if anyone is interested in what I have done http://www.youtube.com/watch?v=NuiTzSdGmKM . Hope someone can help thanks! My code below:
<?php
$username = "yolo";
$password = "swag";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("login", $dbhandle);
$myusername = $_POST['user'];
$mypassword = $_POST['pass'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM logon WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
echo 'It worked!';
}
?>
The error is about establishing the connection to a mySQL server.
(User 'adminryan' can't connect to server 'localhost' and a password was used trying to connect.
This can have several reasons, like:
user adminryan doesn't exist.
the provided password is wrong.
the provided servername is wrong.
user adminryan doesn't have the proper privileges.
etc...
your code says:
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
so I wonder why you see the error given by you instead of "Could not connect to database".
I would expect mysql_connect would return FALSE and therefore die("Could not connect to database") would be executed. (And all code after die(...) wouldn't be executed.)
It has nothing to do with $query = ...
I can't, but I would vote down the comments of MuhammadAli and ArtisticPhoenix because they don't make any sense in relation to the error you provided.
And following your code this line won't be executed.
Do switch to PDO or at least mysqli though.
For the purposes of having a possible solution for others that encounter this error and confirm the username, password and database do exist. I was coming across the same error and when restructuring my requests I was able to get the desired result.
$username = 'username';
$password = 'password';
$db = mysql_connect('localhost', $username, $password);
mysql_select_db("database",$db);
In my instance I have the full username and database being used. So it seems to depend on your cpanel account username. So for example if my domain cpanel login was mydata, then my username would be mydata_username and my database would be mydata_database.
Now I don't know if that matters or not, I haven't tested it the other way around, I have had this error a few times and this is a solution that worked for me. I don't know why it worked as I'm no expert but it did and it's worth a try for others who are experiencing the same issue.
The only difference between this code and my earlier code which is similar to this error is my use of quotes. This code is using single quotes ', the earlier code used double quotes ".
hi im fairly new to cms made simple and ive stumbled upon a problem thats beyond me, my coad is
<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$connect = mysql_connect($dbh, $dbu, $dbp) or die ('Error connecting to mysql');
$yatzi = 'myposts';
mysql_select_db($yatzi);
echo "hello";
?>
and im using this through a user defined tag to import a php file, the proble is that everytime i load this page an error pops up saying:
string(61) "Smarty error: unable to read resource: "globalcontent:footer"" string(61) "Smarty error: unable to read resource: "globalcontent:footer""
and everything gets messedup, i seriously have no idead what is going on, can anybody please help me,, thnks...
If this connection is to the same db server the issue could be that youre overwriting the connection resource and thus your CMS cant pull anything form the db.
This would be because by default PHP will detect that you already have a connection open and return that one if they share the same parameters. You can override this behavior by forcing a new connection:
$connect = mysql_connect($dbh, $dbu, $dbp, true);
Then when using this server you need to make sure you always specify which link to use:
mysql_select_db($yatzi, $connect);
mysql_query($query, $connect);
// etc...
Ohter possible issues might be that you have the code in the worng place (like directly in a Smarty template file without the special php escape tags surrounding it), or that the problem isnt related to your code at all and something is up with your CMS installation or customization.
<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$yatzi = 'myposts';
$connect = mysqli_connect($dbh, $dbu, $dbp,$yatzi) or die ('Error connecting to mysql');
echo "hello";
?>