Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using function to return mysql data 2

Status
Not open for further replies.

ag1060

Programmer
Aug 26, 2011
27
US
Hello,

I'm trying to retrieve data from mysql to a variable and have it return using a function.


This is what I have so far..any ideas? Thanks in advance.


Code:
function display_articles() {

$pid=$this->uri->segment(3, 0);
$query = "SELECT title from articles where pid='$pid'";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){ 
$show_articles=$row[0];
    return $show_articles;
} 
}
 
Looks ok.
You could it a bit more elegantly but this will work.

Remember to escape the pod variable before using it in the query. Do this with mysql_real_escape_string.
 
Hello,

Thanks for replying. The original code only returns one row. . When I do 'echo', it posts all the rows but doesn't pass it on to the variable :
 
The original code only returns one row.

Because that's what you are telling it to return. The Return statement immediately ends the function execution, and returns the argument passed to it. so the function only gets as far as the first row, and then it ends returning that.

If you want to return all rows, you'll need to return an array of rows, or a built string.

Try for instance (for a comma separated string):

Code:
function display_articles() {

$pid=$this->uri->segment(3, 0);
$query = "SELECT title from articles where pid='$pid'";

$result = mysql_query($query) or die(mysql_error());
[blue]$show_articles="";[/blue]
while($row = mysql_fetch_array($result)){ 
$show_articles.=$row[0] [red]. ","[/red];
}
[blue]return $show_articles; [/blue]
}

Or alternatively build an array and return that.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
vacunita,

Thank you; it works. I come from Perl background and I was looking for ".=" way of assigning it in PHP like you could do in Perl. Thanks again :)
 
Hello guys,

I'm having another small problem when I add some JS code to the existing php code from above. Basically, when the user clicks on a link, a JS code menu pop ups:

Code:
function display_articles() {

$pid=$this->uri->segment(3, 0);
$query = "SELECT title from articles where pid='$pid'";

$result = mysql_query($query) or die(mysql_error());
$show_articles="";
while($row = mysql_fetch_array($result)){ 
$show_articles.= <<<EOF



	
	
<li> $row[0] <br>[<a href='#' data-popupmenu='manage_projects'>Manage</a>]<br><br>

<!--HTML for popup manage_project -->
<ul id="manage_projects" class="jqpopupmenu">
<li> <a href="$row[1]">$row[0]</a></li>
</ul>

EOF;
}
return $show_articles; 
}

The popup only shows on the first result and nothing more. The JS script itself works well except when I add it to $show_articles part.

 
you need to create a unique id for each iteration of the while loop otherwise there will be multiple uls with the id 'manage_projects' and your code doubtless defaults to the first of those. this code creates the uniqueness.

nb in the inner UL you reference $row[1], however your query returns only one column (title); so the code will throw an error on each iteration.

Code:
<?php
function display_articles() {
	$pid=mysql_real_escape_string($this->uri->segment(3, 0));
	$query = "SELECT title from articles where pid='$pid'";
	$result = mysql_query($query) or die(mysql_error());
	$show_articles="";
	$i = 0;
	while($row = mysql_fetch_array($result)): 
		$show_articles.= <<<EOF
	<li>{$row[0]}<br/>[<a href='#' data-popupmenu="manage_projects_{$i}">Manage</a>]<br/><br/>
		<!--HTML for popup manage_project -->
		<ul id="manage_projects_{$i}" class="jqpopupmenu">
			<li> <a href="{[red]$row[1][/red]}">{$row[0]}</a></li>
		</ul>
	</li>
EOF;
	endwhile;
	return $show_articles; 
} 
?>
 
jpadie ,

Great post! Thanks a lot. Once added the $x++, everything is working :)




 
oops. silly me. forgot the key element ([bad] pun intended ...) of the unique increment.
 
That's no problem. Your help was invaluable (same goes for vacunita) :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top