0

i am a new to php, am trying to get the code below to work - i am getting it is returning resource id#14

Please bail me out


file 1

<table>   
<tr>
  <td width="245" class="style8"><?php 
										echo "<b>".$database->getLastPoems()."</b>";
										// Echoes: string
										echo $result;

										?></td>
       </tr>
</table>

file 2

function getLastPoems(){

		$q = "SELECT title FROM". TBL_POEMS. "LIMIT 0,10";
		$result = mysql_query($q, $this->connection);
		if ($result)
			{  
				$row = mysql_fetch_assoc($result);
				$referral = $row['title'];
				mysql_free_result($result);
				}  
			return ($result);

		}

Edited by nav33n: Please use [code][/code] tags to wrap your code for easy readability.

2
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by nav33n
0

Hi. Welcome to Daniweb.

Instead of returning $referral, you are returning $result. $result will have the mysql result resource from the previous query [select query] if the query was successful OR it will have 'false' if the query fails.
Read more on mysql_query here.

0

Did you print the query and execute it in mysql console ? Maybe the query itself is returning an empty result set. Also print $referral value inside the function to see if it has any value.

0

I have done that it returned blank screen

but "SELECT title FROM poems LIMIT 0,10" returned the expected result.

0

Okay, Have you turned off your errors ? Can you post all the related code ?

I hope you are using this function getLastPoems in a class and can connect to the database without any problems.

0

Below are the extract from the two files, for connect it is perfect because I have the files seperated i.e. constant, database, session e.t.c

<?php 
										echo "<b>".$database->getLastPoems()."</b>";
										// Echoes: string
										echo $referral;

										?>

File 2

function getLastPoems(){

		$q = "SELECT title FROM". TBL_POEMS. "LIMIT 0,10";
		$result = mysql_query($q, $this->connection);
		if ($result)
			{  
				$row = mysql_fetch_assoc($result);
				$referral = $row['title'];
				mysql_free_result($result);
				}  
			return ($referral);

		}
0

First of all, echo $referral in file1 will definitely not print anything because, the scope of the variable $referral is local to the function getLastPoems and is not available outside the function.

I modified your code a bit and tried it. The problem is with the query.

$q = "SELECT title FROM". TBL_POEMS. "LIMIT 0,10";

This will be,

SELECT title FROMpoemtablenameLIMIT 0,10

You see what I am saying ? You need to have a 'space' after FROM and before LIMIT.
ie.,

$q = "SELECT title FROM ". TBL_POEMS. " LIMIT 0,10";

Cheers!
Naveen

0

Thank you so much,

It returned one row but i want it to return the last 10 rows, please help.

0
if ($result)
			{  
				while($row = mysql_fetch_assoc($result)) {
				$referral[] = $row['title'];

				}  
                          }
			return $referral;

		}

You use a while loop to loop through all the result returned by the query, put it in an array and return the array.
Then call the function,

$poems = $database->getLastPoems();
foreach($poems as $poemname) {
  echo $poemname;
}

Something like that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.