开发者

MySQL database connection problem [closed]

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 3 years ago.

Improve this question

I'm trying to connect to a database f开发者_开发问答rom my script, but keep getting the error: "Cannot select the database"

here's the config.php file

<?php
$hostname = 'localhost'; //it's localhost in all all cases
$db_username = 'root@localhost';
$db_password = '';
$dbname = 'function';
$link = mysql_connect($hostname, $db_username, $db_password) or die("Cannot connect to the database");
mysql_select_db($dbname) or die("Cannot select the database");
?>

I'm using XAMPP on Windows, have created a database called function, with no password, running locally. I still can't figure out why it's not selecting the database.


The username should just be "root", as that should have permissions for all databases :)


This should do the trick, but do yourself a huge favor and look into PDO

<?php
$hostname = 'localhost'; //it's localhost in all all cases
$db_username = 'root'; // NOT @localhost
$db_password = null; // NOT '';
$dbname = 'function';
$link = mysql_connect($hostname, $db_username, $db_password) or die("Cannot connect to the database");
mysql_select_db($dbname) or die("Cannot select the database");
?>

addition: If that doesn't do the trick, you could try "127.0.0.1" instead of "localhost". also inspect mysql_error($link) to get an actually useful message

second addition: removed @localhost as per Jesper Rasmussen's answer (can't believe i missed that.)


Use this code below. It will create a pdo, mysqli obj, or an mysqli pro. Depending on what the variebles are set to. EX: $type = 1 // create a mysqli obj... $type = 2 // create a mysqli pro... $type = 3 // create a pdo connection. Then change the 4 vars on lines 8, 9, 10, and 11.

link.php

<?php
/*
link.php
Created By Nicholas English
*/
$link = null;
$connection = null;
$servername = "";
$username = "";
$dbname = "";
$pass = "";
$mysqli = null;
$pdo = null;
$obj = null;
$pr = null;
$type = 3;
if ($type === 1) {
$mysqli = true;
$pdo = false;
$obj = true;
$pr = false;
} else {
if ($type === 2) {
$mysqli = true;
$pdo = false;
$obj = false;
$pr = true;
} else {
if ($type === 3) {
$mysqli = false;
$pdo = true;
$obj = false;
$pr = false;
} else {
$mysqli = null;
$pdo = null;
$obj = null;
$pr = null;
}
}
}
if ($mysqli === true && $obj === true) {
$link = new mysqli($servername, $username, $pass, $dbname);
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$connection = true;
} else {
if ($mysqli === true && $pr === true) {
$link = mysqli_connect($servername, $username, $pass, $dbname);
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$connection = true;
} else {
if ($pdo === true && $mysqli === false) {
try {
$link = new PDO("mysql:host=$servername;dbname=$dbname", $username, $pass);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection = true;
}
catch(PDOException $e)
{
$connection = null;
echo "Connection failed: " . $e->getMessage();
}
} else {
$link = null;
$connection = null;
}
}
}
if ($connection == null && $link == null) {
$error = 1;
}
?>

Then the code below goes at the top of you're pages.

index.php

<?php
include "link.php";
if ($error === 1) {
$error = ""; // tell user that there is a configuration error
}
?>

So some where on your page you can display error by using this code

<h1><?php echo "".$error.""; ?></h1>

You should switch to mysqli or pdo, for a much secure connection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