I've looked through StackOverflow for an answer to this and have so far come up empty handed. I know some have had a similar issue, but so far none of the responses to the issues have worked.
So I work on two sites, both of which are on the same host with the same PHP Admin set up (different accounts and domains, sites are not affiliated). One of them uses MySqli perfectly, without issues, while the other either gives a database selection failure, localhost access denied with password as "NO" or a blank page when I attempt to replace the MySql with MySqli.
The deprecated code:
$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";
$connection = #mysql_connect($host,$usr,$pwd);
if(!$connection){
die("No connection.");
}
mysql_select_db($dbname,$connection);
The MySqli I am attempting (identical to the site that MySqli works perfectly on):
$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";
$connection = mysqli_connect($host,$usr,$pwd);
if (!$connection) {
die("No connection.");
}
$db_select = mysqli_select_db($connection, $dbname);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));
}
So the PHP error codes did not work at all and it had nothing to do with the local host or port, so I first checked to make sure mysqli_connect was on. Next, I ran a simple test where I echoed a random word before each mysqli command.
echo 'Passed.';
global $c_mysqli;
$conn = new mysqli($host, $usr, $pwd, $dbname);
echo '<br>Passed global mysqli.';
I found that my second pass was not working, and then proceeded to do an error message. An error showed up then. However, nothing I did fixed the issue... then I realized that one of my files, the header file, is the meat and potatoes and if it were to fail then everything else would fail too.
Sure enough, one of my lines of code contained a mysql connection and not mysqli. Long story short, double check and triple check to make sure that all
$statement = mysql_query("STATEMENT");
$query = mysql_fetch_array($statement);
Appear as
$statement = $conn->query("STATEMENT");
$query = mysqli_fetch_array($statement);
Related
I'm trying to connect to a database in phpmyadmin. I'm new to the process and unsure how to connect to it.I'm using godaddy to host. I have this line of code:
$db = mysqli_connect("https://.....secureserver.net", "...", "....", "authenticationdb_");
I went to the table on phpmyadmin and copied the url, then copied the username (the first "...") and password (the second "...") that I used to login in to godaddy, and the name of the database is authenticationdb_.
This is not working and I'm not sure why. I was unsure if the username and password were the ones that I used to login in to godaddy but I don't know what else they would be since i accessed phpmyadmin thru godaddy.
<?php
$servername = "localhost";
$username = "username"; //your user name for php my admin if in local most probaly it will be "root"
$password = ""; //password probably it will be empty
$databasename = ""; //Your db nane
// Create connection
$conn = new mysqli($servername, $username, $password,$databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I am having an issue with connecting my PHP script to the database on my localhost server.
I have posted the code below, it is to enable user registration on the site.
The input boxes appear as they should when I run the code, but nothing updates to the database when I try and complete a sign up.
As a novice with PHP I don't know enough about it to spot any errors I might be making, or what they mean.
Any help on this subject would be appreciated as there is a lot of info about PHP online, but I would rather know what was causing this error in order to prevent it in the future.
Here are the errors appearing in the browser console:
Failed to load resource: the server responded with a status of 404 (Not Found)
ReferenceError: Can't find variable: $
And the UNIX socket code from MAMP (I don't know where this would fit in):
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
And the PHP code:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
if (isset($_POST['register_btn'])) {
session_start();
$username =mysql_real_escape_string($_post['username']);
$email =mysql_real_escape_string($_post['email']);
$password =mysql_real_escape_string($_post['password']);
$password2 =mysql_real_escape_string($_post['password2']);
if ($password == $password2) {
//create user
$password = md5($password); //hash password before storing for security
$sql = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
}else{
$_SESSION['message'] = "Your passwords must match to proceed";
}
}
?>
Where to start? So many problems.
First off, you are using the OLD mysql functions which have been removed entirely in recent versions of PHP. Use the mysqli functions instead. The old functions like mysql_connect and mysql_query have been deprecated. You need to look for all occurrences of mysql_ in this code and think about replacing each command with its new counterpart.
You define this code to connect:
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
and then you don't use the resulting connection -- even check if it worked. You should always check the value returned by mysqli_connect to see if it actually worked or if it returned FALSE. You reconnect again and don't bother checking to see if it worked:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
And in doing so, you redefine $db to something else.
Also, you run a query without checking whether it succeeded or not:
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
You should be checking the result of mysqli_query (not mysql_query as you have in your code) to see what it returned. It should be TRUE if the INSERT query worked.
And after you redirect, you fail to call exit, which means that all the code that follows your redirect attempt may end up actually executing anyway.
I'm trying to connect to the MySQL server using MySQLi. It is enabled in the server end where this code runs. I am unable to connect to the server using the following information. Did I miss something? Any help would be appreciated. Thank you.
<?php
// DBInfo
$hostname = "localhost";
$username = "nc5ff";
$password = "Ni\$hant809472";
$dbname = "test";
// DB Connection
$link = mysqli_connect($hostname, $username, $password, $dbname);
/* check connection */
if (!$link) {
header("Connection Failure", true, 777);
}
mysqli_close($link);
?>
You have to escape the password as it contains special php characters (\ and $):
$password = "Ni\\\$hant809472";
Make sure you have all of the information right i don't see anything wrong with the code, but please make sure you have all the information right.
I wrote an addon module for our WHMCS billing system a long time ago that we recently realized was causing some issues. Essentially each module's PHP file is loaded regardless if it is actually used or not, where this is how their "hook" system is setup.
When I wrote the module, I included my "db_config.php" file at the top in the global space, which I now realize is causing this database to load every page and is apparently being written to when it shouldn't be. As this is the case, I would like to open the Database connection at the top of the function and close it at the end of the function.
I've never seen this done before nor can I find much information on it. The contents of my db_config.php appear as follows and I am wondering if I can just include_once() inside of the function?
<?php
// Connection's Parameters
$hostname = "xxx.xxx.xxx.xxx";
$database = "database";
$username = "username";
$password = "password";
// Connection
$tca_conn = mysql_connect($hostname, $username, $password);
if(!$tca_conn)
{
die('Cannot Establish Connection to Database : ' . mysql_error());
}
$tca_db = mysql_select_db($database, $tca_conn);
if (!$tca_db)
{
die ('Cannot Select Database : ' . mysql_error());
}
?>
Try this one.It might work for you.
$tca_db = mysql_select_db($database);
instead of
$tca_db = mysql_select_db($database, $tca_conn);
I am very new to php. I am trying to make a sign up page with php. So simple but I can't. I've searched across google how to connect it. They said below.
<?PHP
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
print "Connection to the Server opened";
?>
I've successfully created my user database with full set of data in phpmyadmin. And running Apache and Mysql in control panel. I set the password in phpmyadmin and I changed it in $password="mypassword";. But there is no print on my web page. I think the above code is correct and I am having problems before this state. Such as the location of my database, I don't where to put it or just created on myadmin is fine. Thank you for reading my problem and kindly advice me for the beginner course.
<?php
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
// Create connection
$con=mysqli_connect($server, $user_name, $password, $database );
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try mysqli instead of mysql and here is why
no no . . sorry . My html path . . on desktop
PHP, in this context, is a server side programming language. You must request the script from a server that will pass it through a PHP interpreter before sending it to the browser.
Type http://localhost/etc/etc into the browser's address bar. Don't open the PHP program directly in your browser (e.g. by double clicking the file in Windows Explorer).
If you are going to use procedural style function calls with the mysqli interface:
$con = mysqli_connect('localhost','myuser','mypassword','mydb')
or die("Error " . mysqli_error($con));
If you are going to use object oriented style with mysqli interface:
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
Reference: http://php.net/manual/en/mysqli.construct.php
If you are going to use PDO interface
$dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
Reference: http://php.net/manual/en/pdo.connections.php