Jump to content

Simple call function.


zaas

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435879
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435884
Share on other sites

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);

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435916
Share on other sites

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
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435922
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435923
Share on other sites

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435955
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/85426-simple-call-function/#findComment-435978
Share on other sites

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.