| View previous topic :: View next topic |
| Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 3:26 pm Post subject: mysql_connect question |
|
|
Ok I understand what this function does. But I have a question about it. I understand MySQL is a data base, but it's the parameters I don't get. Like if I'm running a website of my computer that has user accounts or something and I'm storing it in a MySQL database does MySQL let you specify what the name, the user, and pass should be during installation? And what if there's a data base with more than one name (or does that not happen)? By the way, when do I execute this code...
| Code: |
<?php
//I know I don't have any error handling so don't tell me
$con = mysql_connect("localhost", "user", "pass");
mysql_query("CREATE DATABASE my_db", $con);
$sql = "CREATE TABLE accounts
(
accountsID int NOT NULL AUTO_INCREMENT,
username varchar(30),
password varchar(16)
)";
mysql_query($sql, $con);
mysql_close($con);
?>
|
Do I just one it once from my computer then edit it out of the file that references it? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Thu Jul 10, 2008 3:35 pm Post subject: |
|
|
Your question is VERY VERY confusing...
Are you running a server on your computer? or what are you doing...
That script is used to connect to a MySQL database... You need it in ever PHP script.. that is why i like to make 1 file that just has that in it. and put in
include('connect.php');
Every time it is included it will connect... then usually you need to run a query and a way to fetch the query then you can easily to whatever you want with it.
Hope that helps
If you are running your own server DONT DO IT... to much hassle... Just go to
x10hosting.com
and get free hosting... then you dont have to worry about databases and administration and all that tough stuff. _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 3:50 pm Post subject: |
|
|
Well, I just plan on hosting a small website from my computer. And I was gonna install apache, and mysql and php and stuff. But my question was that in the code:
| Code: |
$con = mysql_connect("localhost", "user", "pass");
|
How do I know what to put for the servername, username, and password. When I install MySQL does it let me specify that, so I know? And I was also asking when to execute the part that creates the database and table. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Thu Jul 10, 2008 4:17 pm Post subject: |
|
|
1) localhost is what you will use most of the time... unless you are using a remote MySQL database ( no )
2) USually you get to define what the username and password is... im not sure, read the MySQL documentation, it has nothing to do with PHP
3)Check out my new guide on MySQL + PHP on my tutorial page! _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 5:39 pm Post subject: |
|
|
If localhost is used most of the time than how do you know which server to connect to. And what is the difference between a remote MySQL database and a normal (or so I assume) one? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Thu Jul 10, 2008 5:45 pm Post subject: |
|
|
if the file is on localhost and the mysql server is also on localhost, the name of the serverhost in the php has to be localhost!
Only time you would change it is if the MySQL database is on a different server ( this usually never happens, it is basically a computer devoted to just MySQL... )
Dont worry about it... its localhost.
Check out my new tut _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 6:21 pm Post subject: |
|
|
Oh, I get it now! Localhost is kind of like a Private IP address. Say you have two computers on different networks that both have the IP 192.168.1.106. Because they're on different networks it's a private IP, so they can both have the IP address 192.168.1.106. Localhost is the same right? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Localhost I post too much
Reputation: 0
Joined: 28 Apr 2007 Posts: 3402
|
Posted: Thu Jul 10, 2008 7:31 pm Post subject: |
|
|
192.168.1.x : usually thats what the "private" ip of your computer looks like... Computer A on your router would probably have something like 192.168.1.101 or something like that, Computer B with 192.168.1.102... No two computers on your router have the same private IP...
If you type in localhost on Computer A you wont go to the same localhost as computer B... Localhost = private IP...
Dont worry your head about it, seriously. _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 8:15 pm Post subject: |
|
|
No I'm saying if you have two computers on different networks they can have the samse IP address because they have a private one (i.e 192.168.1.106). But yeah, I get it now. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jul 10, 2008 9:22 pm Post subject: |
|
|
localhost / 127.0.0.1 is standard for loopback which will connect the computer to itself. If you are using an SQL database that is on your system, just use 'localhost' in anything that requires a host.
| Quote: | | Ok I understand what this function does. But I have a question about it. I understand MySQL is a data base, but it's the parameters I don't get. Like if I'm running a website of my computer that has user accounts or something and I'm storing it in a MySQL database does MySQL let you specify what the name, the user, and pass should be during installation? And what if there's a data base with more than one name (or does that not happen)? By the way, when do I execute this code... |
MySQL asks for a username / password for root when you install it. If you didn't see the prompt for it, or don't remember the password you can reconfigure it. In the start menu should be a folder for MySQL, and inside that should be a service configuration icon. Use that to reconfigure the service.
| Quote: | | Do I just one it once from my computer then edit it out of the file that references it? |
Any page that requires information to or from the database must have a valid connection. You can create a simple include to connect for you so instead of rewriting the connection code in any file that needs a connection, you can simply include the php file that will do the work for you. (Be sure to cleanup the connection that is created.) _________________
- Retired. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 10:20 pm Post subject: |
|
|
Nono not the connect part, but this part.
| Code: |
mysql_query("CREATE DATABASE my_db", $con);
$sql = "CREATE TABLE accounts
(
accountsID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(accountsID),
username varchar(30),
password varchar(16)
)";
mysql_query($sql, $con);
|
Because I only want this to happen once. So should I put it in a file and execute it and then just delete it (the file and any reference to it)? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 10, 2008 10:24 pm Post subject: |
|
|
| You can do that manually in the MySQL console window, no need for PHP. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 10, 2008 10:26 pm Post subject: |
|
|
REALLY? YAY! _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Fri Jul 11, 2008 6:42 am Post subject: |
|
|
When you create a table, that should be done either once via a single SQL connection (without PHP) or use a control panel like phpMyAdmin. You can use PHP to do it if you want but you don't need to. During runtime there's really no reason to constantly be creating tables. _________________
- Retired. |
|
| Back to top |
|
 |
|