Upgrading from php5 to php7 having trouble running scripts [duplicate] - php

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 5 years ago.
I wonder if someone can help. I gave up web programming about 3 years ago. Since returning to programming about a week ago I realize I have forgotten quite a lot and so much has changed.
I signed on to a deal with php7 but my code is php5 and when I came to running my scripts nothing really works.
For example I couldn't even connect to the database. My database connection file for php5 was
<?php
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "db_username_here";
// Place the password for the MySQL database here
$db_pass = "db_password_here";
// Place the name for the MySQL database here
$db_name = "name_of_database_here";
// Run the actual connection here
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I spoke to the server company and they told me everything has changed and to get my code compatible with php7. They then helped me get my database connected by giving me a sample file of how it should be which is this code.
<?
/* DATEBASE CONFIGURATION */
$dbHost = "ip Address";
$dbName = "name_of_database_here"; // Database name
$dbUser = "db_username_here"; // Database user
$dbPasswd = "db_password_here"; // Database password
function dbConnect(){
global $dbHost, $dbUser, $dbPasswd, $dbName;
mysql_connect( $dbHost, $dbUser, $dbPasswd ) or error( mysql_error() );
mysql_select_db( $dbName );
}
?>
I have always run my database $dbHost from localhost so I have guessed that because they haven't done the connection as localhost and done it as gobal with an ip address I have figured that the database is not on the same server as the website.
I then came to my database scripts for running from the browser to phpmyadmin and they also didnt work. Here is my database script php5.
<?php
require "connect_to_mysql.php";
$sqlCommand = "CREATE TABLE admin (
id int(11) NOT NULL auto_increment,
username varchar(24) NOT NULL,
password varchar(24) NOT NULL,
last_log_date date NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
) ";
if (mysql_query($sqlCommand)){
echo "Your admin table has been created successfully!";
} else {
echo "CRITICAL ERROR: admin table has not been created.";
}
?>
I was wondering if someone could give me some advice on why the script isn't running
Many thanks, Gary

mysql_connect is removed from php7.
instead of that you can use either mysqli or pdo
example with mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
example with pdo
<?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();
}
For more reffer details https://www.w3schools.com/php/php_mysql_connect.asp

Related

How to fix error in dbconn file when upload php mysql project to free webhosting site?

I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1

Error 500 when I try to connect MySql with php's mysqli

I have written below php code to connect MySQL DB:
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
However this code fails to make any connection. All I am getting is below error:
currently unable to handle this request.
HTTP ERROR 500
However if I remove the connection portion of my code then my php file working perfectly, i.e. I am getting echo as 'Connected successfully'
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
echo "Connected successfully";
?>
Can somebody help me to understand what went wrong? I have checked username and password, and they are perfect
I just installed mysqlnd using apt-get install php-mysqlnd, and it works fine.

How to connect to phpmyadmin database?

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";
?>

Connecting to mysql using php through xampp.... Database connection problems [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too
How ever when I attempt to actually connect through php....
<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}
..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called
I'm stumped
Lori
Try this
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
$host_name = "localhost";
$u_name = "root";
$u_pass = "";
try {
$dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
echo "Connection failed: " . $ex->getMessage();
}
?>
I would suggest that you start learning to use PDO man.

Deletion of row in MySQL using php

I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){

Categories