zaas Posted January 10, 2008 Share Posted January 10, 2008 Hi everyone, I'm very new to php, but I want to pick it up as quickly as possible so if someone would be so kind to answer the following query that would be great. I have some code that calls a function. It works ok, but I wanted to do it another way which doesn't work. Heres the two examples. Could someone explain why version 2 doesn't work? ---- WORKING OK ---- if(!isset($action) or $action == 'login') { $array = array(); $query = mysql_db_query($db, "SELECT * from fck_data, * from templates order by id ASC", $connection); //call function displayLinks($array, $query); } //FUNCTIONS HERE function displayLinks ($array, $query) { while ($row = mysql_fetch_array($query)) { $id = stripslashes(nl2br($row["id"])); $title = stripslashes(nl2br($row["title"])); $filename = "showAllLinks.html"; $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); $contents = str_replace(XidX, $id, $contents); $contents = str_replace(XtitleX, $title, $contents); array_push($array,$contents); fclose ($handle); } $array = @implode("\n", $array); $filename = "welcome.html"; $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); $contents = str_replace("XshowLinksX", $array, $contents); print $contents; fclose ($handle); return; } I would rather it worked with having these 2 lines - $array = array(); $query = mysql_db_query($db, "SELECT * from fck_data, * from templates order by id ASC", $connection); - embedded in the function like this --- NOT WORKING -- if(!isset($action) or $action == 'login') { //call function displayLinks(); } //FUNCTIONS HERE function displayLinks () { $array = array(); $query = mysql_db_query($db, "SELECT * from fck_data, * from templates order by id ASC", $connection); //line 144 while ($row = mysql_fetch_array($query)) // line146 { $id = stripslashes(nl2br($row["id"])); $title = stripslashes(nl2br($row["title"])); $filename = "showAllLinks.html"; $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); $contents = str_replace(XidX, $id, $contents); $contents = str_replace(XtitleX, $title, $contents); array_push($array,$contents); fclose ($handle); } $array = @implode("\n", $array); $filename = "welcome.html"; $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); $contents = str_replace("XshowLinksX", $array, $contents); print $contents; fclose ($handle); return; } The NOT WORKING version gives the errors: Warning: mysql_db_query(): supplied argument is not a valid MySQL-Link resource in /home/sites/uk-web-solutions.co.uk/public_html/webdesign/iwd-cms/admin/index.php on line 144 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sites/uk-web-solutions.co.uk/public_html/webdesign/iwd-cms/admin/index.php on line 146 Basically i wanted the function to be self contained as I need to call it from various places and wanted to keep the coding to a minimum. If anyone has any idea on this to help me improve with php i would be very grateful. Kind Regards, Mark Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 It's a variable scope issue. The function doesn't have access to the $db and $connection variables. Either pass them as arguments for the function or put the following line as the first line in the function: global $db, $connection; Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Hi again, I realised my database information wasn't getting carried across so sending these details in the function seems to make it work. //sending displayLinks($db, $connection); //receiving function displayLinks ($db, $connection) { ... Is this the best way to call a function which requires a database connection? Thanks a lot Mark Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 As long as you are only working with one database, you should use the mysql_select_db() function after your mysql_connect() function. Then, all you need is the $connection variable. As far as argument vs global, it's a matter of preference. I use global, but that is just me. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 Oh...and instead of mysql_db_query, you would just use mysql_query() Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Hi Aaron, thanks for that, really appreciate it. I'll do as you suggested I think rather than go global with the connection variables. Regards, Mark Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Hi Aaron, I can't get that to work in my program. The top of the page looks like this at the moment. Where would I need to make the change you suggested? Thanks Mark <?php require("config.php"); $connection = mysql_connect($host,$usr,$pwd); if(!$_SESSION['login'] and !$username and !$password) { include("login.html"); exit(); } elseif (!$_SESSION['login']) { if($username == $admin_username and $password == $admin_password) { $_SESSION['login'] = true; } else { include("login_error.html"); exit(); } } if(!isset($action) or $action == 'login') { displayLinks($db, $connection); } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 10, 2008 Share Posted January 10, 2008 It should be more like: <?php require("config.php"); $connection = mysql_connect($host,$usr,$pwd); mysql_select_db($db_name,$connection); //Added select db function here if(!$_SESSION['login'] and !$username and !$password) { include("login.html"); exit(); } elseif (!$_SESSION['login']) { if($username == $admin_username and $password == $admin_password) { $_SESSION['login'] = true; } else { include("login_error.html"); exit(); } } if(!isset($action) or $action == 'login') { displayLinks($connection); //Removed $db here...also remove it from your function } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 10, 2008 Share Posted January 10, 2008 That last piece of code has lots of issues. firstlt, you never call mysql_select_db to select the database you want to use. Its also full of undefined variables. Where are $username, $admin_username, $password and $action defined? ps: Also note that you do not need to explicitely declare what connection resource your using with mysql_query, it is an optional argument that can be left off. mysql_query will simply use the current open connection providing there is one. Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Hi Thorpe, thanks for looking over this for me. I am very new to this, only been learning a couple of weeks so have a lot to do I think. $admin_username and $admin_password are in my config file. $username, $password and $action are passed variables from a form in 'login.html'. Is this the wrong way to do this? Do I still need to declare these variables? Thanks for your guidance. Regards, Mark Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Thanks rhodesa, just done that and changed the line in the function to $query = mysql_query("SELECT * from fck_data order by id ASC"); as previously instructed and its working as I imagined it should, so thanks for that. Any ideas on the variables not being initialised that thorpe mentioned? Quote Link to comment Share on other sites More sharing options...
zaas Posted January 10, 2008 Author Share Posted January 10, 2008 Actually thorpe, I don't understand why this can't be used to select a database? mysql_select_db What would I use instead of this? Thanks Mark 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.