Cannot connect database to heroku with PHP - 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>";
}
?>

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

Related

Android listview image and text from mysql using php

I'm trying to fetch images and text from mysql using php with this example
example.
The result from php code is empty, where is the error in this php code?
I store my images in server folder and add links in the database.
<?php
require_once('dbConnect.php');
$sql = "SELECT * FROM androidosnames";
$r = mysqli_query($con,$sql);
$result = array();
while($res = mysqli_fetch_array($r)) {
array_push($result,array(
"AndroidNames"=>$res['AndroidNames'],
"ImagePath"=>$res['ImagePath']
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
using your code and this dbConnect.php
<?php
$con = new mysqli('localhost', 'broadcasting', 'broadcasting', 'broadcasting', 3306);
and this database information
CREATE TABLE IF NOT EXISTS `androidosnames` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`AndroidNames` varchar(50) NOT NULL,
`ImagePath` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
INSERT INTO `androidosnames` (`id`, `AndroidNames`, `ImagePath`) VALUES
(1, 'myname', '/ports/links');
yields following result when executing on command line (php -f test.php)
{"result":[{"AndroidNames":"myname","ImagePath":"\/ports\/links"}]}
So everything works as intended. It looks to me like the information from the database cannot be retrieved in your environment.

Code for transfering the table content from localserver to remote server DB using PHP

<?php
$dbhost1 = "10.0.11.211";
$dbuser1 = "root";
$dbpassword1 = "";
$db1 = "product";
$connection1 = mysqli_connect($dbhost1, $dbuser1, $dbpassword1) or die (mysqli_error());
mysqli_select_db($connection1, $db1);
//MySQL Server 2
$dbhost2 = "localhost";
$dbuser2 = "root";
$dbpassword2 = "";
$db2 = "product";
$connection2 = mysqli_connect($dbhost2, $dbuser2, $dbpassword2) or die (mysqli_error());
mysqli_select_db($connection2, $db2);
$sql = "insert into 10.0.11.211.product.inserting select * from localhost.product.inserting";
$result = mysqli_query($connection1, $sql);
if (!$result) {
echo "connection error";
}
?>
How can I correct this code? Actually I connected to servers at time.
You can setup federated tables, which is basically linking a table on one server to a table on another. Then use the federation to do your data transfers.
First, you must have a table on the remote server that you want to
access by using a FEDERATED table. Suppose that the remote table is in
the federated database and is defined like this:
You need to change database host and database/table name as per your details
CREATE TABLE test_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL DEFAULT '',
other INT(20) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
INDEX name (name),
INDEX other_key (other)
)
ENGINE=MyISAM
DEFAULT CHARSET=latin1;
Next, create a FEDERATED table on the local server for accessing the remote table:
CREATE TABLE federated_table (
id INT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) NOT NULL DEFAULT '',
other INT(20) NOT NULL DEFAULT '0',
PRIMARY KEY (id),
INDEX name (name),
INDEX other_key (other)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root#10.0.11.211:9306/federated/test_table';
Then you can query it like any other table.
There are however a decent number of limitations you should read about including the remote password being stored in plain text. If this was a temporary setup purely for a once off copy, and the server isn't available to the public you have already minimised most of the risk associated with it though.

Copying Mysql database table with PHP, does not give exact copy as like operation function copy table in phpmyadmin?

When I try to copy database table from one db to another via phpmyadmin copy table to(database.table) operation function everything works fine, but when I do with the below code it works but some value in database table is turned to be null. So can I know how to make exact copy of the database table from one database to another is there is any other way to do so and what is error in the below code which make some tables null.
Note: Only the database table not whole database.
Here is the code
<?php
if (!$con = new mysqli('localhost', 'username', 'password', 'db_movies2'))
{
die('An error occurred while connecting to the MySQL server!<br><br>'.
$con->connect_error);
}
$sql = array('DROP TABLE IF EXISTS ba_content;',
'CREATE TABLE ba_content SELECT * FROM db_movies1.ba_content');
if (sizeof($sql) > 0)
{
foreach ($sql as $query)
{
if (!$con->query($query))
{
die('A MySQL error has occurred!<br><br>' . $con->error);
}
}
}
$con->close();
?>
Here is example of database in which changes that occur automatically when database table is copied via the above PHP code it occurs in most of the tables.
- `content_name` varchar(255) DEFAULT NULL,
- `type` varchar(25) DEFAULT NULL,
+ `content_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
+ `type` varchar(25) CHARACTER SET utf8 DEFAULT NULL,

How to create a SQL table using php IF date is equal to X

I'm trying to create a MySQL table using a PHP IF statement. I am able to create the table using an IF statement; although the problem I am running into is that the table only gets created when the date is >= to X, as opposed to how I would like to specify the statement; IF the date is == X.
How can I automatically create this SQL table IF the date is == X,
and, is there a way to create this table without having to go to the page where the table data will be displayed and refresh the page?
Here is my code:
$timezone = date_default_timezone_set('America/Chicago');
$TheDate = time("F j, g:i a");
$servername = "server";
$username = "user";
$password = "pass";
$dbname = "db";
$conn = mysqli_connect('server', 'user', 'pass');
(!$conn->set_charset("utf-8"));
if (!$conn) {
die("Connection failed: " . mysqli_error());
}
mysqli_select_db($conn, $dbname);
if ($TheDate == strtotime("April 24, 9:53 pm")) {
$CreateTable = "CREATE TABLE issue11 (
ID INT(5) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
First VARCHAR(40) NOT NULL,
Last VARCHAR(40) NOT NULL,
Title VARCHAR(50) NOT NULL,
StoryLink VARCHAR(140) NOT NULL,
Genre VARCHAR(11) NOT NULL,
About VARCHAR(2000) NOT NULL,
Link VARCHAR(125) NOT NULL,
Picture VARCHAR(500) NOT NULL,
ALT VARCHAR(100) NOT NULL
)";
mysqli_query($conn, $CreateTable);
mysqli_close($conn);
}
Actually there is no problem with the above code ,
The main problem is
The code in the if condition will only work if you run this page at
"April 24, 9:53 pm" (the date you specified for equality).How can you do that , I mean the time is constantly changing and you can't do this checking each time when time changes using php alone.
Idea is by using ajax call
Using ajax
one way is to make an ajax request to run the above code each second , and its not an effective and efficient way to do.
Another way would be to do the checking at the client side and do the operation (create table) only when the if returns true using an ajax call to the page where you wrote the above code.

Problems automatically creating a mysql database so it can be used on a fresh computer using PHP

I am trying to automatically create the database and table so the website i am creating can be used on a fresh version of XXAMP. At the moment I used PHP myadmin to create the table. however when it loads on a fresh version of XXAMP the database will not be saved on the fresh computer. Therefore im trying to create PHP to automatically create the database and table so content can be added. This is my attempt at the moment but it doesn't seem to be working. Can anyone push me in the right direction?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "contentdatabase";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table
$sql = "CREATE TABLE items
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
itemName text NULL,
itemDescription text NULL,
itemPrice float NULL,
itemStock smallint(6) NULL,
itemImage VARCHAR(100) NULL,
)";
// use exec() because no results are returned
$conn->exec($sql);
echo "Table items created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
What you want to do is known as database migration, and there are some frameworks available for it, please take a look at this which is a framework focused in DB migrations.
Replace the corresponding part of your code with the below. I have added comments in the relevant places. Remove the comments before running the code
// sql to create table
$sql = "CREATE TABLE items ( //yours lacks this opening parentheses
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
itemName text NULL,
itemDescription text NULL,
itemPrice float NULL,
itemStock smallint(6) NULL,
itemImage VARCHAR(100) NULL //I have removed the last comma (,)
)";
Thank you for your help!
I have got this semi working now thankfully! I can now create a new database and table. However I am now getting this error but i guess its some sort of if statement i need to add.
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'item' already existsCREATE DATABASE contentdatabase1
SQLSTATE[HY000]: General error: 1007 Can't create database 'contentdatabase1'; database exists

Categories