Jump to content

PHP class to return an array from database


bian101

Recommended Posts

Hey guys,

 

So im building a Content Management System for my A2 project in Computing, and i have a dataBase class. Now, i can return one thing, or do things like inserts however im having a problem returning a list of things such as:

 

dataBase.class.php

 

 
          public static function getNYCPost($table, $limit)
{
	dataBase::_dbConnect();
	if($limit == 0)
	{
		//debug: echo 'in as 0';
		$data = mysql_query("SELECT id, linklabel FROM ".$table." WHERE showing='1' AND id >= 2");
	}

	if($limit == 1)
	{
		//debug: echo 'in';
		$data = mysql_query("SELECT id, linklabel FROM ". $table ." WHERE showing='1' AND id >= 2 ORDER BY id DESC LIMIT 5");
		return $data;
	}
} 

 

When i try to do this in the main code:

 

index.php

 

<?php
		  $list = dataBase::getNYCPost($pages,1);
		//  echo $list;
		  while ($row = mysql_fetch_array($list)) 
			{ 
				 $pageId = $row["id"];
				 $linklabel = $row["linklabel"];
				 $menuDisplay .= '<a href="index.php?pid=' . $pid . '">' . $linklabel . '</a><br />';
			}
			echo $menuDisplay;
		  ?>

 

$list doesnt return anything, the previous method i used in the class was to make a list and an array and put the contents of the query in an array like:

 

list($list[] = $row);

 

or something i cant quite remember i saw a youtube video tutorial and it worked for them, but it wasnt for me.

 

 

If anyone knows how i can return various rows from a database it would be appreciated :)

 

Thanks.

Link to comment
Share on other sites

can you share your database tables and put somewhere mysql_error(); then we may see if mysql gives an error.

 

I put mysql_error(); and it didnt throw an error it just stopped executing the code at the while.

 

The relevant table is: Pages and the field names are:

 

        id int(11)

pagetitle   varchar(255)

linklabel   varchar(255)

pagebody   text

 

showing enum('0', '1')

 

Hope that helps

Link to comment
Share on other sites

dataBase::_dbConnect();

 

Here in your function, reading your entire code, it seems that both functions are in the database class, so you can use self::_dbConnect();

 

This code is pretty basic, and looks like it should work as expected, in theory. Your function should return a resource that you can use to get row values via a mysql_fetch function.

 

Where does $pages come from in your function declaration?

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.