Skip to content
Advertisement

Cannot connect database to heroku with PHP

I am not able to connect my database(.sql) file to heroku. I am using PHP for connecting database and frontend of webpage. The heroku page is showing the static site and for login page, it’s showing error while registering new user. It’s working perfectly fine when I run it on xampp server.

On clicking submit button of a form on heroku deployed app page, it says “herokuapp.com can’t currently handle this request.”.

Here’s the link to my website https://enigmatic-journey-04762.herokuapp.com/login.html

Can someone tell me the steps of deploying php site on heroku using postgresql…..

Like this is the code I’m using for connection using PHP

<?php
session_start();

$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,'task1');
$email = $_POST['email'];
$pass = $_POST['password'];
$s = "select * from farmer where email='$email' && password='$pass'";
$result = mysqli_query($con,$s);
$num = mysqli_num_rows($result);
if($num==1){
    $row = mysqli_fetch_array($result); 
    $_SESSION['email']=$row['email'];
    $_SESSION['fname']=$row['name'];
    $_SESSION['fcity']=$row['city'];
    $_SESSION['logged_in']=true;
    header('location:profileFarmer.php');
}
else{
    echo "Login error";
    echo "<script>setTimeout("location.href = 'login.php';",1500);</script>";
}

?>

Advertisement

Answer

This is what i did to connect to heroku postgresql. Change all mysqli functions in the php to PDO functions so that they work with postgresql as well as mysql also.

Open the .sql file and file queries that start with CREATE and remove ‘ ` ‘ this character For eg Something like this

CREATE TABLE `blog` (
  `id` int(11) NOT NULL,
  `blog_id` text NOT NULL,
  `heading` text NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_id` text NOT NULL,
  `image_link` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

should be changed to this

CREATE TABLE blog (
  id serial PRIMARY KEY,
  blog_id text NOT NULL,
  heading text NOT NULL,
  created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  user_id text NOT NULL,
  image_link text
) 

and paste it to the heroku postgresql console and run it

Use pgweb for a phpmyadmin like interface for postgresql. Get the credentials to connect to the postgresql server in the heroku dashboard

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement