not connecting to the database - php

To connect mssql from my android application, I wrote the following script:
<?php
$myServer= "*****";
$myUser="*****";
$myPass="***";
$db="*****";
echo "hi";
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
echo $dbhandle;
if($dbhandle) {
echo "success";
} else {
echo "failed";
}
$database = mssql_select_db($db);
?>
When I test it in a browser, it's showing only "hi" and it's not executing remaining lines of code. What would be the problem here?

You have not enabled the mssql module in PHP. If you find the error logs, it would say something like "mssql_connect is not a function".
The mssql support built into PHP has been broken for some time. I would suggest following the instructions on this page to help you get it configured.

Related

Debian 11 PHP db2_connect() not working / PECL installed ibm_db2

I have taken the following steps and I am using Debian 11.
I've installed the CLI Driver for DB/2 from the IBM Website
I've installed the ibm_db2 package via pecl for PHP PECL
I've edited the PHP.INI Files for both apache2 and CLI.
My PHP script is executing and it acceptes the db2_connect() command
Nevertheless, it's not connecting to my DB/2 server. I've ensured that I have connectivity from the Debian Server to the DB/2 Server.
I am getting constantly the following message:
PHP Warning: db2_connect(): SQLGetDiagRec returned -2 (is the driver working?) in /var/www/html/db2.php on line 7
Connection failed.Done Processing
Any help is appreciated.
Is the $database string correct? Are there any other setup setps to be taken?
This is my PHP script so far:
<?php
$database = 'NAMZPR1:50055';
$user = 'bouser';
$password = 'xxx';
$conn = db2_connect($database, $user, $password);
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed.";
}
echo "Done Processing ...";
?>
It has turned out that either the db2dsdriver.cfg has to be edited or a valid connection string in PHP has to be used. Here is a sample PHP.
<?php
$connection_string="DATABASE= ;HOSTNAME= ;PORT= ;PROTOCOL=TCPIP;UID= ;PWD= ";
$conn = db2_connect($connection_string, "", "");
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed. " . db2_conn_errormsg() . " ???";
}
?>
Also, ensure that the environment variables have been set. Thanks and acknowledgement to "mao".

How to connect to Oracle Database with php?

