RE: Don't know why query works this way

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here's some functions I use in my development which save on connect and
query calls for me.

//General Purpose Utilities.
//db_connect connects to the database server and selects the proper
database.
//Arguments: None
//Returns: Nothing


function db_connect() {

	mysql_pconnect("localhost","username","password");
	mysql_select_db("Database");
	
}


//db_query queries the database server and returns the results.
//Arguments: SQL query string
//Returns: Query results

function db_query($query) {

	return mysql_query($query);
	
}


//db_fetch returns the next set of results from a db_query.
//Arguments: db_query results variable
//Returns: Next set of query results as an array or 0 if no further
results are present


function db_fetch($results) {

	return mysql_fetch_array($results);
	
}


//db_numrows returns the number of rows selected from a query.
//Arguments: query result
//Returns: number of rows in the query result

function db_numrows($result) {

	return mysql_num_rows($result);
	
}


//Always connect to the database!

db_connect();


Keep these in an include file. The db_connect(); goes at the bottom and
keep the connection open during the routines.


To call db_query you would use it like:

$someQuery = db_query("SELECT * from someTable ....");
 

to get the results out of the query you would call db_fetch and use it
like:


$someResult = db_fetch($someQuery);

If you needed to return an array you would use it with a while or for
statement like:

While ($someResult = db_fetch($someQuery)) {

}

If you needed to get the number of rows from the query you'd use like:

numRows = db_numrows($someQuery);

or

if (db_numrows($someQuery) > ...)


These are just some thing I use to make my life easier.

Hope it helps




-----Original Message-----
From: jeffrey_n_Dyke@Keane.com [mailto:jeffrey_n_Dyke@Keane.com] 
Sent: August 6, 2003 7:49 AM
To: cortesm@fortleboeuf.net
Cc: php-db@lists.php.net
Subject: Re:  Don't know why query works this way


there are many ways to approach this.
one is, select the db before any of this activity with mysql_select_db
("dbname") then you can run as many queries against this database as you
want.
Until you call that function again with another db name, mysql with
_always_ run against that database.

hth
jd




 

                      Michael Cortes

                      <cortesm@fortlebo        To:
php-db@lists.php.net

                      euf.net>                 cc:

                                               Subject:   Don't
know why query works this way                                   
                      08/05/2003 11:35

                      PM

                      Please respond to

                      cortesm

 

 





I have been writing my queries like this.......

$link = mysql_connect ($host, $username, $password);
$query = "insert into $table ('joe', 15)";
if ( mysql_db_query($dbname, $query, $link))          {     }

But then I was reading that I should use mysl_query instead of
mysql_db_query.
But I was having a problem with where do i put the $dbname.

When I used:

mysql_query ($dbname, $query, $link)

I got an error message saying i had too many arguments.  So I changed it
to:

mysql_query ($query, $link)

I then got an error message saying it didn't know what database I was
connected to.  I finally got around the problem by including the
database
name in the query, like so:

$query = "insert into $dbname.$table ('joe', 15)";

I shouldn't have to say, but this is a simplified example.  I didn't
want
to
include all the fields, tables, values that the true example has.  The
question is, am I doing this right?  Do I have no choice but to retype
the
$dbname all over the place if I want to use "mysql_query"?

Thank in advance for any help.


--

Michael Cortes
Fort LeBoeuf School District
34 East Ninth Street
PO Box 810
Waterford PA 16441-0810
814.796.4795

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux