Jump to content

Select Function Not Working Properly


White_Lily

Recommended Posts

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.

Link to comment
Share on other sites

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 >_>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.