I have hosted my website on Linux server and I want to connect MS SQL database. i have used PHP in programming. I have contacted to both server provider and they helped in their extent. But my issue is not solved Can you guide me what to do. My code is below.
While I run this it is showing " could not find driver1"
Please guide me. Thanks in advance
<?php
//echo phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>testing</h1>
</body>
</html>
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'server:port';
$myDB = "DatabaseName";
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
else
{
echo "success";
}
?>
<?php
try {
$conn = new PDO("sqlsrv:Server='server_name';Database=database_name", 'username', 'password');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
die(print_r($e->getMessage() ));
}
$tsql = "select * from table_name";
$getResults = $conn->prepare($tsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);
foreach ($results as $row) {
echo $row['0'].' '.$row['6'];
echo "<br>";
}
?>
Related
I would like to print the measured data of my weatherstation on a website. Therefore I would like to connect my MariaDB-database. Here ist my code.
<?php
$username = "root";
$password = "M1lVhPuio";
$database = "weatherbot";
try {
$pdo = new PDO("mysql:host=raspberrypi;port=3306;database=$database", $username, $password);
//Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
die("ERROR: Could not connect. ". $e->getMessage());
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Wetterstation </title>
</head>
<body>
<h1> Meine Wetterdaten </h1>
</body>
</html>
When I open the website, I see this: "ERROR: Could not connect. SQLSTATE[HY000] [2002] Connection refused"
There's already an entry in this forum about this case, but adding the port didn't work for me. (3306 is the right port.)
Like Sergio Rinaudo said, I had to use the default address instead of "raspberrypi":
$servername = "127.0.0.1";
try {
$pdo = new PDO("mysql:host=$servername;port=3306;database=database", root, M1lVhPuio);
Thank you for your help!
I have the following code:
<?php
//Step1
$db = mysqli_connect('localhost','root','[mypassword]','users')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
</body>
</html>
I am just trying to connect to my MySQL database. It is administered using phpMyAdmin. I am very unfamiliar with MySQL and I have never used it before. [mypassword] is the password I use to successfully connect to mySQL from the Mac terminal. "users" is a name of a table I have created in phpMyAdmin. I am using cPanel. I keep on getting the error:
Error connecting to mySQL server.
In phpMyAdmin it says Server: localhost:3306. I have tried for a very long time to fix this problem but with no results. What am I doing wrong?
I have also tried the following:
<?php
$servername = "localhost";
$username = "root";
$password = "[myPassword]";
try {
$conn = new PDO("mysql:host=$servername;dbname=users", $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();
}
?>
After visiting the webpage it says
Connection failed: SQLSTATE[28000] [1045] Access denied for user
'root'#'localhost' (using password: YES)
Be sure that your mySql instance is up. You can download sequelpro and use that to connect to your mySQL. If that doesn't work then its a mysql setting/config that is wrong.
You can try this code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
How to Connect to mysql using php
Use a try catch to handle errors on the connection proccess, but i strongly advise you to use PDO by alternative from mysqli PDO Book
Use something like this:
<?php
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
And check if the code print some connection error.
Reference: how to use throw exception in mysql database connect
Here is my index.html file. I load the page and nothing happens. Shouldn't it print "Please try again" on the webpage if my info is incorrect?
<html>
<body>
<h1>mySQL</h1>
<?php
$server = "mysql.blah.com";
$username = "my_username";
$password = "my_password";
$database = "my_database";
$mysqlConnection = mysql_connect($server, $username, $password);
if (!$mysqlConnection){
echo "Please try later.";
}
else {
echo "All good";
mysql_select_db($database, $mysqlConnection);
}
?>
</body>
</html>
This is because your file has the .html extension.
Change it to .php and run it again.
Be sure to run it on a web server, that has PHP installed
change the file to the .php extension and use this refactored version
<html>
<body>
<h1>mySQL</h1>
<?php
try
{
$server = "mysql.blah.com";
$username = "my_username";
$password = "my_password";
$database = "my_database";
$mysqlConnection = new PDO('mysql:host={$server};dbname={$database};', '{$username}', '{$password}');
$mysqlConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo ('Please try later.');
echo $e->getMessage();
}
?>
</body>
</html>
I have an Oracle database set up on Amazon RDS and am trying to connect to it in PHP.
I am fairly sure my connection string is correct because I connected using sqlplus and am able to retrieve data. The server is an Amazon Linux server, however I have also tried to connect using a different server and get the error:
Is there any more set up that I need to do in either the server or the database server, I have just started using AWS and am not sure if anything else needs to be done.
ORA-12154: TNS:could not resolve the connect identifier specified... Line 19.
Here is my test code:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
echo "OCI Test<br>";
$tns = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz.abc.us-west-2.rds.amazonaws.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));";
echo "<pre>$tns</pre>\n";
$username = "xxx";
$password = "yyy";
$db = #oci_connect($username, $password, $tns);
if (!$conn)
{
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$sql = "select * from city";
$stmt = OCIParse($db, $sql);
if(OCIExecute($stmt))
{
while(OCIFetchInto($stmt, $row, OCI_RETURN_NULLS))
{
echo $row[0]. "-" . $row[1]."<br>";
}
}
OCIFreeStatement($stmt);
?>
</body>
</html>
Edit: Also the Amazon Linux server just shows nothing, while another server that I have access to gives errors, is there a configuration change for this?
The $tns variable looks wrong.
Try
$tns = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xyz.abc.us-west-2.rds.amazonaws.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));";
ok so I have the following code that I am running in the python shell:
import MySQLdb
db = MySQLdb.connect(host = "xxxx",user="xxxx"password="xxxx",db="xxxx")
cur = db.cursor()
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
Fairly sure the connection part is working, I'll get an error if I enter in the wrong value, or if I deny access to the database for my computer's IP address.
on the webhosting server, I have the following basic index.php file, which I have tested on a server on my computer, and I know works. when I go to the website domain, I get the following error: "Database query failed."
Any ideas why the MySQL query isn't working? My webhosting is Cpanel with godaddy.com, should I look for something else?
<?php
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "xxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); /*1*/
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
$query ="SELECT * FROM qqqq"; /*2*/
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>
<!DOCTYPE html PUBLIC >
<html lang="en">
<head>
<title></title>
</head>
<body>
<ul>
<?php /*3*/
while($subject = mysqli_fetch_assoc($result)){
?><li><?php echo $subject['asdf'];?></li>
<?php
}
?>
</ul>
<?php
mysqli_free_result($result); /*4*/
?>
</body>
</html>
<?php
mysqli_close($connection); /*5*/
?>
You should call db.commit() to have it complete. By default, autocommit is turned off.
You also have an error in your code. The SQL should be a string.
Shouldn't the cursor execute be calling a string? You don't have quotes around your sql statement.
This line without quotas is incorrect:
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
It should be
cur.execute("CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))")
So test in your database whether you really have table qqqq.
You could install SQL Buddy or phpMyAdmin.