DATABASE CONNECTION NOT CONNECTING SCRIPT NOT CONNECTING !

I have been trying connect my database with this script but fails each time no erro displaying even when i have error check in place don’t know what ''m doing wrong help please [code]<?php
$servername = “LOCALHOST”;
$username = “ADMIN”;
$password = “PASS”;

$link = mysqli_connect($servername, $username, $password);

if (!$link) {
die('Could not connect: ’ . mysqli_error($link));
}

echo ‘Connected successfully’;
mysqli_close($link);
?>[/code]

I normally work in PDO, but I think you’re missing the database name that you should be using:

[php]$link = mysqli_connect(“127.0.0.1”, “my_user”, “my_password”, “my_db”); // Notice “my_db” in this example:[/php]

also make you are have error reporting turn on, put the following at the top in your configuration file or top of the page.
[php]/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(-1);[/php]

thanks Strider64 applying your code now

Yes, I think Strider64 is correct.

Also, Kelvinejimogu, this is from the other post where you were trying to use the deprecated MySQL code.
I was wondering if you are using a server or your local machine. Do you already have the database set up
on the server or local system?

This is how I do it on one site that uses MySQLi…
[php]
// Connecting to your database
$db_connect = mysqli_connect($hostname, $username, $password, $dbname);

//  Check if connection was actually made
if (mysqli_connect_errno()) { echo "Failed to connect to the user database system!  Error: " . mysqli_connect_error(); }

[/php]
Note that it uses the connect_errno not the mysqli error… It seems to make a difference on some servers.
This version checks for errors and if any displays it.

And, for the query, it is basically like this:
[php]
$query = “SELECT * FROM users WHERE id =” . $_POST[“user_id”];
$results = mysqli_query($db_connect, $query) or echo “Error selecting users from database!
” . mysqli_error($db_connect);
[/php]
Note that I am using $db_connect not $link, but, the rest is easy to understand…

Sorry guys, I was typing whey you were, so my response is redundant…

i have edited my script as you suggested but still, but it gives me the : “name and password do not match” in line 19 of the admin index page the name and password are correct i know but still, below is the admin index page code:

[code]<?php
error_reporting(1);
include(“config.php”);
$name=$_REQUEST[‘t1’];
$pass=$_REQUEST[‘p1’];

if($_REQUEST[‘sub’])
{
$sel=mysql_query(“select name,pass from details where name=’$name’”);
$arr=mysql_fetch_array($sel);
if($arr[‘name’]==$name and $arr[‘pass’]==$pass)
{
session_start();
$_SESSION[‘eid’]=$name;
header(“location:home.php”);
}
else
{
$er=“name and password do not match”;
}
}
?>

[/code]

This is my server version details below

Apache ver. 2.2.19 (Unix)
PHP version 5.2.*
MySQL ver. 5.1

Kelvine, if you use the connection string as we both told you using the MYSQLi_CONNECT function, then,
you can not use MYSQL functions. You would need to change your code:
$sel=mysql_query(“select name,pass from details where name=’$name’”);
$arr=mysql_fetch_array($sel);
if($arr[‘name’]==$name and $arr[‘pass’]==$pass)
to something more like:
$sel=mysqli_query(“select name,pass from details where name=’$name’”, $db_connect);
$arr=mysqli_fetch_array($sel);
if($arr[‘name’]==$name and $arr[‘pass’]==$pass)

BUT, you do not need to select the name if you already have it. You also need to check if there were any
records returned from the query. If you are doing a WHERE name=“ernie” and there is no entry for that
user’s name, then you would not be able to fetch the array of data as it does not exist. You would need
to check the MYSQLi_num_rows to see if you got a result and if not, state that the name does not exist.
You can’t fetch data if it does not exist and THEN test it.

Here is a tutorial on how to select data from a database using MySQLi or PDO. It will help you…
http://www.w3schools.com/php/php_mysql_intro.asp
Note: Press the green “Next-Chapter” link at the bottom-right of each page to walk thru the tutorial…

Thanks guys i edited my script to use MYSQLI and i was able to connect to my database

Great! I will mark this one solved.

Sponsor our Newsletter | Privacy Policy | Terms of Service