Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to a database ERROR after switching from mySQL to mySQLi 1

Status
Not open for further replies.

evil1966

MIS
Dec 2, 2013
57
0
0
US
I have a PHP connection to a database working:

Code:
	$username="me";
	$host="myHost";
	$password="password";
	$database="myDatabase";

	$con = mysql_connect($HOST,$USER,$PASSWORD,$DATABASE);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
  	$sql = "SELECT * FROM donors WHERE lastname LIKE '$letter%' ORDER BY lastname, firstname";
    $result = mysql_query($sql) or die(mysql_error());

I have this page,psl-config.phpdb_connect.php

Code:
<?php
/**
 * These are the database login details
 */  
define("HOST", "myHost");     // The host you want to connect to.
define("USER", "me");    // The database username. 
define("PASSWORD", "*****");    // The database password. 
define("DATABASE", "myDatabase");    // The database name.

I also have this db-connect.php page.
Code:
<?php

include_once '/home/.../psl-config.php';   // Needed because functions.php is not included

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ($mysqli->connect_error) {
    header("Location: http//[URL unfurl="true"]www.mediqwest.com/error.php?err=Unable[/URL] to connect to MySQL");
    exit();
}

So back on this donorList.php page I put this:
Code:
<?php
include_once '/home/mediqw5/public_html/php/db_connect.php';
include_once '/home/mediqw5/public_html/php/functions.php';
include_once '/home/mediqw5/public_html/php/psl-config.php';

sec_session_start();
?>
and I removed these lines:
Code:
	$username="me";
	$host="myHost";
	$password="password";
	$database="myDatabase";

I'm getting:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'me'@'myHost' (using password: NO) in /home/.../donorList.php on line 112
Unable to select database

I'm trying to switch things here from mySQL to mySQLi, but the examples I've found are different from one another and I'm not sure were exactly the error lies here.

Thanks!
 
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'me'@'myHost' (using password: NO) in /home/.../donorList.php on line 112

The error suggests that the password is empty.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
The error suggests that the password is empty.

Hi Chris,

Yes, though I don't understand why. I don't know if it has to do with the inclucde_once statement where that info is located or the $con=mysql_connect() line. The include_once is working for the login.php page to get to this donorList.php page.

Thanks.
 
The error suggests that somewhere you are still using a regular mysql connection as opposed to a mysql connection. And that its attempting to connect to your DB without a password. This would cause the access denied error if the server is not set to allow passwordless connections.

The first thing to do is to go to line 112 and see what its doing there.

If you have changed your connections to mysqli in the db_connect file then why is there still a mysql connection being made further down in the donorList.php file?


Also, since your constants are being defined after your mysqli connection, you will get notices about undefined constants when your mysqli connection attempts to run.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi Vacunita,

The first thing to do is to go to line 112 and see what its doing there.
If you have changed your connections to mysqli in the db_connect file then why is there still a mysql connection being made further down in the donorList.php file?

So you are saying that I should just have the mySQLi querry and results? That's the confusing part. I don't know the proper syntax for the query and the result. The examples I've found online are all different and I've gotten none of them to work yet. I'll keep working at it.

Also, since your constants are being defined after your mysqli connection, you will get notices about undefined constants when your mysqli connection attempts to run.

Thanks. Good to know.
 
I got this to work:

Code:
$result = $mysqli->query("SELECT * FROM donors WHERE lastname LIKE '$letter%' ORDER BY lastname, firstname");

Now I have to change this code
Code:
 <?php 
    
    while($row = mysql_fetch_array($result))
  {  
  
 ?>
 
Okay it's all working now. I'm returning data from the database correctly.

Thanks!
 
Glad you sorted it out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Glad you sorted it out.

Thanks! Say do I have to close the db connection after returning data? Since I removed the pages' connection string $con= I get the error in the close statement. Once place said it isn't necessary to have one.

Thanks again.
 
You can't close what you don't have. If you did not store the connection handler in a variable, then you can't actually close it. However, no, its not 100% necessary to explicitly close the connection. It will auto-close when the script terminates.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top