bian101 Posted January 21, 2012 Share Posted January 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255471-php-class-to-return-an-array-from-database/ Share on other sites More sharing options...
erdem Posted January 21, 2012 Share Posted January 21, 2012 can you share your database tables and put somewhere mysql_error(); then we may see if mysql gives an error. Quote Link to comment https://forums.phpfreaks.com/topic/255471-php-class-to-return-an-array-from-database/#findComment-1309795 Share on other sites More sharing options...
bian101 Posted January 21, 2012 Author Share Posted January 21, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255471-php-class-to-return-an-array-from-database/#findComment-1309798 Share on other sites More sharing options...
AyKay47 Posted January 21, 2012 Share Posted January 21, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255471-php-class-to-return-an-array-from-database/#findComment-1309802 Share on other sites More sharing options...
bian101 Posted January 21, 2012 Author Share Posted January 21, 2012 Ah! Rookie mistake :/ it should be: $list = sql::getNYCPost("pages",1); as pages is the name of the table and its not stored in a variable. Thank you! That seems to have done the job I just gotta work on the url now, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/255471-php-class-to-return-an-array-from-database/#findComment-1309803 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.