I already have the mysql database made, I just need to connect php to mysql locally but I don't know the php commands for that.
cheers
$server = 'localhost';
$user = 'myUser';
$pass = 'myPass';
$dbname = 'MyDatabase';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db($dbname);
The following contains a pretty good example: http://ca3.php.net/manual/en/mysql.examples-basic.php
This is a simple connect to the mysql server. After Connecting to the server it selects a DB
<?php
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>
Related
I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>
So I wanted to install an acp on my homepage. I uploaded all needed files on my webserver (I use FileZilla Client) and opened phpMyAdmin with xampp.
It connects the database but then it says
"Database does not exist."
I'm fairly new to working with phpMyAdmin and mySQL as well.
this is what my db.php looks like:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw, $db) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
What did I do wrong?
Thanks in advance. :)
Try the below working code:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
NOTE: Don't use " mysql_ ". Better to use " PDO or Mysqli_ " ( safe and secure ) .
I am using phpMyAdmin + MySQL.
I created a database and am now trying to make the connection in a PHP script. The curious thing is that connecting to the DB works, so I get the "Connected to MySQL server" message, but I when it comes to selecting the 'petfood' database, the script shows "DIED at selection".
Any idea why? Thanks, and here's my piece of code:
<?php
$user = 'localhost';
$pass = 'password';
$db_name = 'petfood';
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
echo "Connected to MySQL server";
mysql_select_db($db_name) or die("DIED at selection");
echo "Database Selected";
?>
Spot the difference:
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
^----
mysql_select_db($db_name) or die("DIED at selection");
^---
If you had proper debugging, you'd have been told about the problem:
mysql_select_db($db_name) or die(mysql_error());
^^^^^^^^^^^^^^
Never output a fixed (useless) error message when you can have the system TELL you what's wrong.
1: using mysql
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db("examples",$dbhandle) or die("Could not select examples");
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($query);
2: using mysqli
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($link, $query);
Keep getting same error message
(Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/00/7882800/html/Connections/dueslogin.php on line 27
Unable to connect to database! Please try again later.)
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "TarHeelsDues.db.7882800.hostedresource.com";
$username = "TarHeelsDues";
$dbname = "TarHeelsDues";
//These variable values need to be changed by you before deploying
$password = "************";
$usertable = "DuesLogin";
$yourfield = "your_field";
//Connecting to your database
mysql_connect($hostname, $dbname, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "Name: $name<br>";
}
}
?>
change the mysql_connect line to
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
A little mistake on there, you have to use $username not $dbname in mysql_connect.
mysql_connect($hostname, $username , $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
The correct syntax should be like this:-
mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($TarHeelsDues) or die('Cannot connect with the database!');
I am just using a basic code to connect to my Mysql database. I am able to connect to my server but not database. using sqlyog:
<?php
$username = "root";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username) or die("Unable to connect to MySQL");
$selected = mysql_select_db("project",$dbhandle) or die("Could not select project");
$sql = "SELECT image_small FROM images";
mysql_query($sql,$selected);
while($row=extract_row($sql))
{
echo $row['image_small'];
}
?>
where is password of database? mysql_connect should be used as:
mysql_connect("localhost", "mysql_user", "mysql_password");
otherwise it will be the default password that will be used
There are so many things wrong here.
1. Your have a blank password for the root user in your database.
2. You're using mysql_* which everybody know is subject to many hasck.
3. You're trying to "extract" a row from your SQL query.
Use PDO:
$DB = new PDO("mysql:host=localhost;dbname=project","root","root_password");
$sql = "SELECT image_small FROM images";
foreach($DB->query($sql, PDO::FETCH_ASSOC) as $row) {
echo $row['image_small'];
}
try to connect using the following statement
$selected = mysql_select_db("project");
// i think you have to provide password in here mysql_connect($hostname, $username,$password);
since it is localhost and user is root you could use like this
mysql_connect($hostname, $username,"");