I am trying to connect to my database via PHP but get blank page when I run through Safari(http://pushchat.local:11111/test/databasename.php)
I can see my datbasename.php file under http://pushchat.local:11111/test/ as shown below:
Following is my PHP code:
try
{
if (!defined('APPLICATION_ENV'))
define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : ‘development’);
require_once '../api_config.php';
$config = $config[APPLICATION_ENV];
$pdo = new PDO(
'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'],
$config['db']['username'],
$config['db']['password'],
array());
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query('SET NAMES utf8');
echo 'Database connection successful!';
}
catch (Exception $e)
{
echo 'Could not connect to the database. Reason: ' . $e;
}
I even tried to put a stop sigh using codebug but I think it never stops.
Thanks for your help.
You're missing a closing quote -
'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'].'',
-----------------------------------------------------------------------------^
Note the additional single-quote which balances the line. Error checking would have caught this.
I'm Sorry But I'm Seeing database.php Not databasename.php.
Have You Even Checked If The File Exists?
Also Connecting To The Database Should Be Typed In Config File & You should require It In You're Main PHP File.
For Example The Config File Should Look Like This:
<?php
$connection = mysql_connect('localhost', 'root', 'password'); //here is the host, username and password of mysql account.instead of localhost type in your websites domain name.
if (!$connection){ //and instead of password,type in your own password.
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('test');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
& The Main PHP Should Look Like this:
<?php
require('config.php'); //requires the config.php page
//rest of the code goes here...
?>
Also On Line 10,You Have Missed A Closing Single-Quote.
Related
I have a script and when I go to the website link(http://kodle.co.uk/login.php) the script comes back with this:
Warning: mysql_connect(): Access denied for user 'root'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 12
Warning: mysql_select_db(): Access denied for user 'hkode'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 13
Warning: mysql_select_db(): A link to the server could not be established in >/home/hkode/public_html/dbconnect.php on line 13
Connection failed : Access denied for user 'hkode'#'localhost' (using password: >NO)
The DBCONNECT.php script is as follows:
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'login_base');
$conn = mysql_connect("localhost", "root", "");
$dbcon = mysql_select_db(DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysql_error());
}
Go to your database and check if user root actually does not require a password. If it does then insert it as a third parameter in the empty single quotes below. Now use the code below and you will be just fine. Happy new year.
<?php
$con = mysql_connect('REPLACE_THIS_WITH_IP_ADDRESS_OF_YOUR_SERVER', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("login_base", $con);
?>
define('DBPASS', '');
You want to replace '' with 'youractualpassword'
Then change
mysql_connect("localhost", "root", "");
to
mysql_connect(DBHOST, DBUSER, DBPASS);
On a further note the comment
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
Suggest you don't use these methods. You should only do this if you absolutely know what you are doing, which seems like you do not.
I highly suggest using something like laracasts to learn php.
Good luck
It seems like the password is not right for user root also i would suggest you to use the constants for connecting to mysql which u have already defined.
i would rewrite connection statement and subsequent statement something like this
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}else{ // connection succesful
$dbcon = mysql_select_db(DBNAME);
}
Try this
$conn = mysql_connect(DBHOST, DBUSER);
$dbcon = mysql_select_db(DBNAME, $conn);
Edited Check all passwords
mysql is deprecated you have to use mysqli or pdo. example of mysqli is ...
$hostname= "localhost";
$username = "root";
$password = "";
$dbname = "your_database_name";
$con = new mysqli($hostname, $username, $password,$dbname);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "your_query";
if ($con->query($sql) === TRUE) {
echo "On query result is successfull";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
Hi I am making a login/register database. I am not hosting the database on the same server so how do I make it point to my separate domain? Also if you can tell me what the root is pointing to... Thx
Here are my files:
db.php
<?php
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('register');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Links (my scripts and original scripts): https://docs.google.com/document/d/1u60X_mKd9z548qrh_VjdyTx3hziiZiYPLUa_rJX11mQ/edit?usp=sharing
For starters, DON'T USE mysql_connect()! Use either mysqli or PDO. Second, don't have your application log in to MySQL as root; create an application-specific unprivileged user (with a password!) for this purpose.
You'll need to create your unprivileged user with connect privilege from the web server's IP or DNS address, and instead of connecting to localhost your PHP will need to connect to the DB server's IP or DNS address, like so:
$dsn = "mysql:host=mysqlserver.mynetwork.com;dbname=register";
try
{
$conn = new PDO($dsn, $appuser, $apppasswd);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
You could try the following example
$server = "192.168.1.1"; //server name or IP Address
$username = "root"; //database username
$password = "password"; //database password
$database = "mydatabase"; //database to connect to
$connection = mysqli_connect( $server, $username, $password, $database);
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
else{
//sql statement
}
mysqli_close($connection);
Thanks
Even if I have used LAMPP many times, this time something goes wrong. When I visit the browser(chrome) nothing echos. Here is my code:
index.php
<?php
error_reporting(E_ALL); /*after edit*/
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Do I miss something? The output is nothing. By the way i write my files in
var/www/html/my_pages
and i call it this way: localhost/my_pages. Simple echos are working and php in general is fine. Something goes wrong with my db connection.
Use this code
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
else
{
echo 'Connected successfully';
}
?>
Yes probably, because mysqli_connect() method return the object, not Boolean value.
You can verify the connection with the following code:
if($link->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return false;
}
else{
echo "Connection Successful";
return $link;
}
Why not using PDO.
$dsn = "mysql:host=localhost;dbname=db";
try {
$pdo = new PDO($dsn, "root", "");
} catch (PDOException $ex) {
$ex->getMessage();
}
with PDO you can change anytime you need the Db vendor:
http://php.net/manual/en/book.pdo.php
<?php
echo phpinfo();
?>
Run this file and get all PHP and apache details. Search for mysqli support in it. If it is supported, you should have something like below.
Also check for your root directory
Thanks everyone for the response! I found the problem. Something went wrong and mysqli wasn't enabled in php. That's why i had this error Fatal Error:Call to undefined function mysqli_connect() in /var/www/html/diamond/index.php on line 8 I reinstalled php and problem solved :)
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 have a requirement to connect to two separate databases for a client. One db holds all the standard store data, customers, orders etc, the other is to hold around 41,000 promo codes.
I've looked at PDO to connect to multiple db's as that is the new standard, but i have no idea how to achieve this when we have a configure.php file that contains the db connection info.
configure.php listed below:
define('HTTP_SERVER', 'http://localhost');
define('HTTPS_SERVER', 'https://localhost');
// Use secure webserver for checkout procedure?
define('ENABLE_SSL', 'false');
// NOTE: be sure to leave the trailing '/' at the end of these lines if you make changes!
// * DIR_WS_* = Webserver directories (virtual/URL)
// these paths are relative to top of your webspace ... (ie: under the public_html or httpdocs folder)
define('DIR_WS_CATALOG', '/');
define('DIR_WS_HTTPS_CATALOG', '/');
define('DIR_WS_IMAGES', 'images/');
define('DIR_WS_INCLUDES', 'includes/');
define('DIR_WS_FUNCTIONS', DIR_WS_INCLUDES . 'functions/');
define('DIR_WS_CLASSES', DIR_WS_INCLUDES . 'classes/');
define('DIR_WS_MODULES', DIR_WS_INCLUDES . 'modules/');
define('DIR_WS_LANGUAGES', DIR_WS_INCLUDES . 'languages/');
define('DIR_WS_DOWNLOAD_PUBLIC', DIR_WS_CATALOG . 'pub/');
define('DIR_WS_TEMPLATES', DIR_WS_INCLUDES . 'templates/');
// * DIR_FS_* = Filesystem directories (local/physical)
//the following path is a COMPLETE path to your Zen Cart files. eg: /var/www/vhost/accountname/public_html/store/
define('DIR_FS_CATALOG', '/');
define('DIR_FS_DOWNLOAD', DIR_FS_CATALOG . 'download/');
define('DIR_FS_DOWNLOAD_PUBLIC', DIR_FS_CATALOG . 'pub/');
define('DIR_WS_UPLOADS', DIR_WS_IMAGES . 'uploads/');
define('DIR_FS_UPLOADS', DIR_FS_CATALOG . DIR_WS_UPLOADS);
define('DIR_FS_EMAIL_TEMPLATES', DIR_FS_CATALOG . 'email/');
// define our database connection
define('DB_TYPE', 'mysql');
define('DB_PREFIX', ''); // prefix for database table names -- preferred to be left empty
define('DB_SERVER', 'localhost');
define('DB_SERVER_USERNAME', '');
define('DB_SERVER_PASSWORD', '');
define('DB_DATABASE', '');
// The next 2 "defines" are for SQL cache support.
// For SQL_CACHE_METHOD, you can select from: none, database, or file
// If you choose "file", then you need to set the DIR_FS_SQL_CACHE to a directory where your apache
// or webserver user has write privileges (chmod 666 or 777). We recommend using the "cache" folder inside the Zen Cart folder
// ie: /path/to/your/webspace/public_html/zen/cache -- leave no trailing slash
define('SQL_CACHE_METHOD', 'none');
define('DIR_FS_SQL_CACHE', '/enter/your/path/to/public_html_or_htdocs/and/zencart/here/zen/cache');
Digging through the php files i found this, which appears to be where the connection is created:
// Load queryFactory db classes
require(DIR_FS_CATALOG . DIR_WS_CLASSES . 'db/' .DB_TYPE . '/query_factory.php');
$db = new queryFactory();
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
I looked at using:
try {
$db1 = new PDO('mysql:dbname=databas1;host=127.0.0.1', 'username', 'password');
$db2 = new PDO('mysql:dbname=databas2;host=127.0.0.1', 'username', 'password');
} catch (PDOException $ex) {
echo 'Connection failed: ' . $ex->getMessage();
}
in place of the existing:
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE);
but i'm concerned this means that all the existing queries will need to be changed to cover the $db1, $db2 issue.
Both databases are located on the same server and use identical user/pass combinations.
Any help greatly appreciated.
I tried, and failed with PDO and ended up going with:
$db = new queryFactory();
$db2 = new queryFactory();
$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, USE_PCONNECT, false);
$db2->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE2, USE_PCONNECT, true);
Also had to add the define for DB_DATABASE2 to the configure.php file.
Hope it helps other zen cart users.
Simple really :)