problems with my function and query...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Xplosion84
Forum Newbie
Posts: 9
Joined: Wed Jun 16, 2004 1:30 pm

problems with my function and query...

Post by Xplosion84 »

function:

Code: Select all

<?php
 function calldb($query_type) {
  		$dbname = 'maxforcepc'; 
        $link = mysql_connect ('..........', '........', '..........');
        $result = mysql_query ($dbname, $query_type, $link) or die ('Queryproblem');   
        if (mysql_num_rows($result) >= 1) {   
            while ($row = mysql_fetch_array($result)) {   
                echo '<a href="' . $row['Url'] . '">' . $row['Links'] . '</a><br />';    
            }  
        } else {   
            echo 'The query could not be executed!<br />';     
        }   
        mysql_free_result($result);   
} 
?>
calling the function:

Code: Select all

<?php
calldb("SELECT * FROM nav_links WHERE Cat_Links=". $_GET['CatID']);
?>
I'm getting a parse error but my hosting company shut off php errors

feyd | removed host/user/password from db connect
Xplosion84
Forum Newbie
Posts: 9
Joined: Wed Jun 16, 2004 1:30 pm

Post by Xplosion84 »

oh crap whooops thanks feyd :-)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

Code: Select all

<?php 
function calldb($query_type) { 
        $dbname = 'maxforcepc'; 
        $link = mysql_connect ('..........', '........', '..........');
        mysql_select_db($dbname, $link);
        $result = mysql_query ($query_type, $link) or die ('Queryproblem');    
        if (mysql_num_rows($result) >= 1) {    
            while ($row = mysql_fetch_array($result)) {    
                echo '<a href="' . $row['Url'] . '">' . $row['Links'] . '</a><br />';    
            }  
        } else {    
            echo 'The query could not be executed!<br />';      
        }    
        mysql_free_result($result);    
} 
?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Should the function call not be:

Code: Select all

calldb("SELECT * FROM nav_links WHERE Cat_Links='". $_GET['CatID']."'");
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Re: problems with my function and query...

Post by penguinboy »

Xplosion84 wrote:function:

Code: Select all

<?php
$dbname = 'maxforcepc'; 
$link = mysql_connect ('..........', '........', '..........');
$result = mysql_query ($dbname, $query_type, $link) or die ('Queryproblem');
?>
http://us4.php.net/mysql_query
resource mysql_query ( string query [, resource link_identifier])
Unless it's an undocumented feature;
I don't think you can include the db in the query string.

Try:

Code: Select all

<?php
$dbname = 'maxforcepc'; 
$link = mysql_connect ('..........', '........', '..........');
mysql_select_db($dbname,$link);
$result = mysql_query ($query_type, $link) or die (mysql_error());
?>
Xplosion84
Forum Newbie
Posts: 9
Joined: Wed Jun 16, 2004 1:30 pm

Post by Xplosion84 »

liljester wrote:

Code: Select all

<?php 
function calldb($query_type) { 
        $dbname = 'maxforcepc'; 
        $link = mysql_connect ('..........', '........', '..........');
        mysql_select_db($dbname, $link);
        $result = mysql_query ($query_type, $link) or die ('Queryproblem');    
        if (mysql_num_rows($result) >= 1) {    
            while ($row = mysql_fetch_array($result)) {    
                echo '<a href="' . $row['Url'] . '">' . $row['Links'] . '</a><br />';    
            }  
        } else {    
            echo 'The query could not be executed!<br />';      
        }    
        mysql_free_result($result);    
} 
?>
Great!! That worked, but the odd thing is it works when it wants too. my syntax to call that function is:

Code: Select all

<?php
calldb("SELECT * FROM nav_links WHERE Cat_Links=". $_GET['CatID']);
?>
It worked when i fixed my coding then i went back to it and i'm getting a parse error
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

Try

Code: Select all

$result = mysql_query ($query_type, $link) or die(mysql_error());
Instead of

Code: Select all

$result = mysql_query ($query_type, $link) or die('Queryproblem');
Xplosion84
Forum Newbie
Posts: 9
Joined: Wed Jun 16, 2004 1:30 pm

Post by Xplosion84 »

penguinboy wrote:Try

Code: Select all

$result = mysql_query ($query_type, $link) or die(mysql_error());
Instead of

Code: Select all

$result = mysql_query ($query_type, $link) or die('Queryproblem');
I'm still getting a blank page, but when i comment out the syntax that calls the function my page layout shows...
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

<?php
function conn(){
    $link = mysql_connect ('..........', '........', '..........');
    mysql_select_db("maxforcepc",$link);
}

function calldb($query_type) {
    $result = mysql_query ($query_type, $link) or die ('Queryproblem');   
    if (mysql_num_rows($result) >= 1) {
       while ($row = mysql_fetch_array($result)) {   
           echo '<a href="' . $row['Url'] . '">' . $row['Links'] . '</a><br />';   
       } 
    } else {   
       echo 'The query could not be executed!<br />';     
    }   
    mysql_free_result($result);
}
?>
Then call it with

Code: Select all

<?php
conn();
calldb("sql");
?>
This will keep the SQL connection alive incase you're making other sql queries.

Also, is this output going to be used on multiple pages? Or just the page you're developing now?
If it's only for one page, it'd probably be easier to include the code from calldb instead of putting it in a function.
Xplosion84
Forum Newbie
Posts: 9
Joined: Wed Jun 16, 2004 1:30 pm

Post by Xplosion84 »

LiLpunkSkateR wrote:

Code: Select all

<?php
function conn(){
    $link = mysql_connect ('..........', '........', '..........');
    mysql_select_db("maxforcepc",$link);
}

function calldb($query_type) {
    $result = mysql_query ($query_type, $link) or die ('Queryproblem');   
    if (mysql_num_rows($result) >= 1) {
       while ($row = mysql_fetch_array($result)) {   
           echo '<a href="' . $row['Url'] . '">' . $row['Links'] . '</a><br />';   
       } 
    } else {   
       echo 'The query could not be executed!<br />';     
    }   
    mysql_free_result($result);
}
?>
Then call it with

Code: Select all

<?php
conn();
calldb("sql");
?>
This will keep the SQL connection alive incase you're making other sql queries.

Also, is this output going to be used on multiple pages? Or just the page you're developing now?
If it's only for one page, it'd probably be easier to include the code from calldb instead of putting it in a function.
I'm using one page but using includes to include other pages on the index.php...
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

http://us3.php.net/manual/en/function.e ... orting.php

Try setting this at the top of the script.

Code: Select all

error_reporting(E_ALL);
Post Reply