Connecting to AWS RDS database using beanstalk and PHP - php

In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.

From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>

Use mysql_connect($servername, $username, $password) instead.

Related

Have php7 working however when I try to make a mysqli object, "Class 'mysqli' is not found" [duplicate]

This question already has answers here:
PHP 7 cannot find MySQLi
(2 answers)
Closed 3 years ago.
I have php7 installed and working
I'm using the Built-in HTTP server
Uncommented "extension_dir="C:\php\ext" and extension="php_mysqli.dll" in the php.ini files development and production (did production too just to be safe)
When I used the phpinfo() method, I found that it says "Loaded Configuration file: none"
I've installed php7 therefore mysqli is supposed to already be installed. Looking online I found that you have to edit the php.ini file (mentioned how I did so in bullet points above) however when I run:
$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";
?>
I get the error:
Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Users\Calvin\try\learningMySQL.php:17 Stack trace: #0 {main} thrown in C:\Users\Calvin\try\learningMySQL.php on line 17
Thank you, any help is appreciated.
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect("$servername","$username","$password","$database");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Selecting data from SQL Server Using PHP

I am trying to select data from a local database on my PC using PHP but i am getting this error when i run 127.0.0.1/test.php which is what the file is called.
error:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
C:\xampp\htdocs\test.php:11 Stack trace: #0 {main} thrown in
C:\xampp\htdocs\test.php on line 11
Here is my PHP script:
<?php
$servername = "Windows local servername";
$username = "Windows username";
$password = "windows password";
$dbname = "dbname";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
die('error connecting to database');
}else{
mysql_select_db(dbname, $conn);
}
$sql = "SELECT UserId, UserEmail, UserPassword FROM User";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["UserId"]. " - UserEmail: " . $row["UserEmail"]. " " . $row["UserPassword"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have looked around online but i cant figure out what is wrong here. I can run a hello world php script and it works so i am assuming its a connection issue with the database but what have i missed here? Thanks
EXTRA:
i have:
Try to run
phpinfo()
And look for the mysql extensions, in ubuntu you can install them by typing
sudo apt-get install php-pdo-mysql
Anyway the instruction you're trying to use was deprecated in HP 5.5.0 and deleted in PHP 7.0.0 so may be this is why its missing... depending on you PHP version, you could try with the mysqli extension instead like:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT 'Please, do not use ' AS _msg FROM DUAL");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
You could get more info on the actual way of performing this with PHP7 at http://php.net/manual/en/mysqli.quickstart.dual-interface.php for instance
From the docs
This extension was deprecated in PHP 5.5.0, and it was removed in PHP
7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
You'll probably want to use Microsoft's drivers for PHP for SQL Server rather than the ODBC driver in PDO.
In place of
$conn = mysql_connect($servername, $username, $password, $dbname);
use
$connectioninfo=array("Database"=>"dbname", "UID"=>"userid", "PWD"=>"password", );
$con = sqlsrv_connect($server,$connectioninfo);

install sql server drivers for php

hello everybody I'm new to coding world and I need your help I would like to connect to a server with xampp (3.2.2) and php version (7.2.4) windows 10... I have installed the drivers php_pdo_sqlsrv_72_ts_x86.dll and php_sqlsrv_72_ts_x86.dll to php.ini but also to php\ext
and when I am trying to connect to a server with this code doesn't respond
thank you very much for your time
$servename = "new";
$username = "user";
$password = "tree";
$database = "tree";
$connectionInfo =array("Database" =>"tree","USER" => "user", "PWD" => "tree");
$conn=sqlsrv_connect($servename,$connectionInfo);
if($conn)
{echo"connection established";}
else
{echo"connection failure";die (print_r(sqlsrv_errors(),true));}
Error message:
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()
Here you can use it with help of PDO,
Please check below code and try to replace with your original data
<?php
$dsn = "sqlsrv:Server=**servernamehere**;Database=**Db name here**";
$conn = new PDO($dsn, "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM table_name";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
?>

Connect PHP with MS SQL SERVER

I am new to PHP and I am trying to connect my PHP with MS SQL SERVER.
I have googled it but not found any good solution.
I am using PHP version : 7.0.6
I have downloaded the required extension and place it in xampp/php/ext folder and added these lines in php.ini file
extension=php_pdo_sqlsrv_7_nts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_nts_x64.dll
and I m using this code to connect to my server.
$myServer = "SERVER_IP";
$myUser = "USER_NAME";
$myPass = "PASSWORD";
$myDB = "DB_NAME";
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
bu tit shows me this error:
Fatal error: Uncaught Error: Call to undefined function mssql_connect() in C:\xampp\htdocs\schedule\server.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\schedule\server.php on line 2
Any help in this would be highly appreciated !!
You have sqlsrv_connect not mssql_connect, try using this. If it doesn't work, that means you have problems with your extension ( you can also use function_exists to check ).
More info: sqlsrv_connect: http://php.net/manual/ro/function.sqlsrv-connect.php
$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database_name", "UID"=>"mssql_username", "PWD"=>"mssql_password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Error connecting";
die( print_r( sqlsrv_errors(), true));
}
I believe you have to add that mssql extension in order to use that... but you can Use PDO if you want to.. follow the link here
http://php.net/manual/en/ref.pdo-dblib.php
Issue Solved:
I have used extension=php_odbc.dll
with this code:
$server = '****';
$user = '****';
$pass = '****';
//Define Port
$port='Port=1433';
$database = 'cargo_web';
$connection_string = "DRIVER={SQL Server};SERVER=$server;$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
} else{
die("Connection could not be established.");
}

MSSQL using PDO to connect

I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$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();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.

Categories