Unstoppable Domains

Connecting to db..running a query

Spaceship Spaceship
Watch
Impact
19
Hey
i am reading an ebook on php and mysql..and here is how they teach to connect to a db:
PHP:
 $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:
mysql_connect("localhost", "joeuser", "somepass");
but which one is better? does it matter?
this is how they tell to run a query:
PHP:
 $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;
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
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.
 
0
•••
does it make it less efficient tho ?
and y would "echo $conn; " give me an error?
 
0
•••
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.
 
0
•••
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 :$
 
0
•••
PHP:
<?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.
 
0
•••
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?
 
0
•••
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.
 
0
•••
okie got it:D thanks
 
0
•••
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:
		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
 
0
•••
Bump
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back