PDA

View Full Version : why cant i connect to database (PHP)


Tjobbe
November 17th, 2004, 15:47
I have a database (mysql) and I'm trying to connect to it.

I'm using the exact format of a VTC training video, and I have used it before and it has worked, I've only created my own DB and can't understand why its not connecting:

my query.php file: Correct I think!
<?
$db = mysql_connect("localhost");
mysql_select_db("directory", $db);
$query = "SELECT * FROM directory";
$result = mysql_query($query);
$table = mysql_field_table ($result, 0);
echo $table;
?>

my database file:
(directory.sql)
# phpMyAdmin SQL Dump
# version 2.5.6
# http://www.phpmyadmin.net
#
# Host: localhost
# Generation Time: Nov 17, 2004 at 03:46 PM
# Server version: 4.0.18
# PHP Version: 4.3.6
#
# Database : `directory`
#

# --------------------------------------------------------

#
# Table structure for table `businesstype`
#

CREATE TABLE `businesstype` (
`businesstypeID` tinyint(3) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL default '',
PRIMARY KEY (`businesstypeID`)
) TYPE=MyISAM AUTO_INCREMENT=17 ;

#
# Dumping data for table `businesstype`
#

INSERT INTO `businesstype` VALUES (1, 'Web Design');
INSERT INTO `businesstype` VALUES (2, 'Packaging');
INSERT INTO `businesstype` VALUES (3, 'Art & Craft');
INSERT INTO `businesstype` VALUES (4, 'Accountancy');
INSERT INTO `businesstype` VALUES (5, 'Cleaning');
INSERT INTO `businesstype` VALUES (6, 'Manufacturing');
INSERT INTO `businesstype` VALUES (7, 'Engineering');
INSERT INTO `businesstype` VALUES (8, 'Transport');
INSERT INTO `businesstype` VALUES (9, 'Computing');
INSERT INTO `businesstype` VALUES (10, 'Managerial');
INSERT INTO `businesstype` VALUES (11, 'Recruitment');
INSERT INTO `businesstype` VALUES (12, 'Building');
INSERT INTO `businesstype` VALUES (13, 'Printing');
INSERT INTO `businesstype` VALUES (14, 'Acting');
INSERT INTO `businesstype` VALUES (15, 'Music, Film & T.V');
INSERT INTO `businesstype` VALUES (16, 'Banking');

# --------------------------------------------------------

#
# Table structure for table `busservices`
#

CREATE TABLE `busservices` (
`companyid` tinyint(3) unsigned NOT NULL default '0',
`businesstypeID` tinyint(3) unsigned NOT NULL default '0'
) TYPE=MyISAM;

#
# Dumping data for table `busservices`
#

