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?

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

 

 

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

*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 :)

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.