IBM Bluemix PostgreSQL and psql [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've created a PHP runtime enviroment in Bluemix and attached a postgresql server to it.
When trying to use psql from terminal, it doesn't seem to can reach the database?
Also, adding ./bp-config/options.json and:
{
"PHP_EXTENSIONS": ["mysqli"]
}
I'm not able to conncet via mysqli from my php scripts.

The extension mysqli for PHP is useful to connect to a MySQL database server.
As you described you bound to your application a PostgreSQL database, so this extension is completely useless.
To connect to a PostgreSQL you could use composer dependency manager (supported on Bluemix) with you PHP application and set your composer.json with the dependency. You can obviously add other dependencies to your composer.json.
"require": {
"ext-pgsql": "*"
}
Then you could connect to your PostgreSQL service using a code like the following one in order to get service credentials from VCAP_SERVICES variables available from CloudFoundry
if (getenv("VCAP_SERVICES")===false) {
$db = 'XXXX';
define("APP_DB_SCHEMA", $db);
define("APP_DB_HOST", 'xxxx');
define("APP_DB_PORT", "xxxx");
define("APP_DB_USERNAME", 'xxxx');
define("APP_DB_PASSWORD", "xxxx");
} else {
// getting VCAP configuration
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$pgsql_config = $services_json["postgresql-9.1"][0]["credentials"];
define("APP_DB_SCHEMA", $pgsql_config["name"]);
define("APP_DB_HOST", $pgsql_config["host"]);
define("APP_DB_PORT", $pgsql_config["port"]);
define("APP_DB_USERNAME", $pgsql_config["user"]);
define("APP_DB_PASSWORD", $pgsql_config["password"]);
}
and then establish the DB connection through:
$dbConnectionString = "host=" . APP_DB_HOST . " port=" . APP_DB_PORT . " dbname=" . APP_DB_SCHEMA . " user=" . APP_DB_USERNAME . " password=" . APP_DB_PASSWORD;
$dbConnection = pg_connect($dbConnectionString);

Related

php can't Select or Insert table data inside mariadb database ( Uncaught mysqli_sql_exception: Table 'X' doesn't exist in engine ) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm working on a larger Project that installs a php interpreter via xampp and mariaDB together with a larger project, containing a database and some php files on a blank windows 10 system.
i got everything running so far, the php server is running on command and the mariadb server seems to be reachable too.
The problem I'm facing lies in the fact that our project (which works perfectly under phpstorm and via command line on ANOTHER SYSTEM) doesn't seem retrieve the table data of our database in this VM.
The connection from inside the php is working and the database itself gets recognized too. I checked that by printing it like this:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "toor";
$dbname = "emensawerbeseite";
$dbport = "42069";
$link= mysqli_connect ($dbhost, $dbuser, $dbpass,$dbname,$dbport);
if(! $link ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully</br>';
$link->set_charset("utf8"); //enables the good old äüö's
$db_list = mysqli_query($link, "SHOW DATABASES"); //mysqli
while ($row = mysqli_fetch_object($db_list)) {
echo $row->Database . "</br>";
}
this prints me this output:
I can also list the all table column names.
The database itself I just Copied in the data folder of mariadb.
It persists of the same .frm and .ibd and .opt file(s), that my working version has.
Problem lies when i try to insert or retrieve any sort of actual data from the database e.g. like this.
$sql2 = "insert into gericht (id, name, beschreibung, erfasst_am, vegetarisch, vegan, preis_intern, preis_extern) values (22, 'CCurrywurst mit Pommes', '', '2022-11-14', false, false, 4.20, 6.90)";
if ($link->query($sql2) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql2 . "<br>" . $link->error;
}
Which is when the the following Error appears:
"Fatal error: Uncaught mysqli_sql_exception: Table 'emensawerbeseite.gericht' doesn't exist in engine in C:\xampp\php\index.php:43 Stack trace: #0 C:\xampp\php\index.php(43): mysqli->query('insert into ger...') #1 {main} thrown in C:\xampp\php\index.php on line 43"
What does this actually mean?
And what could i do about it?
Some Posts suggested to just restart or other things that didn't work.
"Table 'emensawerbeseite.gericht' doesn't exist in engine"
This means that your database is corrupted. In case of InnoDB the dictionary (ibdata1) doesn't contain information about table gericht or is missing at all.

Connecting to a database in SQL and PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm making a website that uses SQL and PHP functionalities. How do I connect to a database?
I would advise you begin by looking here.
You need to ensure that you have created user credentials with the correct permissions to query the database before you try this. You can do this through the cPanel of your web server (I'm going to assume you are using a web hosted server for this question).
Once you have a working and tested connection to the database, you can then start looking at the mySQLi documentation here. Which will show you how to execute and retrieve results from a database query and how to handle the returned data with PHP.
I see you are seriously downvoted.
I learned it the hard way and I am still learning to post here.
Stack sites are supposed to be searched first. If your question is already answered then people downvote you.
The solution to your question:
In your mysql or phpmyadmin you can set whether you use a password or not. The best way to learn is to set mysql with a password in my opinion. If you will launch a website online finally, you have to take security measures anyway.
If you make contact to your mysql database with you have to set:
username, password, database and servername ( for instance localhost).
The most secure way is using the OOP / prepared method:
$servername ='localhost';
$username='yourname';
$password='12345';
$dbname='name_database';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($stmt = $conn->prepare("SELECT idnum, col2, col FROM `your_table` WHERE idnum ='5' ")) {
$stmt->execute();
$res = $stmt->get_result();
$qrow = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
var_dump($qrows); // number of rows you have
$total = implode(" / " , $row);
var_dump($total);
$idnum = $row['idnum'];
var_dump($idnum);
}
The easiest way that I do with my site is make a file called db.php containing:
<?php
$host = 'localhost';
$user = 'root';
$pass = 'password';
$db = 'databasename';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
..then in the index.php file, at the top:
<?php
require_once('db.php')
?>

Running SSH command with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have this code
<?php
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
//HERE I WANT THE PHP TO SEND THE COMMAND TO THE SERVER
} else {
echo"No command were given!";
}
?>
Simply I want it to work like this: Let's say I go to MYSERVERIP/sendcommand.php?command=reboot then I want my server to reboot, how can I do this? My server is running Debian.
I've tried to understand it by reading http://php.net/manual/en/function.ssh2-exec.php, but that doesn't get me anywhere.
if you want to run command on the same server php runs - just use exec or shell_exec
ther is an example right there on your link...
mix with your stuff would look like
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, $_GET["command"]);
} else {
echo"No command were given!";
}
Assuming the server would accepts commands like this
If your file is same server with your php file, you can use
exec() or shell_exec( string $cmd )
If Not same server, it's not possible, that all i know
If you want to issue a command on a different server than the server that runs PHP, you need to start with ssh2_connect.
If you want to issue commands locally on the server, start out by reading about program execution.
I suggest you pay close attention to the security aspect of this.
You should also know that these two options are probably disabled if you are on a shared hosting environment.

