Connecting to a mysql Database via PHP? - php

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

Related

How do I connect to an online MySQL database using PHP?

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.

Mysql connection error

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

Cant see data after importing in _post form

Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$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('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli

Error connecting PHP to SQLServer

I am using a script php that connect to sqlserver but it doesn't works.
//connection to the database
$dbconn = mssql_connect($Server)
or die("Couldn't connect to SQL Server on $Server");
if($dbconn)echo 'Connected';
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
![when running script it gives this error, i m using windows authentication][1]
http://i.stack.imgur.com/a9ndN.jpg
Check mysql_connect()
$dbconn = mysql_connect('hostname','username','password');
Connect using mysql_connect()
$dbconn =mysql_connect(servername,username,password);
example
$con = mysql_connect("localhost","me","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

php mysql problem

I have installed php and mysql on windows xp with iis 5.1 as my web server. But when i run the following code it just shows me a blank page. Can anybody let me know where i am wrong. Thanks in advance.
<?php
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
Well, it doesn't generate any output if the connection succeeds so that's probably what's happening. What would you expect?
<?php
$con = mysql_connect('localhost', 'root', 'xxxx') or die(mysql_error());
?>
what does this print ?
* make sure you have mysql running
* make sure you have php running on IIS
* try to check in php.ini:
error_reporting = E_ALL
display_errors = On
Use the following code
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} else {
die('Connection ok!');
}

Categories