Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource


erzulie

Recommended Posts

Hello

 

I tried to move the bold section of the below code into a function getWeights() located in a m_weight.php file (I want to implement MVC pattern)

 

<?php

 

$get_weights_q = mysql_query ("

SELECT * FROM users_weight

WHERE weight_uid = '$user_id'

ORDER BY weight_date DESC

 

", toctoc() ) or die(mysql_error() );

 

while ($weight = mysql_fetch_array($get_weights_q))

 

{ ?>

<li>

<?php echo date( " d M-y H:i (l) " , $weight['weight_date']). ' : ' . $weight['weight'] .

' <a href="'.$_SERVER['SCRIPT_NAME'].'?del=' . $weight['weight_id'] . '">Changer</a> ' ;

}

?>

</li>

</ul>

----------------

 

Here is the function:

 

function getWeights($user_id)

{

 

$get_weights_q = mysql_query ("

SELECT * FROM users_weight

WHERE weight_uid = '$user_id'

ORDER BY weight_date DESC

 

", toctoc() ) or die(mysql_error());

}

 

 

The result is :

Notice: Undefined variable: get_weights_q in H:\home\user\documents\DEV-H\EasyPHP-5.3.8.1\www\Food-Tribe\v_weight.php on line 44

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in H:\home\user\documents\DEV-H\EasyPHP-5.3.8.1\www\Food-Tribe\v_weight.php on line 44


  •  

 

If I let the code just above the While, in procedural mode it works fine...

 

Any idea?

Link to comment
Share on other sites

You need to read up on variable scope.

 

Aie Sorry that is pure esoterism for me. Could you elaborate?

 

"print_r($get_weights_q) " within the function, gives me Resource id #7

and out of the function above the while:Undefined variable...

 

 

Edited by erzulie
Link to comment
Share on other sites

Did you google PHP variable scope? If it's too complicated what part of it don't you understand?

 

Thanks for clarifying :)

I did now and I globalized the variable $get_weight_q

 

Here's the new code:

 

 

function getWeights($user_id)

{

global $get_weights_q ;

 

$get_weights_q = mysql_query ("

SELECT * FROM users_weight

WHERE weight_uid = '$user_id'

ORDER BY weight_date DESC

 

", toctoc() ) or die(mysql_error());

 

}

 

It works! thank you so much .; I'm in love :)

Take care

Link to comment
Share on other sites

*smh*.

 

You shouldn't use global. You should either put all of the logic that uses that variable in the function, (all your sql stuff) or return the variable you need.

Damn ... Here I was thinking i'd hit the holy grail...

So you got me thinking.Clearly I didn't know how to return an sql array.

 

Here's the new code for the function:

function getWeights($user_id)

{

//global $get_weights_q ;

 

 

$get_weights_q = mysql_query ("

SELECT * FROM users_weight

WHERE weight_uid = '$user_id'

ORDER BY weight_date DESC

 

", toctoc() ) or die(mysql_error());

 

return $get_weights_q ;

}

 

Bye bye global, I simply return the sql_query and call it from the controller file (c_file.php)

 

$get_weights = getWeights($_SESSION['id']);

 

then while it in the view_file:

while ($weight = mysql_fetch_array($get_weights))

 

It now works. You are food for thought, thank you :)

Edited by erzulie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.