PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sql queries in function, help Posted by vipercyborg on 10 Feb 2006 at 10:24 AM
i am working on a function for a forum that will replace smilies. i am wanting to get a list of combinations of the different smilies from a database but when i try and sql in the function it comes back with an error. the code i currently have is
<?php
include($doc_loc.'include/config/db_config.php');
$connection = mysql_connect($db_host, $db_user, $db_pass) or die("error connecting");
mysql_select_db($db_name, $connection);

function smile($text)
{
  $query = "SELECT * FROM tit_smilies";
  $result = mysql_query($query, $connection);
  while ($smrow = mysql_fetch_assoc($result))
  {
    $rpt = $smrow['text'];
    $rpi = $smrow['replace'];
    $rpd = $smrow['dec'];
    return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
  }
}
?>


and the errors that are comming back are

Undefined variable: connection
mysql_query(): supplied argument is not a valid MySQL-Link resource
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

vipercyborg
Report
Re: sql queries in function, help Posted by tvienti on 10 Feb 2006 at 3:31 PM
Global variables aren't available inside of a function unless declared so with the global keyword.

 function smile($text)
 {
   global $connection;
   $query = "SELECT * FROM tit_smilies";
   $result = mysql_query($query, $connection);
   while ($smrow = mysql_fetch_assoc($result))
   {
     $rpt = $smrow['text'];
     $rpi = $smrow['replace'];
     $rpd = $smrow['dec'];
     return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
   }
 }



T


: i am working on a function for a forum that will replace smilies. i am wanting to get a list of combinations of the different smilies from a database but when i try and sql in the function it comes back with an error. the code i currently have is
:
<?php
: include($doc_loc.'include/config/db_config.php');
: $connection = mysql_connect($db_host, $db_user, $db_pass) or die("error connecting");
: mysql_select_db($db_name, $connection);
: 
: function smile($text)
: {
:   $query = "SELECT * FROM tit_smilies";
:   $result = mysql_query($query, $connection);
:   while ($smrow = mysql_fetch_assoc($result))
:   {
:     $rpt = $smrow['text'];
:     $rpi = $smrow['replace'];
:     $rpd = $smrow['dec'];
:     return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
:   }
: }
: ?>

:
: and the errors that are comming back are
:
: Undefined variable: connection
: mysql_query(): supplied argument is not a valid MySQL-Link resource
: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
:
: vipercyborg
:

Report
Re: sql queries in function, help Posted by vipercyborg on 10 Feb 2006 at 5:16 PM
ty
little things like that can make all the differance to how i code :)

vipercyborg


: Global variables aren't available inside of a function unless declared so with the global keyword.
:
:
:  function smile($text)
:  {
:    global $connection;
:    $query = "SELECT * FROM tit_smilies";
:    $result = mysql_query($query, $connection);
:    while ($smrow = mysql_fetch_assoc($result))
:    {
:      $rpt = $smrow['text'];
:      $rpi = $smrow['replace'];
:      $rpd = $smrow['dec'];
:      return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
:    }
:  }

: 

:
:
: T
Report
Re: sql queries in function, help Posted by vipercyborg on 11 Feb 2006 at 10:41 AM
another problem i have just came across on this pice of code that i would like some help on. the problem is each time the while function runs it works with the origional (i assum) and once the function is finnished only the last loop is displayed as the output. can someone help me or show me a better way to make a function to process text for emotes

vipercyborg

: Global variables aren't available inside of a function unless declared so with the global keyword.
:
:
:  function smile($text)
:  {
:    global $connection;
:    $query = "SELECT * FROM tit_smilies";
:    $result = mysql_query($query, $connection);
:    while ($smrow = mysql_fetch_assoc($result))
:    {
:      $rpt = $smrow['text'];
:      $rpi = $smrow['replace'];
:      $rpd = $smrow['dec'];
:      return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
:    }
:  }
: 

:
:
: T
:
:
Report
Re: sql queries in function, help Posted by tvienti on 13 Feb 2006 at 2:39 PM
Your return statement is in your while loop. This means it only runs once (on the first record) and returns the string with that smiley replaced. Try something like this:

 function smile($text)
 {
   global $connection;
   $query = "SELECT * FROM tit_smilies";
   $result = mysql_query($query, $connection);
   while ($smrow = mysql_fetch_assoc($result))
   {
     $rpt = $smrow['text'];
     $rpi = $smrow['replace'];
     $rpd = $smrow['dec'];
     $text = str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
   }

   return $text;
 }


For the sake of efficiency, it's worth mentioning that you can pass arrays into str_replace and it will match corresponding indexes. For example:

$start = array('one', 'two', 'three');
$end = array(1, 2, 3);
echo str_replace('three blind mice', $start, $end);


The example above would output "3 blind mice". You could do the same here, looping through your results and building corresponding match/replace array,s then just calling str_replace once with the two arrays as arguments. Not sure how much more efficient that is, but I'm assuming it would help.

T

: another problem i have just came across on this pice of code that i would like some help on. the problem is each time the while function runs it works with the origional (i assum) and once the function is finnished only the last loop is displayed as the output. can someone help me or show me a better way to make a function to process text for emotes
:
: vipercyborg
:
: : Global variables aren't available inside of a function unless declared so with the global keyword.
: :
: :
: :  function smile($text)
: :  {
: :    global $connection;
: :    $query = "SELECT * FROM tit_smilies";
: :    $result = mysql_query($query, $connection);
: :    while ($smrow = mysql_fetch_assoc($result))
: :    {
: :      $rpt = $smrow['text'];
: :      $rpi = $smrow['replace'];
: :      $rpd = $smrow['dec'];
: :      return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
: :    }
: :  }
: : 

: :
: :
: : T
: :
: :
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.