I got hosted ubuntu server where I got apache2 and phpmyadmin. I wrote small script to connect to my mysql database. I placed that script in /var/www/html/ . When I enter to 193.219.91.103:5858/init.php I just see blank page. I use php 7. I found that I could check if mysqli enabled with php -m | grep mysql it did showed me mysqli.
<?php
$db_name = "AndroidDatabase";
$mysql_user = "dbreader";
$mysql_pass = "dbreader";
$server_name = "localhost";
$port = 3306
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name,$port);
if(!$con)
{
echo "Connection error".mysqli_connect_error();
}
else
{
echo "<h3>Database connection success</h3>";
}
?>
EDIT:
Fixed all i missed was extra argument $port
Related
I want to install the db2 extension for PHP on Windows but it just isn't working. I've tried a lot of different solutions but I still get these:
php.exe
phpinfo()
When I try connection to db2 database
I'm using Apache on XAMPP on port 80. I have installed this: https://github.com/ibmdb/php_ibm_db2/tree/master/PHP%207.4.x/x64/TS
and placed it in my C:\xampp\php\ext folder
and set "extension=php_ibm_db2.dll" in my php.ini file. (which is in C:\xampp\php)
Only "db2" appearing in phpinfo()
Variable where db2 is appearing
PATH variable
So, I did not manage to connect to my db2 DB with the db2 extension, but here's an alternative I've found -->
$database = "xxx";
$hostname = "xxx";
$user = "xxx";
$password = "xxx";
$port = 50000;
# Build the connection string
$driver = "DRIVER={IBM DB2 ODBC DRIVER};";
$dsn = "DATABASE=$database; " .
"HOSTNAME=$hostname;" .
"PORT=$port; " .
"PROTOCOL=TCPIP; " .
"UID=$user;" .
"PWD=$password;";
$conn_string = $driver . $dsn;
# Connect
$conn = odbc_connect( $conn_string, "", "" );
if( $conn )
{
echo "Connection succeeded.";
odbc_close( $conn );
}
else
{
echo "Connection failed.";
}
output -->
Connection succeeded.
source -->
https://www.ibm.com/docs/en/db2woc?topic=programmatically-php
I'm trying to take and get data from a database to a website using php. When I'm trying to connect I'm using this code:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?>
but upon trying to open the php file I recieve this on firefox:
connect_errno) echo "failed to connect to database"; ?> (on firefox)
and this on opera/chrome:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?>
I have also tried loading it with internet explorer but it simply tries to save it again. I'm not understanding what I am doing wrong.
For extra detail I am trying to do this through xampp using phpmyadmin.
Okay, I fixed it just incase anyone looks back at this essentially the issue was my misunderstanding of the instruction 'open the php'. I was opening it directly to the browser, I needed to open it via localhost.
so localhost/connect.php rather than just opening connect.php to the browser.
1) Make sure the extension of file is ".php"
2) Make sure that you are running page from "http://localhost//" or your local host ip.
You can test your apache/xampp by visiting "localhost" or "localhost:8080" if it is running on 8080 port.
I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');
I'm having trouble connecting to a MySQL database on my host OS (Win 7) from my Oracle VirtualBox running Ubuntu. I wrote this php code in order to connect but I keep receiving an error connecting:
<?php
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'sdb_user';
$db_password = 'password';
try {
$db = new PDO($dsn, $db_username, $db_password);
}catch (PDOException $e) {
$error_message = $e->getMessage();
include('../errors/database_error.php');
exit();
}
$query = 'SELECT * FROM books';
$books = $db->query($query);
foreach ($books as $book) {
echo $book['title'];
}
?>
I know the code works, because I can get it to run from my Windows 7 host OS using localhost instead of the IP address, and I know that both Linux on VirtualBox and Windows 7 are on the same subnet because I can successfully ping the other from each side.
I'm fairly certain that I opened port 3306 correctly on the windows side also, but I have no idea how to check if that's the problem or not.
What else should I be doing to get the code to connect to the MySQL db?
EDIT: The problem was that the port was not open for remote connections. After I opened the port I got the error message stating I was not allowed to connect to the sql server. From there I created a new user on the Windows MySQL with the username "home" and password "home" and was able to connect to it using:
$ mysql -h 192.168.1.9 -u home -p
However, when I run my file, which now reads:
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'home';
$db_password = 'home';
I still get the 'database error'
Any suggestions?
I created two simple PHP scripts to back up a MySQL database, but these are not working as expected. I use my program on a Mac using MAMP and using AMPPS on Windows 7.
Here the two scripts I use to backup the database:
[Message edited]
The First and only code
<?php
$host="localhost";
$user="root";
$password="root";
$db="trasporti";
$dbcnx_backup=#mysql_connect("$host", "$user", "$password");
mysql_select_db("$db");
$backupFile = '../../t6/backup/' . $db . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h $host -u $user -p $password $db > $backupFile";
system($command, $retval);
echo $command;
var_dump($retval);
if ($retval==0)
{
echo "BackUP Riuscito!";
}
else
{
echo "BackUP Fallito!";
}
mysql_close($dbcnx_backup);
?>
Can someone explain what I am doing wrong here?
Remove the space after -p.
You can remove all the calls to the mysql_ functions in your code.
Is safe_mode enabled in your PHP configuration? Use phpinfo() to tell, if yes, you might need to configure PHP properly or turn it off.