Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!
$host_name = "www.yourdomain.com";
$database = "pdo"; // Change your database name
$username = "root"; // Your database user id
$password = "test"; // Your password
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Yes it is possible.You must have the username and password of that domain in which you want to connect.
mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());
Yes, Simply pass following details about your server:
<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$url = 'mysql:host=xxx.xxx.xxx.xxx;dbname=xxxxxx'
$username = xxx;
$password = xxx;
$db = new PDO($url, $username, $password);
$query = $db->query('select * from some_table');
$query->execute();
$res = $query->fetchAll(PDO::FETCH_ASSOC);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$conn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$conn);
#mysql_select_db($database) or die ("Unable to select database");
?>
I have a doubt in line "$conn = mysql_connect($host,$user,$pass);"
<?php
$host ="localhost";
$user = "root";
$pass = "";
$db = "StuDet";
$con = mysqli_connect($host,$user,$pass,$db);
// Check connection
if (!$con){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try the above code. Use mysqli.
The code is correct. If no connection is made, check your credentials and db name.
Also, since mysql is deprecated, change to mysqli.
link http://php.net/manual/en/book.mysqli.php
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I'm new to PHP and i coded a php page and created a database. I tried to connect db but it's not connecting.
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
Hi Try to debug and echo bugs in connection with mysqli_error() function.
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
You are mixing mysql and mysqli
You have to replace
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
With this code
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
connection variable then database name ..try this
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
and mysql close should be like this
mysqli_close($mysqlconn);
dont mix mysql with mysqli
You should write
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
This will solve Your Problem
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
this mycode
I'am try to edit but no success.
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
{
//*** Insert Record ***//
$file = $_FILES["filUpload"]["name"];
$fileup = mysql_real_escape_string($file);
$strSQL = "INSERT INTO fileproject(filename) VALUES($fileup);";
if(!$strSQL){
echo "<h1>Error Store FileName2DB<h1>";
exit;
}
echo "Upload Complete<br>";
}
you dont execute your query. You have to use your object to the database for query.
try
{
$bdd = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'root', 'yourpassword',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
{
//*** Insert Record ***//
$file = $_FILES["filUpload"]["name"];
$fileup = mysql_real_escape_string($file);
$strSQL = "INSERT INTO fileproject(filename) VALUES($fileup)";
$result=$bdd->query($strSQL);
if(!$strSQL){
echo "<h1>Error Store FileName2DB<h1>";
exit;
}
echo "Upload Complete<br>";
}
You are not using mysql extension for executing your query. Other example is about to PDO.
You can also INSERT the new row by using mysqli_* extension.
MYSQL procedural structure:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"],"docfile/".$_FILES["filUpload"]["name"]))
{
//*** Insert Record ***//
$file = $_FILES["filUpload"]["name"];
$fileup = mysql_real_escape_string($file);
$strSQL = "INSERT INTO fileproject (filename) VALUES ('$fileup')";
// here you need to use mysqli_* extension
if(mysqli_query($conn, $strSQL)){
echo "Upload Complete<br>";
}
else{
echo "<h1>Error Store FileName2DB<h1>";
// exit;
}
}
mysqli_close($conn); // DATABASE CONNECTION CLOSE
?>
Side note:
As I mentioned in comments $fileup value is a string value so you need to use single quotes between the variable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do i write the connection string to connect to mysql database with this info:
Using php connection string
Database Details:
phpmyadmin url: https://medicalng.com/phpmyadmin
host: localhost
database: wlfmedic_ptest1
username: wlfmedic_ptest1
password: Prog#te$t104
Use mysqli. mysqli connection is like this:
$host = "localhost";
$database = "wlfmedic_ptest1";
$username = "wlfmedic_ptest1";
$password = 'Prog#te$t104';
$connection = mysqli_connect($host, $username, $password, $database);
if(mysqli_connect_errno()){ //To show error if fails to connect
die(mysqli_connect_error());
}
Something basic, not secure tho.
<?php
mysql_connect(localhost, ##YOUR USERNAME##, ##YOUR PASSWORD##);
mysql_select_db('##YOUR DATABASE NAME##');
$result = mysql_query("SELECT * FROM emails");
?>
and updated mysqli http://www.sanwebe.com/2013/03/basic-php-mysqli-usage
<?php
$db_hostName="localhost";
$databaseName= "wlfmedic_ptest1";
$db_userName= "wlfmedic_ptest1";
$db_password= "Prog#te$t104";
$con=mysql_connect($db_hostName,$db_userName,$db_password);
if(!$con){
die ("Could Not successfully with DataBase".mysql_error());
}else{
echo "Connect connect with succssfully .";
}
$db=mysql_select_db($db_databaseName,$con);
if(!$db)
{
echo "Can notConnect with Bd Social";
}
else
{
echo "Conncet With Bd Social";
}
?>
try this ..
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found ";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
?>
also you can refer this link
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My database is on dream host. And its unable to connect .
MY code is
<?php
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$databse);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
?>
it gives error like this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/demo_smartmobe/demos.smartmobe.com/nayacinema/test.php on line 20
what may be the problem?
You have a spelling mistake.
$con=mysqli_connect($hostname,$username,$password,$databse);
Should be
$con=mysqli_connect($hostname,$username,$password,$database);
(database spelt wrong)
i have tested your code there was no connectivity.. because of spelling mistake here is correct code , i have tested on my local machine
<?php
$hostname = "localhost"; // eg. mysql.yourdomain.com (unique)
$username = "root"; // the username specified when setting-up the database
$password = ""; // the password specified when setting-up the database
$database = "test"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM user");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['user_name'];
echo "<br>";
}
?>
Thanks