The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > PHP


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-29-2007, 09:02 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon13 PHP and mySQL Create Table Please Help!

Here's the current code that I have:

PHP Code:
$link mysql_connect($host,$username,$password) or die(mysql_error());
$query 'CREATE TABLE mycms ('.
         
'id INT NOT NULL AUTO_INCREMENT'.
         
'username VARCHAR(10) NOT NULL, '.
         
'password VARCHAR(10) NOT NULL, '
         
'PRIMARY KEY (id))';
         
$result mysql_query($query); 
Is there anything wrong with the code that I have? There's no error that PHP gives me, it seems that's all right, but when I double check on the localhost database, it doesn't have the table that I specified "mycms"

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-29-2007, 10:14 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Try using the following:
PHP Code:
$result mysql_query($query) or die(mysql_error()); 
Note that mysql_error() is only for your benefit. You don't want that to actually be shown to users (and potential script kiddies and others with the intent to breach your security).

The "or die(mysql_error())" part simply says if mysql_query($query) returns FALSE (means there was an error), output whatever mysql_error() returns and then stop processing the page after that. Also, try this for your query:
PHP Code:
$query 'CREATE TABLE mycms ('.
         
'id INT NOT NULL AUTO_INCREMENT KEY'.
         
'username VARCHAR(10) NOT NULL, '.
         
'password VARCHAR(10) NOT NULL)'

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-29-2007)
  #3 (permalink)  
Old 07-29-2007, 10:31 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Note that mysql_error() is only for your benefit. You don't want that to actually be shown to users (and potential script kiddies and others with the intent to breach your security).
Thanx a lot for this rpgfan3233!
but it seems not working... probably I should give the full source code:

admin/index.php
PHP Code:
<?php
$username 
'username';         // mySQL username
$password 'password';     // mySQL password
$host 'localhost';    // mySQL host

$link mysql_connect($host,$username,$password) or die(mysql_error());
$query 'CREATE TABLE mycms ('.
         
'id INT NOT NULL AUTO_INCREMENT KEY'.
         
'username VARCHAR(10) NOT NULL, '.
         
'password VARCHAR(10) NOT NULL)'
         
$result mysql_query($query);
?>
I also tried to use:
PHP Code:
$host 'localhost:3306'

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-29-2007, 11:09 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Did you try adding the die(mysql_error()) part to the line with $result like you have it on the line with $link ? What does it say if you tried that?

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-29-2007, 11:45 PM
molotov molotov is offline
Novice
Join Date: Jul 2007
Posts: 37
iTrader: (0)
molotov is on a distinguished road
Um, you're not selecting the database to use. Add this after the connection, but before your SQL:

PHP Code:
$db 'your_db';
mysql_select_db($db); 
Like this:

PHP Code:
$username 'username';         // mySQL username
$password 'password';     // mySQL password
$host 'localhost';    // mySQL host

// this is new
$db 'your_db';        // mySQL db name

$link mysql_connect($host,$username,$password) or die(mysql_error());

// this is also new
mysql_select_db($db);

$query 'CREATE TABLE mycms ('.
         
'id INT NOT NULL AUTO_INCREMENT KEY'.
         
'username VARCHAR(10) NOT NULL, '.
         
'password VARCHAR(10) NOT NULL)'
         
$result mysql_query($query); 
Edit: Also, I didn't look through the rest of your mySQL, but once you have the DB selected, your mysql_error() output should be more telling if you still have errors.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by molotov : 07-29-2007 at 11:48 PM.
Reply With Quote
  #6 (permalink)  
Old 07-29-2007, 11:53 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
I just knew that you can SELECT a database, is that part of the SQL query? Because from what I know of, you need to have the table first before you can SELECT a table well, also called schema..?

PHP Code:
mysql_select_db($db); 
I'm still confused on what should I put as the name of the database? I don't have the table yet... nothing to be SELECT ed.. So, I need to create one

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-29-2007 at 11:56 PM.
Reply With Quote
  #7 (permalink)  
Old 07-30-2007, 01:04 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 416
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
OK. Here's how it goes, HelloWorld:

1) You have users.
2) You have databases.
3) You have tables.

1 user can have access to 0 or more databases. Each database can have 0 or more tables.

You first connect to MySQL using your user. Since the user can access a number of databases, you must then tell MySQL which database you want to use (this user must have access to that database). After you have connected and indicated the database you can then perform operations on the selected database (such as creating a table).

This type of setup has a nice advantage - users can have limited privileges on a database to protect the database from undesired changes by that user (root, for example, can do whatever it wants, but you don't want any ole user to be able to have that same power).

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-30-2007, 08:12 AM
molotov molotov is offline
Novice
Join Date: Jul 2007
Posts: 37
iTrader: (0)
molotov is on a distinguished road
Let me try to epand on what TeraTask explained.

Ok, you have a server oh which your website is kept and served to the internet. Your server has what are called dameon processes (or depending on your setup, 'services') that help it act like a web server. One of these dameons is the MySQL server. The MySQL dameon operates on the entire server, for all of it's users that have the correct privileges or are in the correct group. Now as you know, MySQL deals with databases - but a single MySQL dameon can handle LOTS of databases. You have to tell it which one to use! This is where mysql_select_db() comes in; it has nothing to do with your actual SQL statements except that it tells the MySQL daemon on which database to perform the statements.

Ok, so you've got a server, with a MySQL daemon running and now you have your databases, when you connect to a database in PHP, you have to tell it the server to which you want to connect, the user* you are connecting as, as password for that user, and a database on the MySQL server that you want to use. Once you connect to the server and MySQL daemon and tell it which database to use, you can start sending it SQL statements and they will be executed on your database, and not someone else's.


Any questions?


*side note: You don't need to worry about this, but just for your information, there are two different 'types' or users here: OS users, and MySQL users. Presumably, your host has assigned you to the MySQL group, or given you the appropriate permissions. Once your OS-level user has permission to access the MySQL daemon, MySQL further requires authentication on a database-per-database level.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 07-30-2007, 11:11 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon13

Quote:
You first connect to MySQL using your user.
I got the first part when you say connect to MySQL, alright, that one is with the username and password at the first time I setup MySQL.

Quote:
Since the user can access a number of databases, you must then tell MySQL which database you want to use (this user must have access to that database). After you have connected and indicated the database you can then perform operations on the selected database (such as creating a table).
I don't really get this, can you give me some example? Are you talking about schema? Ok, so $hostname is the database (the first step that told me), and then we have to choose which schema to use right? So, there's no way we can build this? Well, actually I can just run query on my SQL saying CREATE some_table and it will build the schema (which is probably the database that you're talking about).... And usually I use these tables by using the SELECT query as explained by molotov

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-30-2007, 12:58 PM
molotov molotov is offline
Novice
Join Date: Jul 2007
Posts: 37
iTrader: (0)
molotov is on a distinguished road
First of all, you have to have created a database in which to put a table. If you want to think of it in a hierarchy:

Code:
Internet
V
servers
V
databases
V
tables
V
fields
V
rows
You're skipping the 'databases' section and trying to go right to tables.

I drew up an AWESOME graphic for you



If you're testing locally, you're just viewing your HOST file and skipping the internet step, and instead of a lot of servers, you only have one.

Is that ANY clearer?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to molotov For This Useful Post:
HelloWorld (07-30-2007), TeraTask (07-30-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:14 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50