Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Warning: mysql_fetch_assoc()

New Here ,
Jan 03, 2008 Jan 03, 2008
Hi
i make recordset and make a function that iterate through it like this

function getname($id)
{
if($id==0){
echo "...;
return ;
}
while ($row_rs_father = mysql_fetch_assoc($rs_father)){ // this line generates the warning !!!
if($row_rs_father['id'] == $id)
echo $row_rs_father['name'];
return;
}
}


and the $id is passed corerectly but the while line generates error why?
when debug i found that $row_rs_father is null any idea?
and sql statement is right because those have $id=0 are printed

thanks in advance.
TOPICS
Server side applications
3.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 03, 2008 Jan 03, 2008
macnux wrote:
> and the $id is passed corerectly but the whle line generates error why?
> when debug i found that $row_rs_father is null any idea?

Yes, the problem is one of variable scope. Variables inside a function
are treated as though in a black box. This is to prevent clashes with
any variables of the same name elsewhere in the script. The $rs_father
inside your function is completely separate from the $rs_father that
contains the result resource from MySQL.

If this sounds difficult to understand, try this sample script:

$number = 2;

function doubleIt($number) {
return $number * 2;
}

$result = doubleIt(2);

echo $number; // displays 2
echo $result; // displays 4

The $number variable inside the function is completely separate. In
technical terms, it's in a different scope.

For the recordset result to be available inside your function, you need
to pass it as an argument to the function like this:

function getname($id, $recordset)

Inside the function, use $recordset instead of $rs_father.

HOWEVER...

It's not as simple as that. The way that Dreamweaver builds a recordset
extracts the first row into a variable called $row_recordsetName. That's
why repeat regions use a do... while loop, rather than a straighforward
while loop.

So, passing the recordset resource to your function will omit the first
row in the result set. You either need to delete the line at the end of
the Dreamweaver recordset code that extracts the first row, or to pass
the first row into the function and use a do... while loop like Dreamweaver.

Another point to remember is that you cannot loop through a recordset
more than once without using mysql_data_seek to reset it like this:

mysql_data_seek($recordsetName, 0);

It's not clear to me why you want to create a function. Functions
encapsulate routines that will be reused in multiple situations. You're
attempting to use a specific recordset. Unless you need to repeat this
process many times, it would be more sensible to dispense with the
function, and just use an if... else conditional structure and do...
while loop.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 03, 2008 Jan 03, 2008
thanks for reply

i put the function on separate file and on this file i generate a recordset to use it in the function like

/*
code generated from dreamweaver for recordset called $rs_names
*/

function getfathername($id)
{
if($id==0){
echo "...";
return ;
}
mysql_data_seek($GLOBALS['$rs_names'], 0); //warning here
while ($res = mysql_fetch_assoc($GLOBALS['$rs_names'])){ //warning here
if($res["id"] == $id){
echo $res["name"];
return;
}
}
}

and as you see i use $GLOBALS[] for the recordset since it is in the same file and i get now 2 errors !! on lines commented

thanks in advance.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 03, 2008 Jan 03, 2008
macnux wrote:
> mysql_data_seek($GLOBALS['$rs_names'], 0); //warning here

Try $GLOBALS['rs_names'].

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 04, 2008 Jan 04, 2008
thanks very much

one other question
on my signature i wrote i try to apply ajax webservice and gives me error could you help me

thanks in advance.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 04, 2008 Jan 04, 2008
LATEST
macnux wrote:
> on my signature i wrote i try to apply ajax webservice and gives me error could you help me

Perhaps you have edited your post in the forum's web interface. Such
changes are not propagated to the news server, so I cannot see your
question.

However, since it's not directly related to the original question. I
suggest you start a separate thread, and ask the question again. I'm not
as familiar with Ajax as with PHP, so I won't necessarily be able to
help you.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines