SQL with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

SQL with PHP

Post by Johnm »

Is there a good reason you guys use the query functions (like mysql_querry()) or did I learn to do this in an in-efficient way?

Here is a standard query in my eyes... can it be better?

Code: Select all

<?php

$sql="select ".$basename." 
         from bases 
         where job='".$session_infoї'order']."' 
             and loc_code=$session_infoї'loc_code'])."'";
      $qry=ifx_query($sql,$dbid);
      $tmp=ifx_fetch_row($qry,"next");
      ifx_free_result($qry);


?>

Thanks,


Direwolf
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

How is your way any different? Or are you just referring to building the query up outside of the function call? (I do that, too).

You're still using a _query function....
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

here's how I do all my queries... using a function file that I include on my database pages

FUNCTION FILE

Code: Select all

<?php
//***** MAKE DATABASE CONNECTION
$dbhost = 'localhost'; 
$dbuname = 'user'; 
$dbpass = 'password'; 
$dbname = 'dev'; 



function dbconnect() 
{ 
 global $dbhost, $dbuname, $dbpass, $dbname; 
 mysql_connect($dbhost, $dbuname, $dbpass); 
 @mysql_select_db($dbname) or die ("Unable to select database"); 
} 


//***** QUERY MYSQL RESULTS
function query_db($query) 
{ 
 dbconnect(); 
 return @mysql_query($query); 
} 

?>
Then in the page I want to do a query I do

Code: Select all

<?php
require_once("functionfile.php");

$result = query_db("sql select statement here");
?>
so all I need is one line and it saves alot of typing
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Yes, that is what I mean, building outside the function. I guess I did not write it like I was thinking it.

Direwolf
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

I include my dabase connect too.

Direwolf
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I wrote a class that does all the functions I need with db, so I just include that. It's saves me loads and loads of typing. :D
Post Reply