I am testing a website offline with XAMPP. My PHP code connects and works with my local MySQL database (i.e. 'localhost' or '127.0.0.1'). I wish to conduct tests using an online MySQL database.
I am trying to use PHPMyAdmin Demo Server but I get an error when trying to connect to the server. I changed the 'DB_HOST' to '192.168.30.23' (which seems to be the demo server ip address). Am I missing something?
Here is a snippet of my code:
<?php
define('DB_NAME', 'ears');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '192.168.30.23');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
?>
Thank you in advance.
The phpMyAdmin MySQL demo server is not available to test your script. Google for "public access MySQL server" to maybe find one.
Related
I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");
When I press my submit button, I get an error saying :
ErrorTable 'form2.demo' doesn't exist
form2 is my database name which is created in phpmyadmin.
I am a new MAC user.
Below is my php code.
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8888');
My ports are :
Apache : 8888
Msql : 8889
Full code is as below:
<?php
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8889');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Could not connect :' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can\'t use' . DB_NAME . ':' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
if(!mysql_query($sql)){
die('Error' . mysql_error());
}
mysql_close();
?>
Your problem have nothing to do with Mac. You have to show us more code. How the code in your form looks like? How you are sending stuff to database?
Let's assume that everything in code is set up properly. The main thing you have to check is if you actually have demo table in your database. Go to phpmyadmin and check it, if there is no table create it. You can use phpmyadmin to do that or do it via SQL query like that one
CREATE TABLE IF NOT EXISTS `form2`.`demo` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`));
Of course code above will create table with only ID column so you have to adjust it to your needs
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'customer');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
$mysqli->close();
Try the msqli library instead
I am trying to access localhost db from remote server .
$link = mysql_connect('192.168.65.44', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
Warning: mysql_connect(): Can't connect to MySQL server on '192.168.65.44' (4) in /home/xx/xx/xx/xx/xx
Could not connect: Can't connect to MySQL server on '192.168.65.44' (4)
The error tells that there's a problem in your host. If you are trying locally in your system, use
$link = mysqli_connect('localhost', 'root', '');
Note : mysql_ is deprecated. You need to use either Improved Mysqli (mysqli_) or PDO.
put "localhost" in place of "192.168.65.44" in
mysql_connect
Try this it will work :
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
I am very new to PHP so this is my first attempt to connect to a mysql database through a php file and I am getting this message. I dont know how much anyone can help me with this or if at least someone can guide me to a right direction
Can not use : soum_email1:
And my php looks like this
<?php
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
?>
You are assigning the mysql_select_db() function $db_selct, but then checking $db_selected (which with the code you've posted is always falsey.
Also, link should be $link (on line 9).
Your code should be:
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('could not connect:' . mysql_error());
}
$db_selct = mysql_select_db(DB_NAME, $link);
if(!$db_selct){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
You should note though that the mysql_* family of functions are now deprecated, and you should consider using MySQLi or PDO.
First of all:
Please use the pdo or mysqli database like Quentin wrote in the first comment.
Further you should name your variables right,
$db_selct = mysql_select_db(DB_NAME, $link);
and
if(!$db_selected){
die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
have different variable names.
Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares