|
 |
|
|
07-08-2006, 12:39 PM
|
How do I setup a user?
|
Posts: 626
|
I am trying to setup a user on my PC so that I don't have to keep changing the coding on my page.
Right now, everytime I code a page and I test it on my machine at home, I have username as 'root' but everytime I upload it to my hosted site I have to change the user name...
How do I setup a user (with the same name as my hosted site) on my PC?
|
|
|
|
07-08-2006, 07:30 PM
|
Re: How do I setup a user?
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
what OS ??
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
07-08-2006, 09:30 PM
|
Re: How do I setup a user?
|
Posts: 626
|
Windows 2000
MySQL 5.0.11-Beta
PHP 5.0.5
Apache 2.0.54
|
|
|
|
07-09-2006, 05:49 AM
|
Re: How do I setup a user?
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
Win2kPro or Server without Active Directory
Computer Management -> User and Groups -> New User ...
Win2k server AD
Administrative Tools -> Active Directory Users and Computers -> Users -> New User ...
MySQL
Exactly how depends on what tools you have for administering your DB Server, but generally you add a user with localhost access and then grant whatever rights they need.
or you could just do it the simple way;
use an included file for the DB config and have usernames, passwords etc defined in there and have one for the production server and one for the dev server.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
07-09-2006, 11:03 AM
|
Re: How do I setup a user?
|
Posts: 626
|
I don't think I made myself clear on what I want to do... Right now, the following code is used to access my DB when running PHP pages on my PC:
PHP Code:
$host = "localhost";
$user = "root";
$pw = "password";
$db = "dbname";
$table = "tablename";
mysql_connect($host, $user, $pw);
But I have to go in and change ALL of my pages when I want to upload it to the server because the username I have to use there is in the form of "hostuser_admin".
I am trying to eliminate having to change all my pages when I publish them... Is there a way that I can add "hostuser_admin" to my mysql DB on my PC??
|
|
|
|
07-09-2006, 07:34 PM
|
Re: How do I setup a user?
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
With the magic of require(), why would you need to add login info on each page? Just create a single script that connects to the db straight, from a function, or better yet, build a mysql handeling class.
After that, you would only need to change in one place the login info configuration.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-10-2006, 02:36 PM
|
Re: How do I setup a user?
|
Posts: 626
|
I never though of it... can you give me a coding example of that? Please.
|
|
|
|
07-18-2006, 02:07 AM
|
Re: How do I setup a user?
|
Posts: 111
Name: Jonathan
Location: Arizona, USA
|
Also, I ran into this before. I have my test server running on XP Pro, but my live sites are all on Linux. The domain names are usually like this "domain_user" for username, "domain_db" for database. They way I do it is to create the same database name on my test server and then I don't have to change the login names.
And to what mgraphic was suggesting is this:
Make a file and name it something like connect_db.php
In that file write this:
PHP Code:
<? $host = "localhost"; $user = "root"; $pw = "password"; $db = "dbname"; $table = "tablename";
mysql_connect($host, $user, $pw); ?>
Then at the top of each of your other pages that you need to connect to mysql with, write:
Code:
require_once('connect_db.php'); //This will be your connection to the database.
That way you'll only need to change the username and password once if you don't set up the database on your test server to be the same as your live server.
__________________
Please login or register to view this content. Registration is FREE - Computer Repair over the internet!
|
|
|
|
07-18-2006, 10:52 PM
|
Re: How do I setup a user?
|
Posts: 626
|
I have setup the db as the same name as the live site (ie. domain_db) however, on the live site I have to use (domain_user) instead of 'root'. I don't know how to change the 'root' on my test site.
Can you tell me how to change my test site to 'domain_user' instead of root?
And...
don't you have to do:
$link = mysql_connect(blah);
so that when you do the query you do -> $results = mysql_query($sql, $link); ????
|
|
|
|
07-18-2006, 11:51 PM
|
Re: How do I setup a user?
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
I use an effective mysql class that is very handy to use, and keeps the other scripts simple.
(parts of this class taken from zen-cart)
mySqlClass_config.php
PHP Code:
<?php //# Define database connection define('DB_SERVER', 'localhost'); define('DB_SERVER_USERNAME', 'root'); define('DB_SERVER_PASSWORD', ''); define('DB_DATABASE', 'database_name'); ?>
mySqlClass.inc.php
PHP Code:
<?php class mySqlClass { function mySqlClass() { require('mySqlClass_config.php'); $this->count_queries = 0; $this->total_query_time = 0; } public function connect($host, $user, $password, $database) { $this->database = $database; $this->link = @mysql_connect($host, $user, $password, true); if ($this->link) { if (@mysql_select_db($database, $this->link)) { $this->db_connected = true; return true; } else { $this->set_error(mysql_errno(),mysql_error()); return false; } } else { $this->set_error(mysql_errno(),mysql_error()); return false; } public function close() { @mysql_close($this->link); } private function set_error($err_num, $err_text) { $this->error_number = $err_num; $this->error_text = $err_text; if ($err_num != 1141) { $this->show_error(); die(); } } private function show_error() { echo $this->error_number . ' ' . $this->error_text; } public function Execute($sql) { $time_start = explode(' ', microtime()); $obj = new mySqlResult; if (!$this->db_connected) $this->set_error('0', 'Error: MySQL DB Not Connected'); $db_resource = @mysql_query($sql, $this->link); if (!$db_resource) $this->set_error(@mysql_errno($this->link),@mysql_error($this->link)); $obj->resource = $db_resource; $obj->cursor = 0; if ($obj->RecordCount() > 0) { $obj->EOF = false; $result_array = @mysql_fetch_array($resource); if ($result_array) { while (list($key, $value) = each($result_array)) { if (!ereg('^[0-9]', $key)) { $obj->fields[$key] = $value; } } $obj->EOF = false; } else { $obj->EOF = true; } } else { $obj->EOF = true; } $time_end = explode (' ', microtime()); $query_time = $time_end[1]+$time_end[0]-$time_start[1]-$time_start[0]; $this->total_query_time += $query_time; $this->count_queries++; return($obj); } } public function queryCount() { return $this->count_queries; } public function queryTime() { return $this->total_query_time; } } class mySqlResult { public function MoveNext() { $this->cursor++; if ($this->is_cached) { if ($this->cursor >= sizeof($this->result)) { $this->EOF = true; } else { while(list($key, $value) = each($this->result[$this->cursor])) { $this->fields[$key] = $value; } } } else { $result_array = @mysql_fetch_array($this->resource); if (!$result_array) { $this->EOF = true; } else { while (list($key, $value) = each($result_array)) { if (!ereg('^[0-9]', $key)) { $this->fields[$key] = $value; } } } } } public function RecordCount() { return @mysql_num_rows($this->resource); } } ?>
Any php script using the class
PHP Code:
<?php //### Start MySQL connection require('mySqlClass.inc.php'); $db = new mySqlClass(); $db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE); // Execute sql query... $result = $db->Execute('SELECT * FROM table WHERE field = 1'); while(!$result->EOF) { // You can also use $result->RecordCount() to determine // the number of results returned // // Something like this: // if ($result->RecordCount() > 0) { // while(!$result->EOF) { // Action takes place here // } // } echo $result->fields['field_1']; echo $result->fields['field_2']; echo $result->fields['field_3']; $result->MoveNext(); } // Echo total query time echo $db->queryTime(); // Echo total number of queries echo $db->queryCount(); ?>
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
Last edited by mgraphic; 07-19-2006 at 12:16 AM..
Reason: Typo
|
|
|
|
07-19-2006, 11:36 AM
|
Re: How do I setup a user?
|
Posts: 626
|
thanks mgraphic... Not that I am questioning you but if you could explain something for me. I understand how it is advantageous to use the connect aspect of the class. But, why would it be advantageous to use the !$results->EOF and where is that class coming from as I see you are assigning the results of a query to it but it is coming back as a class.
|
|
|
|
07-19-2006, 01:11 PM
|
Re: How do I setup a user?
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
There is a variable defined in the class $this->EOF which is assigned a boolean of true or false. The var will remain false until there are no more rows returned. So using the while loop will cycle through until $remain->EOF (short for end of file) remains false. Just be sure to iterate to the next row by using $result->MoveNext() or you will experience an infinite loop.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-19-2006, 04:09 PM
|
Re: How do I setup a user?
|
Posts: 170
Name: XpIndia.Com
|
The path is anyway going to be dofferent when you upload your file on a live server.
then / turns into \
You just need to edit one line.
What I used to do was, made a con.asp con.php which contained the
commands for establishing connection with server.
and con1.asp or con1.php for working on pc with apache,php,mysql
This way you can keep making the changes to other files.
No need to change the db con file.
|
|
|
|
07-20-2006, 10:03 AM
|
Re: How do I setup a user?
|
Posts: 317
Name: This Space for Rent
Location: Georgia
|
You all have very valid points, but to answer the original question... You can use the free MySQL administrator from http://dev.mysql.com/downloads/administrator/1.1.html It's really easy to use. Just go to user information (or something like that) and create a new user with the required user name/password
__________________
Please login or register to view this content. Registration is FREE
"I think therefore I am, I think." <!-- George Carlin
|
|
|
|
07-20-2006, 01:27 PM
|
Re: How do I setup a user?
|
Posts: 111
Name: Jonathan
Location: Arizona, USA
|
Quote:
|
Originally Posted by ExpressoDan
You all have very valid points, but to answer the original question... You can use the free MySQL administrator from http://dev.mysql.com/downloads/administrator/1.1.html It's really easy to use. Just go to user information (or something like that) and create a new user with the required user name/password
|
That's true, that was the original question wasn't it.
Another option, since you should have phpmyadmin installed, to set up a new user is to go to http://localhost/phpmyadmin
login as root
Click on Priveledges
Click on Add New User
The Username will be whatever you'd like
Host: localhost
Password: whatever you want.
Then, after you set up your new user, mgraphic has given a connection class that provides for a real good way for connecting to the db in your php pages without having to change username/password info across pages.
__________________
Please login or register to view this content. Registration is FREE - Computer Repair over the internet!
|
|
|
|
07-20-2006, 01:36 PM
|
Re: How do I setup a user?
|
Posts: 626
|
Thanks SOOO much!
|
|
|
|
|
« Reply to How do I setup a user?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|