Error with mysql_select_db [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use Linux Debian
Local Lamp Server
i tried to make connection between Database and php :
<?php
$tf_host = 'localhost' ;
$tf_dbname = 'tinyforum' ;
$tf_username = 'root';
$tf_password = '' ;
$tf_handle = mysql_connect($tf_host , $tf_username , $tf_password);
if (!$tf_handle)
die('connection problem ..');
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
{
mysql_close($tf_handle);
die('selection problem');
}
die('OK');
mysql_close($tf_handle);
?>
The result :
http://www.foxpic.com/V0HrSdgb.png
pic of phpmyadmin:
http://www.foxpic.com/V0AlRhi6.png
the place where i save the php file :
/var/www/html/
Unless you copied your code incorrectly, your check is not going to result in what you want.
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
your selection seems fine so either you should change your code to
if(!$tf_db_result) //note the !
or restructure your code
if($tf_db_result) {
//Do the stuff you want to do when you are connected
} else {
//Die connection
}

SSH within PHP script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
preface
I am setting up a web environment intended to be hosted on a local server strictly accessible on the local network. There's about 50 employees within the office and no employee has an assigned computer.
problem
Via an HTML form with parameters ('host', 'username', 'password'), the PHP will attempt an ssh.
Main Point
Basically I'm trying to find out how to interact with ssh from PHP (i.e. edit and view files/directories.
ie
user enters username, password into an html form. My php code takes this information and (if valid) ssh into their user on server. Giving them access to directories and files.
You can use phpseclib to handle the SSH. Here is some simple code to get you started:
<div>
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
function display_home_directory($hostname, $username, $password) {
$ssh = new Net_SSH2($hostname);
if (!$ssh->login($username, $password)) {
echo 'Login Failed';
}
echo '<pre>';
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
echo '</pre>';
}
display_home_directory('example.com', 'myusername', 'mypassword');
?>
</div>

Categories