Page 1 of 1

$result not passing correctly between page and function

Posted: Wed Jun 02, 2004 11:23 am
by lostboy
Hi All,

I have this quickie test page to try where I pass a sql string to a function that connects and runs the query....it doesn't seem to pass the results back...I can't access the data....

here is the code

Code: Select all

<? 
//test to dl case info 
require("dbconn.php"); 

$case_id = "CA8008250"; 

$sql="select * from case_info where case_number='$case_id'"; 
echo $sql."<br>"; 
remote_conn($sql); 

if ($result) 
    
   while ($row = mysql_fetch_array($result)){ 
    
       $data = implode(",",$row); 
    
       $data = str_replace(",","','",$data); 
    
       echo "data is: ".$data."<br>"; 
   } 
   mysql_free_result($result); 
   mysql_close($result); 
?>


and the function

Code: Select all

function remote_conn($strsql) 
{ 
    $username = "root"; 
    $pwd = "******"; 
    $host = "192.168.100.10"; 
    $dbname = "test_db"; 
     
    if (!($conn=mysql_connect($host, $username, $pwd)))  { 
        printf("error connecting to DB by user = $username and pwd=$pwd"); 
        exit; 
    } 
    $db=mysql_select_db($dbname,$conn) or die("Unable to connect to remote database"); 
     
    $result=mysql_query($strsql) or die ("Can't complete remote query because ".mysql_error()); 
     
    return $result; 
}



TIA

Posted: Wed Jun 02, 2004 11:32 am
by markl999
remote_conn($sql); needs to be $result = remote_conn($sql); otherwise the returned result from the function 'gets lost', i.e you have to assign it to something, in this case $result

Posted: Wed Jun 02, 2004 1:27 pm
by lostboy
D'oh! Note to self: Take brain out of fridge and bring to work...