ukweb Posted November 21, 2007 Share Posted November 21, 2007 Hi I get the error: Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/26403/domains/click.sjwright.co.uk/html/click2.php on line 112 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/26403/domains/click.sjwright.co.uk/html/click2.php on line 114 on this page: http://click.sjwright.co.uk/click2.php ...and I really can't see why! The page is one that is included into others and centralises functions into one file. All the other fun ctions I've created but the sitepages function doesnt and it brings the above error, and I cannot see why. I changed the username and password in the file specified above and sure enough I got the error saying the suername and password were incorrect, so thats working properly. The full code is below. <?php // connections $hostname_click_sql = "internal-db.s26403.gridserver.com"; $database_click_sql = "db26403_click_apps"; $username_click_sql = "db26403_public"; $password_click_sql = "publicaccess"; $click_sql = mysql_pconnect($hostname_click_sql, $username_click_sql, $password_click_sql) or trigger_error(mysql_error(),E_USER_ERROR); $hostname_rental_click_sql = $hostname_click_sql; $database_rental_click_sql = "db26403_rentals"; $username_rental_click_sql = $username_click_sql; $password_rental_click_sql = $password_click_sql; $rental_click_sql = mysql_pconnect($hostname_rental_click_sql, $username_rental_click_sql, $password_rental_click_sql) or trigger_error(mysql_error(),E_USER_ERROR); // Functions function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } function insertsql($dbselect, $dbuser, $table, $cols, $values, $no_response) { if ((!isset($table)) || (!isset($cols)) || (!isset($values))) { $msg = $msg.'» SQL input parameters incorrect \n'; } else { $insertSQL = sprintf("INSERT INTO ".$table." (".$cols.") VALUES (".$values.") "); mysql_select_db($dbselect, $dbuser); if (mysql_query($insertSQL, $dbuser) or die(mysql_error())) { if (!($no_response == "true")) { $msg = $msg.'» Data entered into table ".$table." successfully \n'; } } else { die('» SQL Encountered some problems: '.mysql_error()); } } } function resizer($source, $output, $r_width, $quality, $no_response) { if ((!isset($source)) || (!isset($output)) || (!isset($rwidth))) { $msg = $msg."» Resizer input parameters incorrect \n"; } else { list($src_width, $src_height) = getimagesize($source); $ratio = ($r_width / $src_width) * 100; $r_height = round(($ratio * $src_height) / 100); $resized = imagecreatetruecolor($r_width, $r_height); $original = imagecreatefromjpeg($source); if(imagecopyresampled($resized, $original, 0, 0, 0, 0, $r_width, $r_thumb, $src_width, $src_height)) { imagejpeg($resized, $source, $quality); if (!($no_response == "True")) { $msg = $msg.'» Image resized successfully \n'; } } } } function uploader($filename, $saveloc, $record, $resize, $no_response, $thumb, $width, $thumbwidth) { if ((!isset($_FILES['userfile']['name'])) || (!isset($saveloc))) { $msg = $msg."» Uploader input parameters incorrect \n"; } else { $imgtypes = array('.jpg','.gif','.bmp','.png'); $filename = date("ymdHis")."_".$_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!is_writable($saveloc)) { $msg = $msg.'» You cannot upload to the specified directory, a permissions error occured'; } else { if(move_uploaded_file($_FILES['userfile']['tmp_name'],$saveloc.$filename)) { if (in_array($ext, $imgtypes)) { if (($resize == "true") && (isset($width))) { resizer($saveloc.$filename, $saveloc.$filename, $width, 100, 'false'); list($a_width, $a_height) = getimagesize($saveloc.$filemame); insertsql($database_click_sql, $click_sql, "images", "site_id, page_id, ident, name, loc, description, width, height", "{$row_login['site_id']}', '{$_POST['page_id']}', '{$_POST['ident']}', '{$_POST['name']}', '$saveloc.$filemame', '{$_POST['description']}', '$a_width', '$a_height'"); if (($thumb == 'true') && ($isset($thumbwidth))) { resizer($saveloc.$filename, $saveloc."thumbs/".$filename, $thumbwidth, 100, 'false'); } } else if ((in_array($ext, $imgtypes)) && (!isset($width))) { if (!($no_response == "True")) { $msg = $msg.'» No width is defined \n'; } } } } else { if (!($no_response == "true")) { $msg = $msg.'» Your upload failed, undefined error \n'; } } } } } function sitepages() { $varsite_pages = $site_id; $varpage_pages = $homepage_id; if (isset($_GET['page_id'])) { $varpage_pages = $_GET['page_id']; } mysql_select_db($database_click_sql, $click_sql); $query_pages = sprintf("SELECT * FROM pages WHERE site_id = %s AND page_id = %s", GetSQLValueString($varsite_pages, "int"),GetSQLValueString($varpage_pages, "int")); $pages = mysql_query($query_pages, $click_sql) or die(mysql_error()); $row_pages = mysql_fetch_assoc($pages); $totalRows_pages = mysql_num_rows($pages); } // start functions sitepages($site_id, $homepage_id); ?> Any help is received with much thanks. Quote Link to comment Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 This meens your connection os failing. Make sure your username, password and host are all correct and you have permissions to connect. Quote Link to comment Share on other sites More sharing options...
ukweb Posted November 21, 2007 Author Share Posted November 21, 2007 They are deffinately fine, and they work when I take the query out of the function I'm defining as below; //function sitepages() { $varsite_pages = $site_id; $varpage_pages = $homepage_id; if (isset($_GET['page_id'])) { $varpage_pages = $_GET['page_id']; } mysql_select_db($database_click_sql, $click_sql); $query_pages = sprintf("SELECT * FROM pages WHERE site_id = %s AND page_id = %s", GetSQLValueString($varsite_pages, "int"),GetSQLValueString($varpage_pages, "int")); $pages = mysql_query($query_pages, $click_sql) or die(mysql_error()); $row_pages = mysql_fetch_assoc($pages); $totalRows_pages = mysql_num_rows($pages); //} can't understand why it fails when being defined in a function... Quote Link to comment Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 Because the variable $database_click_sql does not exist within the function. You either need to pass it as an argument to the function or make it global. Quote Link to comment Share on other sites More sharing options...
ukweb Posted November 21, 2007 Author Share Posted November 21, 2007 Ok, so I made the variables global, but I can only echo values produced by the function when the 'echo' is defined in the function, when I call the function and then echo a value from it, nothing is echoed. I hope this makes sense. see code below; function sitepages($s_id, $h_id) { global $_GET, $database_click_sql, $click_sql, $query_pages; $varsite_pages = $s_id; $varpage_pages = $h_id; if (isset($_GET['page_id'])) { $varpage_pages = $_GET['page_id']; } mysql_select_db($database_click_sql, $click_sql); $query_pages = sprintf("SELECT * FROM pages WHERE site_id = %s AND page_id = %s", GetSQLValueString($varsite_pages, "int"),GetSQLValueString($varpage_pages, "int")); $pages = mysql_query($query_pages, $click_sql) or die(mysql_error()); $row_pages = mysql_fetch_assoc($pages); $totalRows_pages = mysql_num_rows($pages); echo "total rows echoed inside function ".$totalRows_pages; } // test functions sitepages(1, 6); echo "total rows echoed outside function ".$totalRows_pages; The desired output would be: total rows echoed inside function 1 total rows echoed outside function 1 But the result I'm getting is: total rows echoed inside function 1 Cannot see why? Is there something I have to do to make the values produced by the function to make then usable outside the function once its been called?? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 21, 2007 Share Posted November 21, 2007 If you want a value from a function to be available outside the function, you need to return it to the calling procedure: <?php function sitepages($s_id, $h_id) { global $_GET, $database_click_sql, $click_sql, $query_pages; $varsite_pages = $s_id; $varpage_pages = $h_id; if (isset($_GET['page_id'])) { $varpage_pages = $_GET['page_id']; } mysql_select_db($database_click_sql, $click_sql); $query_pages = sprintf("SELECT * FROM pages WHERE site_id = %s AND page_id = %s", GetSQLValueString($varsite_pages, "int"),GetSQLValueString($varpage_pages, "int")); $pages = mysql_query($query_pages, $click_sql) or die(mysql_error()); $row_pages = mysql_fetch_assoc($pages); $totalRows_pages = mysql_num_rows($pages); echo "total rows echoed inside function ".$totalRows_pages; return ($totalRows_pages); } // test functions $totalRows_pages = sitepages(1, 6); echo "total rows echoed outside function ".$totalRows_pages; ?> Ken Quote Link to comment Share on other sites More sharing options...
revraz Posted November 21, 2007 Share Posted November 21, 2007 You can either use Pass by Value or Pass by Reference. Functions are a beast, and if you don't understand them, you'll get frustrated. Cannot see why? Is there something I have to do to make the values produced by the function to make then usable outside the function once its been called?? Quote Link to comment Share on other sites More sharing options...
ukweb Posted November 22, 2007 Author Share Posted November 22, 2007 I'm understanding better whats going on and how to use them now. I;ve added a bit of code and got a result using thisL function sitepages($s_id, $h_id) { global $_GET, $database_click_sql, $click_sql, $query_pages; $varsite_pages = $s_id; $varpage_pages = $h_id; if (isset($_GET['page_id'])) { $varpage_pages = $_GET['page_id']; } mysql_select_db($database_click_sql, $click_sql); $query_pages = sprintf("SELECT * FROM pages WHERE site_id = %s AND page_id = %s", GetSQLValueString($varsite_pages, "int"),GetSQLValueString($varpage_pages, "int")); $pages = mysql_query($query_pages, $click_sql) or die(mysql_error()); $row_pages = mysql_fetch_assoc($pages); $totalRows_pages = mysql_num_rows($pages); return array ($totalRows_pages, $row_pages); } // test functions list ($totalpages, $rows) = sitepages(1, 6); echo "total rows echoed outside function ".$totalpages, $rows; ?> Thing is the $row_pages bit is an array. How do I output those as part of the return array as well as the total rows? Quote Link to comment 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.