NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page connecting to db..running a query

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 01-06-2007, 09:00 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes

connecting to db..running a query


Hey
i am reading an ebook on php and mysql..and here is how they teach to connect to a db:
????: NamePros.com http://www.namepros.com/programming/277976-connecting-to-db-running-a-query.html
PHP Code:
 $conn mysql_connect("localhost""joeuser""somepass");

 echo 
$conn
from reading other tutorials and looking at scripts..i learned that you dont need to define the mysql conection to a variable and echo it later on..u can just do:
PHP Code:
mysql_connect("localhost""joeuser""somepass"); 
but which one is better? does it matter?
this is how they tell to run a query:
PHP Code:
 $sql "CREATE TABLE testTable (id int not null primary key auto_increment,

 testField varchar (75))"
;

 
// execute the SQL statement

 
$result mysql_query($sql$conn);

// echo the result identifier

 
echo $result
unknowngiver is offline  
Old 01-06-2007, 09:05 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
If you are using multiple databases, you need to use a variable (such as $conn.) Otherwise, I don't think you need it.

echo $conn; would give you an error, too.

The ", $conn" in your third code doesn't need to be there. I don't know about $sql, because I'm not good at doing CREATE's in queries, but as far as I can tell, it's right.
Dan is offline  
Old 01-06-2007, 09:16 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
does it make it less efficient tho ?
and y would "echo $conn; " give me an error?
unknowngiver is offline  
Old 01-06-2007, 09:46 PM   #4 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
You'll get "Resource link identifier" or something if you try to do "echo $conn;". It's not really an error, but it's not really not an error.

I don't think using the MySQL link variable into the query does anything to do unless you are accessing multiple databases. I wouldn't worry about using it in queries, however I still would give the mysql_connect() a variable.
Dan is offline  
Old 01-06-2007, 09:52 PM THREAD STARTER               #5 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
Quote:
however I still would give the mysql_connect() a variable.
if u give it a variable..how would u execute it? by using the echo function? i am a lil confused here sorry :$
unknowngiver is offline  
Old 01-06-2007, 10:01 PM   #6 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
PHP Code:
<?php

$handle 
mysql_connect("localhost""username""password");
$db mysql_select_db("database");

$query  "SELECT * FROM internets WHERE site = 'namepros'"
$result mysql_query($query);
$row    mysql_fetch_array($result); // Lets just get one row from NamePros
????: NamePros.com http://www.namepros.com/showthread.php?t=277976
echo $row['something'];

?>
Of course that's just made up queries and stuff.. but you get the point.
Dan is offline  
Old 01-06-2007, 10:03 PM THREAD STARTER               #7 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
and that would work? wow yai lol

1 question...what if u were using multiple databases...how would u tell it 2 use the one u want it 2 use?
unknowngiver is offline  
Old 01-06-2007, 10:45 PM   #8 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
That's what "$conn" is for.

You would do $conn = mysql_connect... then something like $conn2 = mysql_connect...

To query the first database, you'd use mysql_query($query, $conn) and to query the second, you'd just use $conn2.
Dan is offline  
Old 01-06-2007, 11:03 PM THREAD STARTER               #9 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
okie got it thanks
unknowngiver is offline  
Old 03-02-2007, 07:40 PM THREAD STARTER               #10 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
Hey
I need this now..and i cant figure it out
the $conn stores the user information..not the database
so how do u tell it 2 use a specific database?
right now i m working a script where users can make and edit databases..and 1 is the Main database that they are using almost all the time..and then there are Databases that they make/edit/delete etc..
right now to connect and run a query i have
PHP Code:
        function MYSQLDB()
        {
            
$this->connection mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die (mysql_error());
            
mysql_select_db(DB_NAME,$this->connection)or die(mysql_error());
        }
        
        function 
query($query)
        {
            
$success mysql_query($query,$this->connection);
            return 
$success;
        } 
so now..how would i make it that lets say..they want to edit a database called "db1" how would i make it do it?
????: NamePros.com http://www.namepros.com/showthread.php?t=277976
P.S: this is in a class..if it makes any difference
unknowngiver is offline  
Old 03-03-2007, 04:08 PM THREAD STARTER               #11 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
Bump
unknowngiver is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 08:12 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger