Jump to content

Script works fine, but not in functions


jumpenjuhosaphat

Recommended Posts

I wrote this code and included it and it worked fine.  When I decided to make it a function, it stopped working.  The $password variable comes up empty, where the $user variable isn't.  They are both in the same file, which is included in the script that calls this function.

Also, when I insert the password, I get another error, stating that the argument passed to mysql_fetch_array isn't valid.  I didn't change anything in the code when I moved it from the included file to the functions, I even double and triple checked. 

Is there a trick to using queries and certain variables within a function?

[code]function category_list($childid)
  {
   
    $select=='
<select name="category">
<option value=""></option>
';

    $con=mysql_connect("localhost",$user,"$password") or die("category_list function connect error ".mysql_error());
    mysql_select_db($database, $con);

        $query=sprintf("SELECT * FROM category WHERE childid=%d ORDER BY catname",$childid);
        $result=mysql_query($query);



        while($row=mysql_fetch_array($result))
          {
            $select.='
  <option value="'.$row['catname'].'">'.$row['catname'].'</option>
';
          }
        $select.='
</select>
';

    return $select;
  }[/code]
Link to comment
https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/
Share on other sites

That's sort of what I figured.  But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?

Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function?
I use this to create my dropdowns you are welcome to use modify
[code]<?php
function create_dropdown($sname, $table, $id_field, $namefield, $where){
$select = "<select name=$sname>";
$select .= "<option value="">--Please Select--</option>";
$sql = "SELECT $id_field, $namefield FROM $table WHERE $where ORDER BY $namefield ASC";
$res = mysql_query($sql) or die (mysql_error());
  while($r = mysql_fetch_assoc($res)){
  $select .= "<option value=".$r[$id_field].">".$r[$namefield]."</option>";
  }
$select .= "</select>";
return $select;
}

echo create_dropdown("selectname", "tablename", "id_field", "namefield", "field = 'something'")
?>[/code]

Add what you like but this will let you select data from the table with 1 where clause.

Ray
[quote author=jumpenjuhosaphat link=topic=120030.msg492066#msg492066 date=1167229172]
That's sort of what I figured.  But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?

Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function?
[/quote]

Since you didn't show us the script that calls this routine, it's hard to guess the answers to your questions.

Post the calling script.

Ken
[quote author=jumpenjuhosaphat link=topic=120030.msg492066#msg492066 date=1167229172]
That's sort of what I figured.  But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?

Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function?
[/quote]For the function to work correctly as you have it, you need to use something like this in the function:
[code=php:0]
$user = $GLOBALS['user'];
$password = $GLOBALS['password'];
[/code]

MySQL doesn't know where your query is in your code, so it shouldn't matter if it's in a function or not. If it does, there's a problem with your code, and you should echo your querys to see what the difference is.
A simple example, test it - then remove the global part and see what happends:
[code]

<?php

$hello = "Hello";
$message = "function calling!";

function Greetings($name)
{
  global $hello,$message;
  echo $hello." ".$name." ,".$message;
}


Greetings("jumpenjuhosaphat");

?>

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.