sql-query works alone but not in function...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis

    sql-query works alone but not in function...

    Hy,

    My english is not the best, so if you don´t understand something,
    please ask!

    I want to print the content of a mysql-table where a field is a
    specified value.

    For that i have written some code that works fine. But i don´t want to
    rewrite the code everytime the specified value changes, so i tried to
    make a function out of my code.
    But now, i get a mysql error:
    Warning: Supplied argument is not a valid MySQL result resource in
    ..........\conf ig.php on line 35

    I have only added one line before the code:
    function selLinkKategori e() {

    and i want to call that function with:
    selLinkKategori e();

    It would be very nice if someone can help me a bit, cause i´m still
    learning php, and i don´t see the problem...

    Here is the code (line 35 from the error is line 6 here):



    function selLinkKategori e() {
    $verbindung = mysql_connect($ baseurl, $dbuser, $dbpass);
    mysql_select_db ($dbase, $verbindung);
    $sql = "SELECT * FROM $links WHERE kategorie = 'tuts'";
    $result = mysql_query($sq l, $verbindung);

    $num = mysql_num_rows( $result);
    $cur = 1;

    echo '<table width="727" border="0" align="center" cellspacing="2"
    cellpadding="1" class="content" >
    <tr>
    <td nowrap><font color="#003366" ><b><u>Link</u></b></font></td>
    <td nowrap><font color="#003366" ><b><u>Beschrei bung</u></b></font></td>
    </tr>';

    while ($num >= $cur) {
    $row = mysql_fetch_arr ay($result);

    $link = $row["link"];
    $beschr = $row["beschreibu ng"];

    printf ("<tr><td nowrap bgcolor='%s'>$l ink</td>", bgcolor(3));
    printf ("<td nowrap bgcolor='%s'>$b eschr</td>", bgcolor(3));

    $cur++;
    }
    echo "</table>";
    }
  • Tom Thackrey

    #2
    Re: sql-query works alone but not in function...


    On 27-Sep-2003, dennis@schilde. org (Dennis) wrote:
    [color=blue]
    > My english is not the best, so if you don´t understand something,
    > please ask!
    >
    > I want to print the content of a mysql-table where a field is a
    > specified value.
    >
    > For that i have written some code that works fine. But i don´t want to
    > rewrite the code everytime the specified value changes, so i tried to
    > make a function out of my code.
    > But now, i get a mysql error:
    > Warning: Supplied argument is not a valid MySQL result resource in
    > .........\confi g.php on line 35
    >
    > I have only added one line before the code:
    > function selLinkKategori e() {
    >
    > and i want to call that function with:
    > selLinkKategori e();
    >
    > It would be very nice if someone can help me a bit, cause i´m still
    > learning php, and i don´t see the problem...
    >
    > Here is the code (line 35 from the error is line 6 here):
    >
    >
    >
    > function selLinkKategori e() {
    > $verbindung = mysql_connect($ baseurl, $dbuser, $dbpass);
    > mysql_select_db ($dbase, $verbindung);
    > $sql = "SELECT * FROM $links WHERE kategorie = 'tuts'";
    > $result = mysql_query($sq l, $verbindung);
    >
    > $num = mysql_num_rows( $result);
    > $cur = 1;
    >
    > echo '<table width="727" border="0" align="center" cellspacing="2"
    > cellpadding="1" class="content" >
    > <tr>
    > <td nowrap><font color="#003366" ><b><u>Link</u></b></font></td>
    > <td nowrap><font
    > color="#003366" ><b><u>Beschrei bung</u></b></font></td>
    > </tr>';
    >
    > while ($num >= $cur) {
    > $row = mysql_fetch_arr ay($result);
    >
    > $link = $row["link"];
    > $beschr = $row["beschreibu ng"];
    >
    > printf ("<tr><td nowrap bgcolor='%s'>$l ink</td>", bgcolor(3));
    > printf ("<td nowrap bgcolor='%s'>$b eschr</td>", bgcolor(3));
    >
    > $cur++;
    > }
    > echo "</table>";
    > }[/color]

    You either need to pass parameters for $baseurl, $dbuser, $dbpass, and
    $dbase or declare them as globals.

    --
    Tom Thackrey

    Comment

    • Jan Pieter Kunst

      #3
      Re: sql-query works alone but not in function...

      In article <de9e51d.030927 0720.1c472c04@p osting.google.c om>,
      dennis@schilde. org (Dennis) wrote:
      [color=blue]
      > It would be very nice if someone can help me a bit, cause i´m still
      > learning php, and i don´t see the problem...
      >
      > Here is the code (line 35 from the error is line 6 here):
      >
      >
      >
      > function selLinkKategori e() {
      > $verbindung = mysql_connect($ baseurl, $dbuser, $dbpass);
      > mysql_select_db ($dbase, $verbindung);
      > $sql = "SELECT * FROM $links WHERE kategorie = 'tuts'";
      > $result = mysql_query($sq l, $verbindung);
      >
      > $num = mysql_num_rows( $result);
      > $cur = 1;
      >
      > echo '<table width="727" border="0" align="center" cellspacing="2"
      > cellpadding="1" class="content" >
      > <tr>
      > <td nowrap><font color="#003366" ><b><u>Link</u></b></font></td>
      > <td nowrap><font color="#003366" ><b><u>Beschrei bung</u></b></font></td>
      > </tr>';
      >
      > while ($num >= $cur) {
      > $row = mysql_fetch_arr ay($result);
      >
      > $link = $row["link"];
      > $beschr = $row["beschreibu ng"];
      >
      > printf ("<tr><td nowrap bgcolor='%s'>$l ink</td>", bgcolor(3));
      > printf ("<td nowrap bgcolor='%s'>$b eschr</td>", bgcolor(3));
      >
      > $cur++;
      > }[/color]


      You need to explicitly pass the variables $baseurl, $dbuser, $dbpass and
      $dbase into your function. Otherwise those variables are undefined
      inside the function.

      function selLinkKategori e($baseurl, $dbuser, $dbpass, $dbase) {
      ...
      }

      By the way, the mysql_fetch_arr ay loop can be done with fewer steps:

      while($row = mysql_fetch_arr ay($result)) {
      ...
      }

      JP

      --
      Sorry, <devnull@cauce. org> is een "spam trap".
      E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

      Comment

      • Geoff Berrow

        #4
        Re: sql-query works alone but not in function...

        I noticed that Message-ID:
        <MZhdb.9927$TW. 7975@newssvr25. news.prodigy.co m> from Tom Thackrey
        contained the following:
        [color=blue]
        >You either need to pass parameters for $baseurl, $dbuser, $dbpass, and
        >$dbase or declare them as globals.[/color]

        And also to the where clause.

        --
        Geoff Berrow
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        Working...