Is it possible to go from localhost to external adress - php

I can connect to my localhost and get info from a local phpMyAdmin database.
Now I want from a php file, that is stored on my local computer, but now the php file refers me to go to an external website:
<?php
//mysql_connect('localhost','root','root');
mysql_connect("http://eternal website","username","password");
mysql_select_db("dlvplant_portret_v1");
$q=mysql_query("SELECT * FROM people WHERE voornaam LIKE '%%'");
//$q=mysql_query("SELECT * FROM people WHERE voornaam ='""'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
I tried the link and copy/paste the username and pass and I use the good website link. (the link brings me at the myPhpAdmin login from the server). So everything works and is filled in good. But when I try to connect with my android phone (from phone to local comp's php file) then the php file refers me to go to external webpage, but it says: Host is unknown
Is it even possible to go from local to external and back again?

You can connect to external MySQL hosts - but you do not use the HTTP protcol for it. And external access needs to be enabled on the MySQL server.

You should use plain ip address(xxx.xxx.xxx.xxx) or single "DOMAIN.SOMETHING" instead of "http://DOMAIN.SOMETHING"
mysql_connect("IPADDRESS","username","password");

Related

Website to read file on diff raspberry pi and display values based on variable

I'm trying to create a website and 2 raspberry pi's that will connect to the website.
The goal is:
rpiA's display will be different from rpiB.
I'll put a file on each rpi containing their name. When I visit the website using rpiA, rpiA's name will be displayed (vice versa).
What I did is I placed a file in var/www/html/name.php
<?php
$xname= 'RPI-001';
echo $xname;
?>
Then on the website I placed:
$device_name = file_get_contents('http://127.0.0.1/name.php');
echo $device_name;
However, the result is always empty. I checked the allow_url_fopen and it is On. The reason maybe the 127.0.0.1 is broad and it needs a specific ip?
I also tried curl but the result is Error 404.
Is there another way to do this?
I did not consider login or session since I'm using a small screen in rpi and it will be hard to type without vnc.
127.0.0.1 is the IP of the system running the request. If you run that file_get_contents on any central server and access this using a browser, it will always access the server itself.
If you want to access any website on the system that is accessing the website, you need to use the IP address of the client, like file_get_contents('http:/' . $_SERVER['REMOTE_ADDR'] . '/name.php')

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");

ESP8266 to XAMPP server

Recently I have been trying to work with my ESP. I have set up a server using XAMPP on my laptop. I can access that server using 127.0.0.2. I know that the content viewed on this page is present in the htdocs folder in XAMPP.
So I have created a folder named TEST in htdocs. The ESP is connected to PSoC and is sending some data at regular intervals to the TEST folder on the server. My question is: what is the IP address that will be needed to connect to the server when I use the AT+CIPSTART command on the ESP side?
Is it 127.0.0.2 or some other IP from the router? I have tried sending data to ThingSpeak before and there they provide a ready-made GET request link to send data to the server. What will be the GET request link in my case if the server is created using XAMPP? Please help as I am new to networking. Thank you.
Ok. I wrote a php file which takes value in a variable SAP and writes into a text file which is stored in the ht docs folder. The php code is as follows
'
$content = "SAP ID :".$var1." is present for the lecture \r\n";
echo $content;
echo "<br >";
$status = file_put_contents('attendance_record.txt',$content,FILE_APPEND);
if ($status != false)
{
echo "Data is written to the file :p ";
}
else
{
echo "Data was not written into the file :( ";
}
?>'
After then I enable the Xampp server and access this file by using 'http://127.0.0.2/receiver.php?SAP=104' Until this point everything is working fine and the text file is being updated. Now the same thing is needed to be done via AT commands using the ESP. The following lines of code is running on the ESP side.
AT+CWJAP="SSID","PASSWORD"
AT+CIPSTART="TCP","192.168.0.104",80
AT+CIPSEND=35
GET /receiver.php?SAP=69 HTTP/1.1
Now i'm not getting any updates on the text file. Any help will be appreciated. Thank you.
172.0.0.2 is the same at calling 'localhost' so you would need your laptop's IP for the ESP to connect to. Depending on your OS you can find that opening your terminal and write ipconfig(windows) or ifconfig(linux) On a MAC go to System Preferences > Network and your IP is displayed under'Status:'

How to setup Database connection using php coding and where to save that file?

I am having some problems with php coding i have written php coding correct but it is not working,it is giving an error that "The Object not found" in browser.
Hey guyz please help me i am stuck on this and tried every possible thing to make this coding work.
Thanks
First php is not supported by itself in browser, you need a server to run php,you can use a localhost server like wamp, create DB in phpMyAdmin panel with the desired username and password then you can connect to it from your classes.
$this->host="localhost";
$this->username="root";
$this->password="";
$this->database="dbname";
$con=mysql_connect($this->host,$this->username,$this->password) or die(mysql_error());
$res=mysql_select_db($this->database,$con)or die(mysql_error());
you can save the file in any folder but it is advisable for u to save it in where all your files are kept for a quick reference.
below is php code for setting up database:
$con=mysqli_connect("host","user","password") or die(mysqli_error());
mysqli_select_db($con,"yourdatabasename");
Note:
host=localhost(if wamp or xampp is used) or your domain name(like www.google.com)
You can save dis php file with any name e.g config.php.
user= username of your database used in your phpmyadmin. likewise your password.
If you are using wamp or xampp, use dis format:
$con=mysqli_connect("locahost","root","") or die(mysqli_error());

PHP MySQL fails when page is viewed remotely

I have two servers on my local network - one a web frontend and the other a MySQL backend. I have a PHP script that looks like this:
<?php
error_reporting(-1);
echo "Connecting...\n";
$link = mysql_connect("192.168.1.15", "-----", "-----") or die(mysql_error());
echo "Communicating with the server...";
mysql_query("INSERT INTO .....
//More code down here...
?>
This script is called on my web frontend to connect to the backend server. When this script is accessed from the local network (i.e. when I open the page by going to http://192.168.1.14), the script outputs
Connecting...
Communicating with the server...
and a row is added to the database, as it should. However when I connect remotely (i.e. going to http://myDomainName.com/mysql_insert_script.php) from a connection not on the local network, all I see is:
Connecting...
No error messages follow, the script just cuts off, and no data is added to the database. When I place a second, 'proxy' script on the server that simply requires() the above script and then I access the proxy remotely, everything works fine. Below is the proxy script, so you can get a better idea of what works and what does not:
<?php
//this script makes it appear that the mysql_script is being viewed from the local network
//I exist on the web frontend at 192.168.1.14
require("http://192.168.1.15/mysql_insert_script.php");
?>
I am sorry if I can't provide any more information, but I am stumped. Any help would be appreciated.
Chris
P.S. - I have verified that the mysql server is accessible from external hosts on the local network, but I have a firewall that prevents connections from outside my network. I don't think this would matter, however, as the MySQL server and the PHP script connecting to it are both run on the local net.
you got wrong server name

Categories