sql queries in function, help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sql queries in function, help

vipercyborgvipercyborg Posts: 20Member
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
[code]<?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);
}
}
?>[/code]

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

Comments

  • tvientitvienti Posts: 230Member
    Global variables aren't available inside of a function unless declared so with the global keyword.

    [code]
    function smile($text)
    {
    [red]global $connection;[/red]
    $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",''.$rpd.'', $text);
    }
    }
    [/code]


    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
    : [code]<?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);
    : }
    : }
    : ?>[/code]
    :
    : 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
    :

  • vipercyborgvipercyborg Posts: 20Member
    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.
    :
    : [code]
    : function smile($text)
    : {
    : [red]global $connection;[/red]
    : $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",''.$rpd.'', $text);
    : }
    : }

    : [/code]
    :
    :
    : T
  • vipercyborgvipercyborg Posts: 20Member
    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.
    :
    : [code]
    : function smile($text)
    : {
    : [red]global $connection;[/red]
    : $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",''.$rpd.'', $text);
    : }
    : }
    : [/code]
    :
    :
    : T
    :
    :
  • tvientitvienti Posts: 230Member
    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:

    [code]
    function smile($text)
    {
    [red]global $connection;[/red]
    $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",''.$rpd.'', $text);
    }

    return $text;
    }
    [/code]

    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:

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

    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.
    : :
    : : [code]
    : : function smile($text)
    : : {
    : : [red]global $connection;[/red]
    : : $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",''.$rpd.'', $text);
    : : }
    : : }
    : : [/code]
    : :
    : :
    : : T
    : :
    : :
    :

Sign In or Register to comment.