White_Lily Posted September 26, 2012 Share Posted September 26, 2012 Hi, i have just tried writing a function that when the needed variables are filled it will select that data from the database. Problem is is that the php.ini file is coming up with errors saying that the "$result" varible is undefined. Here's the code: <?php /*************************** *Connect to a database ***************************/ function connect(){ $host = $GLOBALS["host"]; $user = $GLOBALS["user"]; $pass = $GLOBALS["pass"]; $database = $GLOBALS["dbase"]; $con = mysql_connect($host, $user, $pass) or die("Could not connect."); $db = mysql_select_db($database) or die("Could not select database."); } /*************************** *Select data from database ***************************/ function select($amount = "*", $table, $where = NULL, $order = NULL, $limit = NULL){ $query = "SELECT ".$amount." FROM ".$table; if($where != NULL){ $query .= " WHERE ".$where; } if($order != NULL){ $query .= " ORDER BY ".$order; } if($limit != NULL){ $query .= " LIMIT ".$limit; } $result = mysql_query($query); } /*function bannerImage($imgLink, $image, $alt){ }*/ ?> and the code in the page i want data to show on: <?php select("*", "pages"); $num = mysql_num_rows($result); $fetch = mysql_fetch_assoc($result); if($num != 0){ $heading = $fetch["name"]; $content = $fetch["pageContent"]; $active = $fetch["active"]; if($active == 1){ echo "<h1>".$heading."</h1>"; echo "<p>".$content."</p>"; } }else{ echo "You need to enter page data into the database before extracting."; } ?> Any help is gladly appreciated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 26, 2012 Share Posted September 26, 2012 In your second section of code, where is $result defined? The error is fairly clear. Your function returns a value and you do nothing with it... Quote Link to comment Share on other sites More sharing options...
White_Lily Posted September 26, 2012 Author Share Posted September 26, 2012 Okay... well how do i define the variable? I was under the impression that by including the varible in the function i would only have to do mysql_num_rows($result); etc... Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 26, 2012 Share Posted September 26, 2012 http://php.net/manual/en/language.functions.php http://php.net/manual/en/functions.user-defined.php http://php.net/manual/en/functions.returning-values.php http://php.net/manual/en/language.variables.scope.php Quote Link to comment Share on other sites More sharing options...
White_Lily Posted September 26, 2012 Author Share Posted September 26, 2012 None of that actually helped... did returns like other people do them and still nothing, there aren't any spelling mistakes, and variables are constructed correctly... still getting the same undefined variable error... Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 26, 2012 Share Posted September 26, 2012 When you return a value, you must assign it to a new variable if you want to use it. There is no way you read all of those links in that short amount of time. :/ Quote Link to comment Share on other sites More sharing options...
White_Lily Posted September 26, 2012 Author Share Posted September 26, 2012 Well thats odd... $queryString = "SELECT ".$rows." FROM ".$table; if($where != null) $queryString .= " WHERE ".$where; if($order != null) $queryString .= " ORDER BY ".$order; if($limit != null) $queryString .= " LIMIT ".$limit; if($offset != null) $queryString .= " OFFSET ".$offset; $query = mysql_query($queryString); // if query is successful if($query) return $query; else return false; That code works, but i don't see variables being re-assigned new variables >_> Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 26, 2012 Share Posted September 26, 2012 That has nothing to do with your original question. The error is telling you - $result is undefined. It's defined within your function, but not when you try to use it after calling the function, because that's not how functions work. See the code below. function changeName(){ $name = 'John'; return $name; } $name = 'Bob'; changeName(); echo $name; What will be echo'd? Now change it to $name = 'Bob'; $name = changeName(); echo $name; How are they DIFFERENT? Quote Link to comment Share on other sites More sharing options...
White_Lily Posted September 26, 2012 Author Share Posted September 26, 2012 Ah okay... sorry, have been told to learn using functions because ive been apprently cluttering my code with things queries, whereas i could just call one function wherever it needs to be, and thus making my coding more simple, and easier to read. 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.