Can't access PostgreSQL with PHP - php

I've been trying to connect to my database, which has been created with PostgreSQL. However, it's unsuccessful.
I've watched lots of videos and researched quite a bit on here, but my issue seems too simple to be an issue for others.
The setup:
I'm hosting a local MAMP (free version) server, in order to use PHP.
I've downloaded and created a database using PostgreSQL locally, and am doing it from pgAdmin 4.
The code is written in a .php file, surrounded by PHP clamps, and the first print line is visible, so I believe there is a connection to the web page in my chrome browser.
I don't get any response from my if-else statement or the pg_query. All the page shows is: This is it:
Issue in short: What is the cause of me not being able to connect to the database and get data from it?
print "This is it:";
$db_connection = pg_connect("host=localhost dbname=first_creation user=postgres password=1234");
if($db_connection)
{
echo "connected";
}
else {
echo "not working";
}
$result = pg_query($db_connection, "SELECT text_content FROM strings");

you need to instll and configure the driver of pgsql in php.ini, please check the following question: How do I enable php to work with postgresql?

Related

Hard time to connect to my database with php

I am trying to build a homne server for studies on a raspberry pi 3.
I am just trying to connect to my raspberry database and php base to use my database on a HTML page.
I tried everything 9not apparently), to change several times my login, i tried with 'root' and create an other user and give all privileges etc, but nothing seem to work. I have all installed, php work just fine, i can reach my server with a domain name hosted by noip. the html and php content pop just fine.
I can even connect to my database via ssh.
But since few month i tried to make it work and i am desesperate, so if someone can help me ...
<?php
$db = mysqli_connect("mywebsite","root","myLogin","mydbName")
or die(mysqli_error($db));
?>
so i would just be able to make request on my database, i am just blocked on that step
$con=new mysqli("localhost","root","root_password","database_name");
Generally, root has no password so the connection should be like this
$con=new mysqli("localhost","root","","database_name");

Trouble connecting to mySql on IIS

So I need to do something for work which is to simply take a users name/last name/email and store it to a database.
As a simple test, I put this in my connect file
php:
$server="localhost";
$uname="root";
$pass="PASSHERE";
$db = "DBNAMEHERE";
$c2d = mysqli_connect($server, $uname, $pass, $db) or die("Failed to connect to database yo" . mysqli_error($c2d));
if($c2d){
echo "connection made";
}else{
echo "connection NOT made";
}
This above works when I run it from WAMP locally or from my test live server on Godaddy etc. Now, the issue is that the production environment is an IIS box to which I installed Apache on it via the IIS tool. It's my first time working with IIS so....Just trying to figure things out But need a bit of assistance
For some reason, when I drag and drop the working file from my local machine to the production one for testing, the connection can never be made. It's always erroring out.
I've done a bit of digging around but to no avail. I've tried different connection strings, ive tried messing with the PHP.ini file on the IIS box, ive tried other simple examples but again it always fails. After doing some research and reading random posts here on SO, I came across another post that essentially said to make sure that "mysqli" extension was enabled in php.ini and it is.
Lastly, since i dont have a lot of time to get this working, I figured instead of saving to a database, id write locally to a file(csv and/or text file), with something like:
$putContentsIntoCSV = fopen("001registrations.csv", "a");
$putContentsIntoTXT = file_put_contents("001regtext.txt", $insertDataToText, FILE_APPEND);
if($putContentsIntoCSV && $putContentsIntoTXT ){
http_response_code(200);
setcookie("success", true);
echo "Your data has been saved!";
fputcsv($putContentsIntoCSV, $insertData);
fclose($putContentsIntoCSV);
}else{
http_response_code(400);
echo "no connection";
}
and that too works in my local env. and my Godaddy server, but as soon as i drop it inside IIS/apache, it doesnt work. To be clear, PHP works, but the functionality im needing doesnt.
Im pretty much stuck not sure what else to try. Any help is greatly appreciated.

PHP Connection to SQL Server 20005

