How to execute queries in remote servers - php

I am trying to insert data to a web site from the code of another web site. How can I use the mysql_connect() command for that. Can I use the IP address of the first web site in the second web site code?
Please help me

Yes, you can use a remote MySQL server with mysql_connect(). Use mysql_connect() according to the PHP manual (http://php.net/manual/de/function.mysql-connect.php) and use your server's IP as $server. However, make sure the MySQL port is accessible from remote on the MySQL server (port 3306).

For inserting data in the database you need to connect the database
Selecting DATABASE
you need to just connect the server on which the db exist in which we are inserting
<?php
$link = mysql_connect('ip or server of the db in which inserting', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make myDB the current database
$db_selected = mysql_select_db('myDB', $link);
if (!$db_selected) {
die ('Can\'t use myDB: ' . mysql_error());
}
?>
for inserting records in another website , you need to just connect the server and DB of that particular website, and fire Queries.
let me know if more..

Related

Access denied for user 'consult'#'localhost' (using password: NO)

I am using bluehost domain. I want to connect database but I don't know password. After the long war I found the username and localhost name. I am a beginner. Help me to find the phpmyadmin password.
This my php database conection code.This code is worked in localhost.
$link = mysql_connect('localhost', 'consult', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('consultv_hrms', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
Only the admin can reset database password. In this case you need to follow the instructions given by your hosting provider
Log into the cPanel
Click the MySQL Databases icon under the "Databases" category
Change the password
Refer this for detailed instructions

Database connectionto both local and online database

I have an online database and what I need to do is to fetch data from it and save to my local database(localhost). I tried adding remote mysql database cause it said it is needed(I added the IP address of my local computer), I dont know if its right. The problem is it gives me an error
"arning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '10.24.168.85' (4) in /home/umalert/public_html/server/_includes/connection.php on line 18
Database connection failed:".
For now I have this connection script:
// create database connection
$connection_local = mysql_connect('x.x.x.x:3306', 'xxx', 'yyy');
if(!$connection_local) {
die ("Database connection failed: " . mysql_error());
}
// selece database to use
$db_select_local = mysql_select_db('umalert_db', $connection_local);
if(!$db_select_local) {
die("Database connection failed: ". mysql_error());
}
Hope you can help me with this. Thank you.
you should export your data from one database and import to another one.

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to
Below is my Code
<?php
session_start();
$server = 'localhost';
$db_usernmae = 'root';
$db_password = '';
$database = 'househockey';
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}
?>
Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.
Thanks
As you have written your code :
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
This will when connection is true print the following: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work:
if(!mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
The ! will negate the returned value of your mysql_connect and tell you if you're connected.
As far as I know, PMA explicitly needs a username and password. Set a root password through
mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).
You may run into this problem if you have an anonymous user defined on your database.
"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order.
The server uses the first row that matches the most specific username and hostname."
Delete the anonymous user and try again.

best practice to handle Too many mysql connections

<?php
// Connect to mysql server
$link = mysql_connect(HOST, USER, PASSWORD);
if(!$link) {
die('Could not connect to server: ' . mysql_error());
}
// Select database
$db = mysql_select_db(DATABASE);
if(!$db) {
die('Cannot use the database');
}
mysql_set_charset('charset=utf8', $link);
//code
?>
I am php beginer.I have written a simple php HTTP API which read some data from database and return in body. This API is accessed by many uses application asynchronously. When we increase the number of user more than 200 each second, i nam getting following warning
PHP Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in /home/ws/public_html/include/get_details.php on line 3
I want to know best solution for it.
Thanks
first you can use mysqltuner to check your db status. Other solution, use redis or memcache to reduce the connections. If you have your sessions stored in DB, try to change and use redis.phpredis

Trouble with PHP mysql_select

Ok, I've just started learning php, and for some reason the mysql_connect() and mysql_select_db() do not appear to connect to the database.
I also need a good example of a mysql table, as I am not sure what to put in my table. How do I manage to connect my site to the database?!
Here is the code I am using in my connect.php page.
<?php
//not working
$connect = mysql_connect('localhost',
'myuser', 'password', 'mypass')
or die("This function should work.");
//still a lingering error
$db_select = mysql_select_db("mydbname", $connect);
?>
If you just start with PHP then a good idea would be to get Apache and MySQL on your own computer. There are plenty of easy installable packages like WAMP (http://www.wampserver.com/en/). There are examples and HOWTOs there. The good in this package, is that there is a PHPMyAdmin that comes packed, and you can use it to make a sample table, or just to play around.
Next, for the concrete code, there is a nice example at the PHP documentation site (http://php.net/manual/en/function.mysql-connect.php).
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
When using the local server you can use the ROOT user in MySQL. For a test table you can make one called, for example friends, with two VARCHAR fields for name and phone number. And you can play around to list them edit them and so on.
Good luck.

Categories