garethhall Posted May 29, 2009 Share Posted May 29, 2009 Hello please help me out I am getting this error " mysql_query(): supplied argument is not a valid MySQL-Link resource in" I don't understand why my query is good I have tested it in phpMyAdmin and runs fine so why am I getting this error // Protect Variables from SQL injection function checkVars($value){ // Stripslashes if (get_magic_quotes_gpc()){ $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)){ $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; }; // set Variables $clientID = checkVars($_GET['clientID']); mysql_select_db($database_admin , $admin); function photoCount($saleState, $clientID){ $SQL_countPhotos = "SELECT COUNT(photos.photoID), photos.saleState, photos.clientId FROM photos WHERE photos.saleState = '$saleState' AND photos.clientId = '$clientID'"; $rs_countPhotos= mysql_query($SQL_countPhotos, $admin); $row_countPhotos = mysql_fetch_assoc($rs_countPhotos); //echo $SQL_countPhotos; echo $row_countPhotos['COUNT(photos.photoID)']; } Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/ Share on other sites More sharing options...
justAnoob Posted May 29, 2009 Share Posted May 29, 2009 This is easier on the eyes. <?php // Protect Variables from SQL injection function checkVars($value){ // Stripslashes if (get_magic_quotes_gpc()){ $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)){ $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; }; // set Variables $clientID = checkVars($_GET['clientID']); mysql_select_db($database_admin , $admin); function photoCount($saleState, $clientID){ $SQL_countPhotos = "SELECT COUNT(photos.photoID), photos.saleState, photos.clientId FROM photos WHERE photos.saleState = '$saleState' AND photos.clientId = '$clientID'"; $rs_countPhotos= mysql_query($SQL_countPhotos, $admin); $row_countPhotos = mysql_fetch_assoc($rs_countPhotos); //echo $SQL_countPhotos; echo $row_countPhotos['COUNT(photos.photoID)']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/#findComment-844644 Share on other sites More sharing options...
Ken2k7 Posted May 29, 2009 Share Posted May 29, 2009 PHP function's variable scope is local. Any variables not defined within the functions, either as parameters or newly created variables, are considered to be undefined. So $admin is not defined. PHP is not the same as JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/#findComment-844646 Share on other sites More sharing options...
MadTechie Posted May 29, 2009 Share Posted May 29, 2009 Okay.. i'll let you work it out, but first think about these two questions #1 what is $admin. and #2 What does the error say is invalid ? and i assume this works when they are not in their own functions idea.. global $admin; EDIT: awww Ken2k7!! lol Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/#findComment-844647 Share on other sites More sharing options...
garethhall Posted May 29, 2009 Author Share Posted May 29, 2009 Ahh so simple Thank you so much I just passed the $admin into the function and now it works. Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/#findComment-844652 Share on other sites More sharing options...
MadTechie Posted May 29, 2009 Share Posted May 29, 2009 Solved ? please click topic solved! Quote Link to comment https://forums.phpfreaks.com/topic/160094-solved-mysql-error-i-dont-understand-why/#findComment-844746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.