INSERT INTO `busservices` VALUES (1, 1);
INSERT INTO `busservices` VALUES (1, 2);
INSERT INTO `busservices` VALUES (1, 3);
INSERT INTO `busservices` VALUES (1, 5);
INSERT INTO `busservices` VALUES (1, 7);
INSERT INTO `busservices` VALUES (1, 11);
INSERT INTO `busservices` VALUES (2, 1);
INSERT INTO `busservices` VALUES (2, 3);
INSERT INTO `busservices` VALUES (2, 5);
INSERT INTO `busservices` VALUES (2, 6);
INSERT INTO `busservices` VALUES (2, 7);
INSERT INTO `busservices` VALUES (2, 8);
INSERT INTO `busservices` VALUES (2, 9);
INSERT INTO `busservices` VALUES (2, 11);
INSERT INTO `busservices` VALUES (3, 3);
INSERT INTO `busservices` VALUES (3, 4);
INSERT INTO `busservices` VALUES (3, 7);
INSERT INTO `busservices` VALUES (3, 9);
INSERT INTO `busservices` VALUES (3, 13);
INSERT INTO `busservices` VALUES (3, 14);
INSERT INTO `busservices` VALUES (4, 1);
INSERT INTO `busservices` VALUES (4, 2);
INSERT INTO `busservices` VALUES (4, 5);
INSERT INTO `busservices` VALUES (4, 6);
INSERT INTO `busservices` VALUES (4, 3);
INSERT INTO `busservices` VALUES (4, 8);
INSERT INTO `busservices` VALUES (5, 3);
INSERT INTO `busservices` VALUES (5, 4);
INSERT INTO `busservices` VALUES (5, 7);
INSERT INTO `busservices` VALUES (5, 9);
INSERT INTO `busservices` VALUES (5, 10);
INSERT INTO `busservices` VALUES (5, 14);
INSERT INTO `busservices` VALUES (6, 4);
INSERT INTO `busservices` VALUES (6, 10);
INSERT INTO `busservices` VALUES (2, 15);
INSERT INTO `busservices` VALUES (2, 10);
INSERT INTO `busservices` VALUES (7, 1);
INSERT INTO `busservices` VALUES (7, 2);
INSERT INTO `busservices` VALUES (7, 6);
INSERT INTO `busservices` VALUES (8, 1);
INSERT INTO `busservices` VALUES (8, 3);
INSERT INTO `busservices` VALUES (8, 7);
INSERT INTO `busservices` VALUES (8, 2);
INSERT INTO `busservices` VALUES (8, 12);
INSERT INTO `busservices` VALUES (9, 6);
INSERT INTO `busservices` VALUES (9, 15);
INSERT INTO `busservices` VALUES (10, 10);
INSERT INTO `busservices` VALUES (10, 5);
INSERT INTO `busservices` VALUES (10, 11);
INSERT INTO `busservices` VALUES (10, 3);
INSERT INTO `busservices` VALUES (10, 9);
INSERT INTO `busservices` VALUES (3, 16);
INSERT INTO `busservices` VALUES (5, 16);

# --------------------------------------------------------

#
# Table structure for table `companydetails`
#