I am currently working on a website that connects to an Oracle Database. I have two php files, one for connection with the database and the other is the html structure itself.
Connect.php:
<?php
$servername = "//////";
$username = "/////";
$password = "/////";
$conn = oci_connect($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected!";
?>
I have been having a very hard time connecting to the database. I followed the Oracle tutorial and edited the oci8.connection_class = MYPHPAPP in php.ini, but everytime I run the Connect.php, I get the HTTP Error 500. Did I miss anything? What should I do?
Edit 1: I used display_errors and the error I am getting is Call to undefined function oci_connect()
Edit 2: I tried everything at this point to make the oci_connect work. I downloaded the oracle client and made it an environmental variable but oci_connect is still not working. I would really appreciate if any mac users could help me with this.
Download the appropriate oracle client to your machine, extract it and copy and paste in your system drive.
Add the path of your oracle client to environment variables.
Then you need to enable php_oci8_12c extension using php.ini or GUI if available.
Open php file and write the following code:
function connect(){
$dburl = "(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = your_host)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = your_db_sid)
)
)";
//db charset is optional
$db_charset = 'WE8MSWIN1252'; //your db charset goes here
try {
return oci_connect("username", "password",$dburl,$db_charset);
}
catch (Exception $e) {
die($e);
}
}
This code is work fine on my windows 10 machine, php 7.1.9 and oracle 12c.
Forth link in google after searching for your exact questions brought up the following link: http://me2learn.wordpress.com/2008/10/18/connect-php-with-oracle-database/
<?php
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.34)(PORT = 1521)))(CONNECT_DATA=(SID=orcl)))" ;
if($c = OCILogon("system", "your database password", $db))
{
echo "Successfully connected to Oracle.\n";
OCILogoff($c);
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
?>

My php code returns jibberish

So I'm trying to connect my very basic android application to an apache server.
I went as far as downloading wampserver and have started coding the php files to connect and post (both separate files). While it is connecting to my database, it is failing to retrieve or to post data into the database using php.
<?php
require "init.php";
$name="Lucky";
$message="Getlow";
$sql_query="select * from post_its where name = '$name';";
$result= mysqli_query($con,$sql_query);
if(mysqli_num_rows($result)>0)
{
echo "Login Successful";
}
else
{
echo "Error";
}
?>
My php file for connecting to the database is given below:
<?php
$db_name="webappdb";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
echo Connection Error;
else
echo <h3>Connection Success</h3>
?>
In my browser when I try and run my post.php file, it shows me:
Connection Success ?>(followed by random chinese characters)
Please help!

PHP access a database in MS SQL server 2008 r2

Dears,
I have a php application and I cannot access the SQL server database even though I installed the drivers and modify the php.ini file.
when I write the following code it doesn't provide me with an output
$servername = "localhost";
$info = array('Databse'=>'Test');
$con = sqlsrv_connect($servername);
if($con)
{
echo "Success";
}
else
{
echo "Failure";
}
anything below the sqlsrv_connect() is ignored.
Please help me I've been stuck in this for the past 3 weeks :(
You could print out the connection error with using sqlsrv_errors()
$con=sqlsrv_connect($servername);
if($con)
{
echo "Success";
}
else
{
die( print_r( sqlsrv_errors(), true));
echo "Faluire";
}
For further info check manual http://php.net/manual/en/function.sqlsrv-connect.php

PHP ignoring mysql_connect requests

I'm new to PHP and have installed on Linux to boot (also a newbie).
Anyway, PHP is working...
<?
$myVar = "test";
echo($myVar);
?>
... works just fine.
But...
<?
$dbhost = "localhost";
$dbuser = "myuser";
$dbpass = "mypass";
$dbname = "mydb";
echo($dbhost . "-" . $dbuser . "-" . $dbpass . "-" . $dbname);
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to MySQL");
print $conn;
mysql_close($conn);
phpInfo();
?>
... does nothing. Nor errors, nothing. Its as if the code isn't even there.
Any help?
Try to do the following:
First make sure display_errors is turned on in your php configuration file. Also set the level of error_reporting to show all errors, including strict (error_reporting = E_ALL|E_STRICT). After you make changes, restart your webserver.
Run phpinfo(), and check that the mysql extension is installed and working. If it isn't make sure that you uncommented it in the php configuration file (again, remember to restart apache after each change to the configuration file).
At this point MySQL should be loaded and working, and you should be able to tell from the error (if it persists) what's the problem.
Try also dumping the contents of the connection result ($conn) to see what it contains.
In general, I'd recommend using long php tags (<?php and not <?) since it is more portable (short tags are off by default in PHP 5 installations).
Try adding this to the top of your code:
error_reporting(E_ALL);
If it does nothing, doesn't that mean that it connected fine? What output do you expect out of that statement?
You could try
error_reporting(E_ALL);
$conn = mysql_connect("localhost", "myusername", "mypassword");
if(!$conn) {
echo 'Unable to connect';
} else {
echo 'Connected to database';
}
var_dump($conn);
edit: Addressing the comment saying that you have a mysql query setup, if you are not seeing "success" it means something is wrong with your query. Add to the above
$sth = mysql_query("SELECT * FROM tablename");
if(!$sth) {
echo 'unable to query: ' . mysql_error();
} else {
echo 'success';
}
Is there more code than you're showing us? The block you have just sets up a connection. You won't see anything at all if it succeeds, you have to use $conn to do something.
To confirm, try changing your password to a deliberately wrong value, and then see if you get an error. If you do, the code works just fine.
Connecting to a database with
$conn = mysql_connect("localhost", "myusername", "mypassword") or die("Unable to connect");
will have no (visible( results if the connection was made succesfully. However, once you run this statement, you can use the other mysql functions to make make queries to the database.
Connecting to a database tells your program "hey, I want to talk to this database".
This code is supposed to create a db connection, nothing else. What do you expect to see?
Try this
<?php
$conn = mysql_connect("localhost", "myusername", "mypassword")
or die("Unable to connect");
print("code sample");
print $conn;
?>
It should print you something like "resource #1"...
And then you may use that connection to communicate with db server

Categories