Welcome! Log In Create A New Profile

Advanced

DB connection problem

Posted by fionam 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar DB connection problem
October 23, 2011 06:23PM
I'm having a problem with my db connection include code. It is returning the following error message

Warning: mysql_select_db() expects parameter 1 to be string, resource given in G:\xampp\htdocs\academy\includes\dbconn.inc.php on line 13

Unable to locate the database.


Line 13 is, of course, referring to the second if statement.

DB connection page contains the following code:

Language: PHP
<?php   $conn = mysql_connect('localhost', 'acuser', 'db555#');   if (!$conn)   { $error = 'Unable to connect to the database server.'; include 'error.inc.html.php'; exit(); }   if (!mysql_select_db($conn, 'academy')) { $error = 'Unable to locate the database.'; include 'error.inc.html.php'; exit(); } ?>

The connection to the server is definitely being made because I am not receiving an 'Unable to connect to the database server' error but it's simply not finding the database itself and I have no idea why. And yes, I have checked the name of the database and have also tried using the default username and password i.e. root and no password. I cannot, for the life of me, figure out why it is not finding the database.sad smiley
avatar DB connection problem
October 23, 2011 11:33PM
Assuming 'academy' is the name of your database, change the order of the arguments in line 13 of your code and see if it will work:

Language: PHP
if (!mysql_select_db('academy', $conn))
avatar Re: DB connection problem
October 24, 2011 07:47AM
Hi fionam,

when using MySQL, you don't need to specify a connection resource like:

Language: PHP
if (!mysql_select_db('academy', $conn))

This should work:
Language: PHP
if (!mysql_select_db('academy'))

student no: 77315138
avatar Re: DB connection problem
October 24, 2011 10:50AM
Thanks guys, but I also tried both of those solutions - that's why I'm stumped. Beginning to wonder if it's a Xampp issue .....

Am going to try the 'long' way of doing it directly in the code and see what happens.
Re: DB connection problem
October 27, 2011 09:59PM
<?php

$servername='127.0.0.1'; // Your MySql Server Name or IP address here

$dbIDNumber='root'; // Login user id here

$dbpassword=''; // Login password here(i didnt set a password for me...

$dbname='registration'; // database name



connecttodb($servername,$dbname,$dbIDNumber,$dbpassword); //connection

function connecttodb($servername,$dbname,$dbuser,$dbpassword) //its function

{

global $link;

$link=mysql_connect ("$servername","$dbuser","$dbpassword"winking smiley;

if(!$link){die("Could not connect to MySQL"winking smiley;}

mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());

}





?>
Sorry, only registered users may post in this forum.

Click here to login