I just started using a web hosting service and I'm new to PHP and MySQL. I was trying to use the video I see here https://www.youtube.com/watch?v=gvGb5Z0yMFY as a simple recipe on adding a row to a table. I have a table called forumites in my MySQL database and it has 3 columns -- username, email and password.
HTML form:
<div class="boardbox hidden" id="regdiv">
<form id="regform" action="newuser.php" method="post">
<p>Username:<input class="threadipt" type="text" name="username"></p>
<p>Email address:<input class="threadipt" type="text" name="email"></p>
<p>Password:<input class="threadipt" type="text" name="password"></p>
<button onlick="phpfunction">Create Account</button>
</form>
</div>
newuser.php:
<?php
$conn = mysql_connect();
if (!$conn)
echo 'could not connect';
$db = mysql_connect_db('forumites');
$name = $_Post['username'];
$em = $_Post['email'];
$pass = $_POST['password'];
if (!mysql_query("INSERT INTO forumes VALUES('$name','$em','$pass')"))
echo 'Could not enter user info:';
?>
I think the part that is confusing me is the mysql_connect(...) command. In the video I'm watching, the guy writes mysql_connect('localhost', 'root', ''), but I'm not sure if he's using a web hosting service like I am. I read the W3Schools page on the command http://www.w3schools.com/php/php_mysql_connect.asp but it's not very informative.
Long story short, I'm trying to connect to the databse on my web hosting service and I'm probably doing a ton of things wrong. I was wondering if you guys could point out a few of the things I'm doing wrong.
to answer your question about 'localhost' when creating a database connection, and this applies to mysqli and PDO as well, which as John Conde commented, you should be using instead of mysql_
if your database is hosted on a computer other than your local machine, the hostname 'localhost' can be replaced either with an IP address, or with a domain address.
for example if you're on an internal network, and your database is on another computer within your network, you can use '192.168.1.15' (if the database computer's IP ends with 15, that's just an example, you need the actual IP of that machine)
if it's on a website, you can use the website, for example, if msn is hosting your database, your hostname would be www.msn.com. your host provider should have something like cpanel which provides you with that sort of information
Related
I have a textarea in which you type your message and a textarea that is supposed to output all messages from any hosts on my page:
<form action= "chatroom.php" method="post">
Chatbox:<textarea name="results" rows="10" cols="40">
<?php
$accum = $results . $message;
echo $accum;
?>
</textarea>
<br>
Send message:<textarea name="message" rows="7" cols="30"></textarea>
<input type="submit" value="Send" name="send">
</form>
It works fine, but only on any individual computer.
I don't believe it's a server issue (using WAMP). The site is on the web, and other echoes do appear on other clients such as
echo "<br>Connected to MySQL.<br>";
But, when I send a message on my server computer, it does not appear in the Chatbox textarea on my host computer, or vice versa.
Already tried moving the Chatbox textarea outside the form, which did not solve the problem.
Tried just echoing $accum straight onto the page and not into the textarea, which did not fix the problem.
<?php session_start(); ?>
at the start of chatroom.php didn't work either.
As a side note I'm using mySQL and used "localhost" rather than my external IP in mysqli, in my php code. I don't think this is the problem because the database does add a new user when they sign up remotely.
I thought that all PHP was executed on the server, so shouldn't $accum be picking up text from all hosts?
I'm wondering if the problem is that all clients have their own $message, $results and $accum variable because each client is connected to the server, and not to each other? So would that explain the behavior? (Or am I not completely correct?)
And forgot to mention, I am refreshing the page via clicking Send again to check if the text appeared; I haven't implemented auto-refresh yet.
edit:
in chatroom.php:
<?php
$message = $_POST["message"];
$results= $_POST["results"];
?>
I'm just turning the html name parameter into a php variable, then posting that variable right back onto the page in a different location.
Also have:
<?php
$username = $_POST["username"];
$word = $_POST["word"];
$conn = new mysqli("localhost", "user", "pw", "", 8080);
if($conn->connect_error)
echo $conn->connect_error;
else
echo "<br>Connected to MySQL.<br>";
if ($conn->query("create database chatdb")===TRUE)
echo "Created database";
else
echo $conn->error;
if ($conn->query("use chatdb")===TRUE)
echo "using chatdb";
else
echo "<br>Not using chatdb<br>";
if (
$conn->query("create table
users(
id INT(6) unsigned auto_increment primary key,
username varchar(30),
word varchar(30)
)")===TRUE)
{
echo "table created";
}
else
echo $conn->error;
if ($conn->query("insert into users (username, word) values ('$username',
'$word')")===TRUE)
echo "<br>inserted values<br>";
else
echo $conn->error;
?>
My sign-up page:
Sign Up:
<form action="chatroom.php" method="post">
Username:<input type="text" name="username"><br>
Password:<input type="text" name="word"><br>
<input type="submit" value="Sign Up!" name="signup">
</form>
Again, I don't think the mySQL has anything to do with it-- but I could be wrong.
The HTTP protocol is a Request/Response protocol. When someone displays the chat page, I assume your code reads messages from the database and shows those.
There was a request (GET /chat.php, Response is HTML for chatpage) and at that point the connection between the web browser and server is closed.
After that, there is no way for that web client to know that someone posted a new message into the database unless:
There is a persistent socket connection (websockets) OR
Your application is polling the server (perhaps using a timer and ajax calls OR
Your client application is refreshing at some interval
Solutions 1,2 and 3 require some level of javascript competency.
I am trying to connect to a specific wifi network by keying in the ssid and password manually and then submitting using a button. This is all done on a RPi web page server which I am accessing through an access point of the RPi. What I am trying to achieve is that php gets the values(ssid and password) of the network, and pass it on to python so that it can connect. I have tried the following codes but to no avail. Would appreciate any help.
HTML page:
<form action="wificonn.php" method="POST">
SSID:<input type="text" name="ssid"/>
Password:<input type="password" name=password"/>
input type ="submit" /> </form>
PHP code(wificonn.php):
<?php
$var1 = "ssid";
$var2 = "password";
exec ( "/usr/bin/python wigi.py $var1 $var2 $var3" );
?>
Referenced the php code from this link
Python wifi connection referenced from here
Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));
I have to build an android app which connects to a remote MYSQL webserver and retrieve information for display purpose.
So I did research coz I had no idea where to start. I read an article it mentions the better approach for that is to use php script. And didn’t know anything about how server, database and php works, so I studied php from here “http://www.homeandlearn.co.uk/php/php12p1.html” to understand and downloaded WhampServer to do some testing. On local machine everything worked fine.
But main thing I don’t understand is “HOW TO CONNECT TO REMOTE SERVER/DATABASE”.
It’s obvious that I’m doing something really stupid, I just need help to find out what am I doing wrong.
When we test php script on local machine in webrowse we use "localhost/some.php." But when I want to test same php script on remoter server from my local machine then what and how should I do? Do I need to make some changes in configuration file on server side?
I have done more research before asking this question here to understand remote server connection in php but I still don’t understand. I have gone through almost all the pages within the link below:
https://www.google.co.uk/search?q=connect+to+remote+mysql+webserver+php&rlz=1C1AVSX_enGB447GB448&oq=connect+to+remote+mysql+webserver+php&aqs=chrome.0.69i57j69i62l3.19724j0&sourceid=chrome&ie=UTF-8#fp=5a2359cf96dc5f79&q=how+to+connect+to+remote+mysql+web+server+php
And help would be much appreciated.
If my question is not clear please let me know I'll try to explain more. or think of it as if you have to connect to a remote MySQL server how would you do , means what is the process and steps involved in that.
Thanks everyone.
Edit
I have created a database "dealt3_raj_test" on remote server. and when I type "examplewebserver.CO.UK/myphpscriptname" in my web browser.
It gives me error "An error occurred , You have reached the error page"
<?PHP
$user_name = "dealt3_raj";
$password = "5dN5nh&eMd(vCR$dzk";
$database = "dealt3_raj_test";
$server = "examplewebserver.CO.UK";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if($db_handle)
{
print "Connected";
}
else
{
print "Can not connect to server";
}
if ($db_found)
{
print "DataBase found";
}
else
{
print "DataBase not found";
}
?>
Adding onto #user4035's comment, after opening the connection, use JDBC in your Android/Java code to interact with the database.
That said, it is not good practice. Rather create a web service
Your application may experience latency/connectivity issues. This will impact performance.
Your MySQL server will have to be open to remote connections which is strongly advised against
If your Android App is intended for public usage, it means the database username and password of your MySQL server reside on everyone's phone using your app. Encrypted or not this makes your database server a "step less" secure
Well answered here on SO (JDBC vs Web Service for Android)
use this code to connect with data base
$username = "database user name";
$password = "DB password";
$hostname = "hostname";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("data base name",$dbhandle) or die("Could not select examples");
PLEASE CLOSE THIS I HAVE FIGURED OUT THE
------------MY PROBLEM----------------------------------------
mysql_select_db("members") or die(mysql_error());
echo "Database Found! ";
----------------SOLUTION------------------------------------------------------------------
mysql_select_db("a2670376_Pass") or die(mysql_error());
echo "Database Found! ";
Here's my script
<?php
$ud_ID = $_REQUEST["ID"];
$ud_firstname = $_POST["ud_firstname"];
$ud_surname = $_POST["ud_surname"];
$ud_FBID = $_POST["ud_FBID"];
$ud_IMG = $_POST["ud_IMG"];
mysql_connect('mysql13.000webhost.com'… 'a2670376_Users', 'Password') or die(mysql_error());
echo "MySQL Connection Established! <br>";
mysql_select_db("members") or die(mysql_error());
echo "Database Found! <br>";
$query = "UPDATE stokesley_members SET firstname = '$ud_firstname', surname = '$ud_surname',
FBID = '$ud_FBID' WHERE ID = '$ud_ID'";
$res = mysql_query($query);
if ($res)
echo "<p>Record Updated<p>";
else
echo "Problem updating record. MySQL Error: " . mysql_error();
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
IMGNU: <input type="text" name="ud_img" value="<?=$IMGNU;?>"><br>
First Name: <input type="text" name="ud_firstname" value="<?=$firstname?>"><br>
Last Name: <input type="text" name="ud_surname" value="<?=$surname?>"><br>
FB: <input type="text" name="ud_FBID" value="<?=$FBID?>"><br>
<input type="Submit">
</form>
Here's my error
MySQL Connection Established!
Access denied for user 'a2670376_Users'#'10.1.1.40' to database 'members'
I don't know what the 10.1.1.40 is about though I have tried changing it to
("mysql13.000webhost.com", "a2670376_Users", "Password")
and still the same thing
now this confuses me a lot so I'm not even sure there is an error but i think there is cause if there was no error the script would show
could this error be caused because I haven't made the file update.php yet?
i have worked out many bugs in this already but cant seem to get this one out please help me you will be a lifesaver and give me credit for noobish script I'm only 13
I don't need to grant rights because I already have rights I have a file named connect.php it connects the register and login scripts that I have that works fine but this wont..
http://fni.site11.com/edit.php is the page I am working on
Please check database location and set the privileges for user.
first of all switch from mysql_ to mysqli_ or PDO they are more reliable.
Then, check the spelling (host, user and password) and the case ! password and username are case sensitive.
Be sure that user a2670376_Users has privilege for selecting, updating and inserting
First make sure that the user specified above enough privileges to select the data from the db given. you can login to the db hosting account and check the privileges of the user "a2670376_Users". And 10.1.1.40 might be your hosted server IP Address.
If your db hosted on other server then you need to grant the access from * not only for localhost.
When creating MySQL databases and users, you then need to grant the user privileges to databases. This allows you to create multiple users with different access levels, for example a user that can do everything, and another user who may only have read (SELECT) access, so they can’t INSERT/UPDATE/DELETE or DROP databases and tables.
If you use something like cPanel, you can add users to your database from there.