Heres my code. Simple.
<?php
echo 'start<br>';
//Do the conntection
$checkconnection = mysql_connect('localhost', 'root', 'rootpass');
//Check if it's valid
if(!$checkconnection) {
echo 'CheckFailed';
} else{
echo 'CheckSucess';
}
echo 'end'; ?>
but I only can see 'start'. There is no 'CheckFailed', 'CheckSucess', 'end'
What should I do?
I already install mysql, create database, create tables, of course.
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return false;
}
$result = mysqli_query($con, "SELECT * FROM table;");
?>
Related
<?
$server_ip = "my ubuntu ip";
$server_port = "my ubuntu port";
$server_user = 'my id';
$user_pw = 'my pw';
$connection = ssh2_connect($server_ip, $server_port);
if (ssh2_auth_password($connection, $server_user, $user_pw)) {
echo "Authentication Successful!<br>";
} else {
echo "Authentication failed...";
}
$mysql_ip = '10.41.12.71';
$mysql_default_port = '3306';
$tunnel = ssh2_tunnel($connection,$mysql_ip,$mysql_default_port);
if($tunnel) {
echo "tunnel created<br>";
}else {
echo "tunnel creation failed";
die();
}
$mysql_user = 'root';
$mysql_user_pw = 'tkakcy159*';
$dbname = 'test';
// $dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);
$dbconn = new mysqli($tunnel, $mysql_user, $mysql_user_pw, $dbname);
if ($dbconn) {
echo $dbname." connected<br>";
} else {
echo "DBConnection failed: " . $dbconn->connect_error;
}
// $insert_sql = " insert into test_table values('0001','wlsdhks0423'); ";
// $result = mysqli_query($dbconn,$insert_sql);
// if($result) {
// echo "data insert success<br>";
// }
// else {
// echo "data insert failed : ".mysqli_error($dbconn);
// }
$select_sql = " select * from test_table; ";
$result = mysqli_query($dbconn,$select_sql);
if($result) {
echo "selected rows : ".mysqli_num_rows($result)."<br>";
$row = mysqli_fetch_array($result);
$key = $row['test_key'];
$value = $row['test_value'];
echo "key = ".$key."<br>value = ".$value;
}else {
echo mysqli_error($dbconn);
}
// phpinfo();
?>
This is my code. mysql is on ubuntu device.
And I think I can access ubuntu, but mysql connection is failed
this php's output is
Authentication Successful!
tunnel created
test connected
and if i use this line
$dbconn = mysqli_connect($tunnel,$mysql_user,$mysql_user_pw,$dbname);
then output is
Authentication Successful!
tunnel created
DBConnection failed:
The obvious thing is that $connection and $tunnel has no problem.
I had tested them. And works well.
This is an exact duplicate of the postgresql question because they both exist for the same reason. You're both trying to connect a service over a SSH Tunnel by using ssh2_tunnel incorrectly.
ssh2_tunnel returns a resource aka a file pointer
The ssh2_tunnel() return a socket object which means you can operate this socket object just like you use standard socket function.
I have such code:
fwrite($ssh_tunnel, $db_password);
while (!feof($ssh_tunnel)) {
echo fgets($ssh_tunnel, 1280);
}
and I get such result:
a 5.5.5-10.3.34-MariaDB-cll-lve��=^Aile5x����}.hBNEM5_^:Gmysql_native_password!��#08S01Got packets out of order
I think fwrite can send something to such tunnel we have created, so I am currently figuring out what data should be sent to.
I am trying to produce an outcome if both rows in two different tables match, but I am having a hard time trying to make it work. Can someone please tell me if I am missing something.. Thank you in advance
**Note: my connections are in an included file and both tables are in the same database
<html>
<p>Pending Documents</p>
<?php
$sql ="SELECT * FROM `forms`";
if ($_SESSION['user_name'] == $row["username"]){ ?>
<p>SUTA Document</p>
<? }else { ?>
<p>No Pending Documents</p>
<? } ?>
</html>
You should solve it with mysqli. Here is a working snippet:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . mysqli->connect_error;
}
if ($result = $mysqli->query("SELECT * FROM `forms`")) {
while ($obj = $result->fetch_object()) {
if ($_SESSION['user_name'] == $obj->username){ ?>
<p>SUTA Document</p>
<? }else { ?>
<p>No Pending Documents</p>
<? }
}
$result->close();
}
$mysqli->close();
?>
I'm trying to create a basic PHP/MySQL test script. When I attempt to print the two tables in the database I created ('andrew'), the PHP output returns none. However, when I run the same command ('show tables;') through the MySQL 5.6 Command Line, both tables are returned. Any idea what I'm doing wrong?
<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);
if(mysqli_connect_errno())
{echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else{echo 'Connected to MySQL! </ br>';}
$result=mysqli_query($connect,'show tables;');
if(!$result){
echo 'Nope';}
else{echo '<p>Result!!!</p>';}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_assoc($result)){
print_r ($row);}
echo "</ul>";
?>
Try run this sql in your mysql command line:
use mysql;
grant all on andrew.* to 'root'#'localhost' identified by 'root password';
Then again try to run your script.
The script you have is working properly.
DETAILS:
$host='localhost';
$username='root';
$password='';
$database='test';
OUTPUT:
Connected to MySQL!
Result!!!
Tables in database:
Array ( [Tables_in_test] => tester )
<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);
mysqli_select_db($connect,$database);
if(mysqli_connect_errno()) {
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else {
echo 'Connected to MySQL! </ br>';
}
$result=mysqli_query($connect,'your sql query');
if(!$result){
echo 'Nope';
} else{
echo '<p>Result!!!</p>';
}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_array($result)){
print_r($row);
}
echo "</ul>";
?>
I am new to mysql and php. I am working on database programming with php with mysql but getting "no data base selected" error continuously. I found this error quite famous on internet. I tried every answer which were given to others having the same problem but nothing worked.
Here is my code:
if(!#mysql_connect('localhost','root','') || !#mysql_select_db ('a_database') ){
die ('Connection Error !');
}
$query = "SELECT `food`,`calories` FROM `food` ORDER BY `id`";
if($query_run=mysql_query($query)){
while($query_row = mysql_fetch_assoc($query_run))
{
$food = $query_row['food'];
$calories = $query_row['calories'];
echo $food.' has '.$calories.' Calories'.'<br>';
}
} else {
echo mysql_error();
}
This is the code that gives error. After some search on net. I made some bit of changes, but the result was same.
Changes that I made to first 3 to 4 lines:
$link = mysql_connect('localhost','root','');
if(!$link || !mysql_select_db ('a_database', $link) ){
die ('Connection Error !');
}
Please tell me what should I do to get rid of this problem, Thank you.
Try with this
<?php
// Create connection
$con=mysqli_connect("localhost","root","","a_database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try this
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n"; // if everything is successful
And yes don't use mysql_* as it's deprecated, use mysqli_ or PDO.
This question already has answers here:
Error: file is encrypted or is not a database
(7 answers)
Closed 5 years ago.
I have an SQLite database and am trying to connect to it with PHP. This is what I'm using:
<?php
$dbconn = sqlite_open('combadd.sqlite');
if ($dbconn) {
$result = sqlite_query($dbconn, "SELECT * FROM combo_calcs WHERE options='easy'");
var_dump(sqlite_fetch_array($result, SQLITE_ASSOC));
} else {
print "Connection to database failed!\n";
}
?>
However, I get this error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in C:\xampp\htdocs\deepthi\combadd\combadd_db.php on line 4
Connection to database failed!
What's wrong and how can I fix it?
Try to use PDO instead of sqlite_open:
$dir = 'sqlite:/[YOUR-PATH]/combadd.sqlite';
$dbh = new PDO($dir) or die("cannot open the database");
$query = "SELECT * FROM combo_calcs WHERE options='easy'";
foreach ($dbh->query($query) as $row)
{
echo $row[0];
}
$dbh = null; //This is how you close a PDO connection
Connecting To Database
Following PHP code shows how to connect to an existing database. If database does not exist, then it will be created and finally a database object will be returned.
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('combadd.sqlite');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
?>
Now let's run above program to create our database test.db in the current directory. You can change your path as per your requirement. If database is successfully created then it will give following message:
Open database successfully
SELECT Operation
Following PHP program shows how we can fetch and display records
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('combadd.sqlite');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql =<<<EOF
SELECT * FROM combo_calcs WHERE options='easy';
EOF;
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
echo "ID = ". $row['ID'] . "\n";
}
echo "Operation done successfully\n";
$db->close();
?>
<?php
if ($db = sqlite_open('sampleDB', 0666, $sqliteerror) ) {
$result = sqlite_query($db, 'select bar from foo');
var_dump(sqlite_fetch_array($result) );
} else {
die($sqliteerror);
}
?>
Make sure sqlite support is enable, check phpinfo()
One another solution to your problem is:
Using sqlite3 module instead
class DB extends SQLite3
{
function __construct( $file )
{
$this->open( $file );
}
}
$db = new DB( 'sampleDB.sqlite' );