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);

		}

Recommended Answers

All 11 Replies

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.

I had done that it returned blank as result.

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.

I have done that it returned blank screen

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

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.

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);

		}

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

Thank you so much,

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

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.

I ok now, thank you.

Cheers! If your problem is solved, mark this thread as "Solved". :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.