Jump to content

Another PHP/MySQL Connectivy Problem


Kaizen Maven

Recommended Posts

I have just read this post:  http://www.phpfreaks.com/forums/index.php/topic,95378.0.html, but this seems to apply for someone that has PHP installed on their computer or local machine.

 

I am using a hosting server and my hosting company has PHP 5.0.4 installed.  I am trying to work on creating tables for a database using the book "Beginning PHP, Apache, MySQL Web Development", and for some reason I can not seem to connect to the MySQL DB.

 

Here is my code:

 

<?php
//Connect to MySQL DB.

$connect = mysql_connect("localhost","php","mypasshere") or
die ("Hey loser, this dog anit gonna hurt.  You might want to check your server connection.");

//Create the main database
mysql_create_db("php")
or die(mysql_error());

//Make sure our recently created database is the active one
mysql_select_db("php");

//Create the "movie" table

$movie = "CREATE TABLE movie (
movie_id int(11) NOT NULL auto_increment,
movie_name varchar(255) NOT NULL,
movie_type tinyint(2) NOT NULL default 0,
movie_year int(4) NOT NULL default 0,
movie_leadactor int(11) NOT NULL default 0,
movie_director int(11) NOT NULL default 0,
PRIMARY KEY  (movie_id),
KEY movie_type (movie_type,movie_year)
) TYPE=MyISAM AUTO_INCREMENT=4";

$results = mysql_query($movie)
	or die (mysql_error());

//Create "movietype" table
$movietype = "CREATE TABLE movietype (
movietype_id int(11) NOT NULL auto_increment,
movietype_label varchar(100) NOT NULL,
PRIMARY KEY  (movietype_id)
)  TYPE=MyISAM AUTO_INCREMENT=9";


$results = mysql_query($movietype)
	or die(mysql_error());


//Create "people" table

$people = "CREATE TABLE people (
people_id int(11) NOT NULL auto_increment,
people_fullname varchar(255) NOT NULL,
people_isactor tinyint(1) NOT NULL default 0,
people_isdirector tinyint(1) NOT NULL default 0,
PRIMARY KEY  (people_id)
)  TYPE=MyISAM AUTO_INCREMENT=7";


$results = mysql_query($people)
	or die (mysql_error());


echo "Movie Database Successfully Created!";


?>

 

I do have PHPMyAdmin, and I know I could do it through there, but I am trying to follow the book.  I created the db php (cbxwebde_php) through my cPanel, and then created the username php (cbxwebde_php), then my password.  Should I have done this first?

 

 

 

I am getting this message:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'php'@'localhost' (using password: YES) in /home/cbxwebde/public_html/php/createmovie.php on line 15

Hey loser, this dog anit gonna hurt. You might want to check your server connection.

 

Thanks for any help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/50442-another-phpmysql-connectivy-problem/
Share on other sites

Try this may be helpful

 

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

 

$sql = 'CREATE DATABASE my_db';

if (mysql_query($sql, $link)) {

    echo "Database my_db created successfully\n";

} else {

    echo 'Error creating database: ' . mysql_error() . "\n";

}

?>

Ok, so I just said the hell with it and I'll do i from PHPMyAdmin.  Now I'm familiar with setting up tables and inputting the fields with their settings.

 

But the book says to type this:

 

$movie = "CREATE TABLE movie (
movie_id int(11) NOT NULL auto_increment,
movie_name varchar(255) NOT NULL,
movie_type tinyint(2) NOT NULL default 0,
movie_year int(4) NOT NULL default 0,
movie_leadactor int(11) NOT NULL default 0,
movie_director int(11) NOT NULL default 0,
PRIMARY KEY  (movie_id),
KEY movie_type (movie_type,movie_year)
) TYPE=MyISAM AUTO_INCREMENT=4";

 

I know how to convert the movie_id, name, type, leadactor, and director into fields.  But what about the PRIMARY KEY & KEY.  In PHPMyAdmin what do I make the KEY?  I see icons for Primary (with Key), also for Index, Unquie, and Fulltext.

 

But do I make the movie_id field and the movie_type and year the primary key?  Or just one?

U can keep multiple primary keys in table.

 

Ok, so basically PRIMARY KEY & KEY are the same thing, and I can set them both in the PHPmyAdmin

 

I just tried to make the movie_id, movie_type, and movie_year the primary key and my PHPAdmin said

 

Error

Multiple primary key defined

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.