[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 01-06-2007, 08:00 PM   #1 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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:
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, 08:05 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 08:16 PM   #3 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


does it make it less efficient tho ?
and y would "echo $conn; " give me an error?
unknowngiver is offline  
Old 01-06-2007, 08:46 PM   #4 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 08:52 PM   #5 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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, 09:01 PM   #6 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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
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, 09:03 PM   #7 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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, 09:45 PM   #8 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

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, 10:03 PM   #9 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


okie got it thanks
unknowngiver is offline  
Old 03-02-2007, 06:40 PM   #10 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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?
P.S: this is in a class..if it makes any difference
unknowngiver is offline  
Old 03-03-2007, 03:08 PM   #11 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


Bump
unknowngiver is offline  
Closed Thread


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

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

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


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 11:46 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85