I have PHP installed on a web server and also have MySQL on the same server and all works perfectly.
I now have SQL Server 2005 installed on another server 192.168.90.250 that I want to connect to as there is a view i want to display in PHP.
I have read around for a number of days but still can not get a connection to work.
I am running on the web server:
Windows Server 2008 R2
Apache 2.2
P 5.3.0
And SQL Server 2005 on another server
I presume I need to download a driver like SQLSRV30 and place the correct .dll file into the C:\Program Files (x86)\PHP\ext directory.
I presume I then need to edit the php.ini file and add something like the following:
extension=php_sqlsrv_53_ts.dll
I would then need to restart the apache server.
At this point I presume I would be able to use something like the following to connect to SQL Server:
<?php
if (function_exists('mssql_connect')) {
die("Your PHP installation does not have MSSQL support.");
}
$db = mssql_connect ($hostname, $username, $password);
if($db){
echo "no connection";
}else{
echo "connected";
}
?>
It seems as if I can not run the function mssql_connect, as the page just displays a blank page.
If I comment out the:
//$db = mssql_connect ($hostname, $username, $password);
the page at least displays some data.
I have literally spent days on this, what am I doing wrong?
Am I using the correct driver as there are different versions?
Thanks for your help.
D
For using the sqlsrv extension you need to use sqlsrv_ functions or better yet switch to PDO

php returns different mysql result

I have just moved my host to another machine but now a problem has occured. I get different mysql results from php. To be more spefecific, php returns the last result when i got back-up. I am checking database via mysql console but there are new entries. But php continues to return the old results. What do i need to do fix this?
P.S i can download php files which are on my new host. I can see the source code. Weird...
Thank you.
Your php might still be connecting to the old database
If you can download the php files, your server isn't set up to run PHP and/or has a mis-configured .htaccess file
Check your database connection code.
It's possible that you're connecting to a remote host, rather than 'localhost':
$mysql_connection = mysql_connect('mysql.example.com', 'user', 'pass');
There is another possibility, different databases:
mysql_select_db('using_old_db', $mysql_connection);

Connecting to MySQL with PHP

I have MySQL running such that I can open a client command line and log on and make databases, tables, etc.
I wanted to do some AJAX. Doing AJAX with ASP, SQL Server, etc is not advisable since both places where I am hosting my websites do not use the Microsoft products. So I am forced to do my development with PHP and MySQL.
I just about have everything set up. I have set up IIS so that I can go to my localhost and I can test out web pages. I have PHP installed so that I can pull up the PHP setting pages.
The problem occurs when I try to bring up the MySQL database in PHP. I use this very simple PHP command:
<?php
$con = mysql_connect('localhost', 'testuser', 'testpassword');
?>
When I try to connect to the mysql database through PHP, I get this error:
Click here
I figure the problem must be in the settings that are in the php.ini. But it seems that my php.ini is set up for MySQL although it is not mentioned in the output when I query the phpinfo page with this code
Here is the result from this:
Click here
It looks that your php is missing mysql module
in php.ini make sure that you have correct extensions path eg.:
extension_dir = "C:\php\ext"
and make sure you have commented out:
extension=php_mysql.dll
extension=php_mysqli.dll
Which version of PHP are you running and are you sure you have MySQL installed and running on localhost with a username of "testuser" and a password of "testpassword" (and have you reloaded the privilege tables after creating those users)?
Have you got IIS configured to log errors into a file - does that provide any more information?
Create a new PHP file that contains the following:
<?php
phpinfo();
?>
This will helpy you to discover if MySQL has been compiled in properly etc. Can you access the MySQL server from the command line; using something similar to:
mysql -u testuser -ptestpassword testdatabase
If you get a prompt from MySQL, the service is running and we can better help from there.
Hope that helps a little!
Are you selecting the database? After your connect statement, you need to use:
mysql_select_db($databaseName, $conn);
Documentation here: http://php.net/mysql_select_db
What I would do is to download the complete package in one go (Server, MySQL and PHP). There are tons of good resources like WAMP or MAMP (for MAC users). Once you download the package the installation is just easy and you don't have to set anything as it does it automatically.
They also have phpMyAdmin where you can manage your database and its users and privileges.
Once you got your server running you can test that code that seems fine for me. Try running something like this:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected';
mysql_close($link);
?>
Cheers

Categories