CREATE TABLE `companydetails` (
`companyid` tinyint(3) unsigned NOT NULL auto_increment,
`busname` varchar(40) NOT NULL default '',
`address1` varchar(40) default NULL,
`address2` varchar(40) default NULL,
`town` varchar(40) default NULL,
`county` varchar(40) NOT NULL default '',
`postcode` varchar(40) NOT NULL default '',
`phone` varchar(18) NOT NULL default '',
`fax` varchar(18) NOT NULL default '',
`email` varchar(60) NOT NULL default '',
`website` varchar(60) NOT NULL default '',
`comments` varchar(80) NOT NULL default '',
PRIMARY KEY (`companyid`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;

#
# Dumping data for table `companydetails`
#

INSERT INTO `companydetails` VALUES (1, 'Bleep Bleep Records', '14 Industrial Ave', 'Main Road', 'Troughton', '', 'PL25 3AR', '65465465', '64351231', 'bleepbleep@freeemail.com', '', 'Interesting UV-glow clubwear also on sale');
INSERT INTO `companydetails` VALUES (2, 'Hi Fly Sounds', '15 Outersdale Road', '', 'Imblendon', '', '', '64231615', '', 'hiflier@hotmail.com', '', '');
INSERT INTO `companydetails` VALUES (3, 'Tuff Tune', '13 Yard St', '', 'Troughton', '', '', '64321654', '64351684', 'tufftune@yahoo.com', '', 'Specialist t\'ing ..');
INSERT INTO `companydetails` VALUES (4, 'Deeper Beat Records', '53 Blempton Ave', '', 'Durringvale', '', '', '64231654', '64615644', 'info@deeperbeat.com.au', 'www.deeperbeat.com.au', '');
INSERT INTO `companydetails` VALUES (5, 'Riddim Records', '37 Dorset Road', '', 'Hellington', '', '', '64234516', '', 'manager@riddim.com.au', 'www.riddim.com.au', 'Rudeboy riddims');
INSERT INTO `companydetails` VALUES (6, 'Phat Bass Music', '91 Bevelled Road', 'Long Park', 'Sirdale', '', '', '64321684', '64351568', 'phatbass@mymail.com', '', 'Loud flavas');
INSERT INTO `companydetails` VALUES (7, 'Minimalism', '24 Crane St', '', 'Hellington', '', '', '64321684', '64321684', 'us@minimalism.com.au', 'www.minimalism.com.au', 'Seriously stripped-down techno');
INSERT INTO `companydetails` VALUES (8, 'Explosive Records', '71 Bang St', '', 'Troughton', '', '', '64321684', '64651684', 'bang@hotmail.com', '', '');
INSERT INTO `companydetails` VALUES (9, 'Fab Sounds', '21 Enders Lane', '', 'Imbledon', '', '', '64231654', '64654651', 'tony@fabsound.com.au', 'www.fabsound.com.au', 'Gorgeous toons daarling!');
INSERT INTO `companydetails` VALUES (10, 'Freaky Beat', '85 Ungoliant Ave', 'Wyrd Hills', 'Trippiton', '', '', '64321654', '', 'str@oddside.com.au', '', 'Very, very odd.');




I'm really really confused! :o

Tjobbe
November 17th, 2004, 15:49
oh and its supposed to be a database of companies and what services they offer, as a self-project I want to create a serchable PHP/MySql database of companies, according to location and business type, ie, computers, manufacturers etc etc. so "Computers and Ohio" would return X results.

sonicgroup
November 17th, 2004, 21:31
You need to specify a username and password for your mysql_connect function:

[syntax:81851e948e="php"]$db = mysql_connect('localhost', 'username', 'password') or die('Could not connect to the MySQL server. '.mysql_error());[/syntax:81851e948e]

Tjobbe
November 17th, 2004, 22:32
but its on a local pc - i.e i have it installed on my pc at home, ive never had to use a username and pw before, why is it now playing up?

sonicgroup
November 18th, 2004, 13:37
Well, if you don't have a username/password set, then that may not be the issue.

Do this though: Take the second half of what I posted (the part that begins with "or die(") and paste it into your code. That will give the error message the MySQL reports. Post that here and we'll go from there.

Tjobbe
November 21st, 2004, 13:23
heres the output after I did what you said:


Warning: mysql_field_table(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\php\companies\query.php on line 6

and this is line 6 from that file:

$table = mysql_field_table ($result, 0);

sonicgroup
November 21st, 2004, 13:43
Well, that error generally means that the query didn't run for one reason or another.

I just looked up the manual page for mysql_field_table() and the use of it in this case looks right (though I can't imagine a situation where you'd need this).

In short, you've got me on that one. One thing to try though, specify the fieldname you want in your query (SELECT x FROM table), and specify it in the mysql_field_table line as the offset.

Tjobbe
November 21st, 2004, 13:46
I'll try it now, thanks very much for the reply.

Tjobbe
November 21st, 2004, 14:50
I changed a few things, and got somewhere, slowly.

<?
$db = mysql_connect("localhost");
mysql_select_db("directory", $db);
$query = "SELECT * FROM directory";
$result = mysql_query($query);
$table = mysql_field_table ($result, 0);
echo $table;
?>

this code, as far as I understand should return EVERYTHIN, from the "directory" database.

when I run it, I get:

Warning: mysql_field_table(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\php\companies\query.php on line 6



if i change this line:
$query = "SELECT * FROM directory";



to:
$query = "SELECT * FROM companydetails";



I get:
companydetails displayed on the page.


why wont it display everything from the db onto the page as asked?

sonicgroup
November 21st, 2004, 22:37
Because the mysql_field_table displays the table name that the field specified by the offset belongs to.

If you want to display everying in that table, try this: (also, for future reference, it's a good idea to throw debugging in there as well, as I have done)

[syntax:dfcd9a9b84="php"]
<?
$db = mysql_connect('localhost') or die('Could not connect to the MySQL Server: '.mysql_error());
mysql_select_db('directory', $db) or die('Could not select the database: '.mysql_error());
$result = mysql_query('SELECT * FROM directory') or die('Could not execute query: '.mysql_error());
while ($row = mysql_fetch_array($result)) {
print_r($row);
}
?>
[/syntax:dfcd9a9b84]