Stumping Sql query...

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

    #1

    Stumping Sql query...

    I must be tired tonight:

    $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
    $result=mysql_q uery($query, $DB) or die(mysql_error ());
    if ($result) {
    $InContext=mysq l_fetch_assoc($ result);
    echo $query;
    print_r($InCont ext);
    }
    mysql_free_resu lt($result);

    No error but $InContext is always empty, although the $query as displayed
    runs fine from PhpMyAdmin...
    Something escapes me here.
    --
    Guillaume Dargaud
    Climbing adventures, deep cold Antarctica tales and pictures, penguins, royalty-free photography, geek code and humor, plenty of quotes...



  • sheldonlg

    #2
    Re: Stumping Sql query...

    Guillaume Dargaud wrote:
    I must be tired tonight:
    >
    $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
    $result=mysql_q uery($query, $DB) or die(mysql_error ());
    if ($result) {
    $InContext=mysq l_fetch_assoc($ result);
    echo $query;
    print_r($InCont ext);
    }
    mysql_free_resu lt($result);
    >
    No error but $InContext is always empty, although the $query as displayed
    runs fine from PhpMyAdmin...
    Something escapes me here.
    $InContext is a row from the database. If you did var_dump($InCon text)
    would give you results, or if you display the contents of the row via
    individual keys.

    (I've looked at my own code like this many times and not seen it).

    Comment

    • Jerry Stuckle

      #3
      Re: Stumping Sql query...

      Guillaume Dargaud wrote:
      I must be tired tonight:
      >
      $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
      $result=mysql_q uery($query, $DB) or die(mysql_error ());
      if ($result) {
      $InContext=mysq l_fetch_assoc($ result);
      echo $query;
      print_r($InCont ext);
      }
      mysql_free_resu lt($result);
      >
      No error but $InContext is always empty, although the $query as displayed
      runs fine from PhpMyAdmin...
      Something escapes me here.
      Did you check the number of rows returned? It sounds like the query ran
      OK, but no rows were returned (which is NOT an error).

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Guillaume Dargaud

        #4
        Re: Stumping Sql query...

        Did you check the number of rows returned? It sounds like the query ran
        OK, but no rows were returned (which is NOT an error).

        It indeed returns 0 row.

        But if I run the SAME query (copy/pasted) into PhpMyAdmin, I get the
        expected result.
        I stared at those 4 lines of code all evening. I have identical lines in
        other parts that work just fine.
        --
        Guillaume Dargaud
        Climbing adventures, deep cold Antarctica tales and pictures, penguins, royalty-free photography, geek code and humor, plenty of quotes...



        Comment

        • Rik Wasmus

          #5
          Re: Stumping Sql query...

          On Wed, 23 Apr 2008 10:59:57 +0200, Guillaume Dargaud
          <use_the_form_o n_my_contact_pa ge@www.gdargaud .netwrote:
          >Did you check the number of rows returned? It sounds like the query ran
          >OK, but no rows were returned (which is NOT an error).
          >
          It indeed returns 0 row.
          >
          But if I run the SAME query (copy/pasted) into PhpMyAdmin, I get the
          expected result.
          I stared at those 4 lines of code all evening. I have identical lines in
          other parts that work just fine.
          - The same database? (Not for instance an more or less equal database with
          an empty/other ImageList table?)
          - Are you checking the query in the _source_ of the output? Watch out for
          whitespace and the like.
          --
          Rik Wasmus

          Comment

          • C. (http://symcbean.blogspot.com/)

            #6
            Re: Stumping Sql query...

            On 23 Apr, 09:59, "Guillaume Dargaud"
            <use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
            Did you check the number of rows returned? It sounds like the query ran
            OK, but no rows were returned (which is NOT an error).
            >
            It indeed returns 0 row.
            >
            But if I run the SAME query (copy/pasted) into PhpMyAdmin, I get the
            expected result.
            You're not cutting and pasting from the code you showed us - which
            never writes out the query and has interpolated variables in it.

            C.

            Comment

            • venti

              #7
              Re: Stumping Sql query...

              You're not cutting and pasting from the code you showed us - which
              never writes out the query and has interpolated variables in it.
              >
              C.
              I am assuming you are cutting and pasting the query into phpmyadmin
              and then hard coding in an integer in place of the variable to test
              the query. Otherwise it would barf.

              Make sure that $value[0] is actually what you think it is by echoing
              it prior to your if ($result) block -- or the converse, hard code an
              integer into the php and try running it.

              Comment

              • Peter H. Coffin

                #8
                Re: Stumping Sql query...

                On Wed, 23 Apr 2008 06:12:08 -0700 (PDT), venti wrote:
                >
                >You're not cutting and pasting from the code you showed us - which
                >never writes out the query and has interpolated variables in it.
                >>
                >C.
                >
                I am assuming you are cutting and pasting the query into phpmyadmin
                and then hard coding in an integer in place of the variable to test
                the query. Otherwise it would barf.
                It also doesn't show what $query contains at the time.
                Make sure that $value[0] is actually what you think it is by echoing
                it prior to your if ($result) block -- or the converse, hard code an
                integer into the php and try running it.
                OP needs to print out the value of $query right after assembling it to
                see what it ACTUALLY contains. I don't know exactly what's niggling at
                me about the subscripted variable trying to get interpreted INSIDE the
                quoted string like that, but I remember avoiding it often enough that it
                looks odd when I read it. I've schooled myself to use dot-concat when
                building queries for SOME reason and maybe that's it.

                $query="SELECT * FROM ImageList WHERE Name='" . $value[0] . "'";

                it more how I'd write it. But, I don't know why that looks more right to
                me than the other way.

                --
                I got told by a friend's ex-girlfriend that she could tell I was
                a Linux geek from the way I *walked*.
                -- Skud

                Comment

                • Rik Wasmus

                  #9
                  Re: Stumping Sql query...

                  On Wed, 23 Apr 2008 16:02:12 +0200, Peter H. Coffin
                  <hellsop@ninehe lls.comwrote:
                  On Wed, 23 Apr 2008 06:12:08 -0700 (PDT), venti wrote:
                  >>
                  >>You're not cutting and pasting from the code you showed us - which
                  >>never writes out the query and has interpolated variables in it.
                  >>
                  >I am assuming you are cutting and pasting the query into phpmyadmin
                  >and then hard coding in an integer in place of the variable to test
                  >the query. Otherwise it would barf.
                  Nope, strings would do too apparantly... Weird that, not having an integer
                  for a name...
                  It also doesn't show what $query contains at the time.
                  >
                  >Make sure that $value[0] is actually what you think it is by echoing
                  >it prior to your if ($result) block -- or the converse, hard code an
                  >integer into the php and try running it.
                  >
                  OP needs to print out the value of $query right after assembling it to
                  see what it ACTUALLY contains.
                  And what does he do?

                  $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
                  $result=mysql_q uery($query, $DB) or die(mysql_error ());
                  if ($result) {
                  $InContext=mysq l_fetch_assoc($ result);
                  echo $query;
                  print_r($InCont ext);
                  }

                  Would that print the query? Yes it would (if it's valid), even with no
                  rows in the resultset. Does he do what y'all ask? Yes, allready, save for
                  giving us the exact output.
                  --
                  Rik Wasmus

                  Comment

                  • Captain Paralytic

                    #10
                    Re: Stumping Sql query...

                    On 23 Apr, 14:02, "Peter H. Coffin" <hell...@ninehe lls.comwrote:
                    but I remember avoiding it often enough that it
                    looks odd when I read it. I've schooled myself to use dot-concat when
                    building queries for SOME reason and maybe that's it.
                    That particular one SHOULD work OK. The troublesome ones are

                    "something $value[$n] something"

                    but this can be solved with:

                    "something {$value[$n]} something"

                    Comment

                    • venti

                      #11
                      Re: Stumping Sql query...

                      I have identical lines in
                      other parts that work just fine.

                      Well, it's a longshot, but it's possible that you have an utf-8 / asci/
                      who knows mismatch. This can happen when you paste code from a
                      browser into an editor.

                      If you really have IDENTICAL code which gives you correct results,
                      then either erase the problem code, and paste in the working code, or
                      turn it into a function and call it.

                      Comment

                      • Guillaume Dargaud

                        #12
                        Re: Stumping Sql query...

                        $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
                        $result=mysql_q uery($query, $DB) or die(mysql_error ());
                        if ($result) {
                        $InContext=mysq l_fetch_assoc($ result);
                        echo $query;
                        The query gets printed here and it's (for instance):
                        SELECT * FROM ImageList WHERE Name='MaceTrave rse'
                        which when fed straight into PhpMyAdmin returns:
                        Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
                        MaceTraverse Climbing/Arizona/MaceTraverse.jp g Arizona

                        For reference the table is defined as
                        CREATE TABLE ImageList (
                        Name VARCHAR(100) NOT NULL,
                        Path VARCHAR(255) NOT NULL,
                        Source TEXT,
                        PRIMARY KEY (Name))

                        I just tried again after resetting Apache, and still no go.

                        If I add the following code to print everything:

                        $query="SELECT * FROM ImageList";
                        echo $query."|<BR>";
                        $result=mysql_q uery($query) or die(mysql_error ());
                        while ($InContext=mys ql_fetch_assoc( $result)) { print_r($InCont ext); echo
                        "<BR>"; }
                        mysql_free_resu lt($result);

                        Then among the output I DO get a line containing:
                        Array ( [Name] =MaceTraverse [Path] =Climbing/Arizona/MaceTraverse.jp g
                        [Source] =Arizona )

                        print_r($InCont ext);
                        }
                        mysql_free_resu lt($result);
                        >
                        No error but $InContext is always empty, although the $query as displayed
                        runs fine from PhpMyAdmin...
                        Something escapes me here.
                        --
                        Guillaume Dargaud
                        Climbing adventures, deep cold Antarctica tales and pictures, penguins, royalty-free photography, geek code and humor, plenty of quotes...

                        >

                        Comment

                        • Rik Wasmus

                          #13
                          Re: Stumping Sql query...

                          On Wed, 23 Apr 2008 22:04:54 +0200, Guillaume Dargaud
                          <USE_MY_WEB_FOR M@gdargaud.netw rote:
                          > $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
                          > $result=mysql_q uery($query, $DB) or die(mysql_error ());
                          > if ($result) {
                          > $InContext=mysq l_fetch_assoc($ result);
                          > echo $query;
                          >
                          The query gets printed here and it's (for instance):
                          SELECT * FROM ImageList WHERE Name='MaceTrave rse'
                          which when fed straight into PhpMyAdmin returns:
                          Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
                          MaceTraverse Climbing/Arizona/MaceTraverse.jp g Arizona
                          >
                          For reference the table is defined as
                          CREATE TABLE ImageList (
                          Name VARCHAR(100) NOT NULL,
                          Path VARCHAR(255) NOT NULL,
                          Source TEXT,
                          PRIMARY KEY (Name))
                          >
                          I just tried again after resetting Apache, and still no go.
                          >
                          If I add the following code to print everything:
                          >
                          $query="SELECT * FROM ImageList";
                          echo $query."|<BR>";
                          $result=mysql_q uery($query) or die(mysql_error ());
                          while ($InContext=mys ql_fetch_assoc( $result)) { print_r($InCont ext);echo
                          "<BR>"; }
                          mysql_free_resu lt($result);
                          >
                          Then among the output I DO get a line containing:
                          Array ( [Name] =MaceTraverse [Path] =
                          Climbing/Arizona/MaceTraverse.jp g
                          [Source] =Arizona )
                          Which is NOT the formatting print_r will deliver if you view it in the
                          source. It will have newlines.
                          Just, humor me, what happens if you do a $value[0] = trim($value[0]);
                          before making the $query string? And if that still doesn't work, examine
                          the _source_ of a possibly html browser you're using to check the query,
                          and maybe check in a hex editor what the ascii values are.
                          --
                          Rik Wasmus

                          Comment

                          • Guillaume Dargaud

                            #14
                            Re: Stumping Sql query...

                            Just, humor me, what happens if you do a $value[0] = trim($value[0]);

                            Sure. Here's the modified code:

                            $value[0] = trim($value[0]);
                            echo "|$value[0]|<BR>";
                            $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
                            echo $query."<BR>";
                            $result=mysql_q uery($query) or die(mysql_error ());
                            if ($result) { $InContext=mysq l_fetch_assoc($ result); echo
                            "|$InContext|<B R>"; }
                            mysql_free_resu lt($result);

                            It prints
                            |MaceTraverse|
                            SELECT * FROM ImageList WHERE Name='MaceTrave rse'
                            ||

                            And if that still doesn't work, examine the _source_ of a possibly html
                            browser
                            |<WBR>Mace<WBR> Traverse|<BR>SE LECT * FROM ImageList WHERE
                            Name='<WBR>Mace <WBR>Traverse'< BR>||

                            Well, I guess you nailed it. I don't know what those <WBRare doing there,
                            but they have no business in my nice query !!!

                            Thanks Rik and Co.
                            --
                            Guillaume Dargaud
                            Climbing adventures, deep cold Antarctica tales and pictures, penguins, royalty-free photography, geek code and humor, plenty of quotes...



                            Comment

                            • Peter H. Coffin

                              #15
                              Re: Stumping Sql query...

                              On Wed, 23 Apr 2008 22:04:54 +0200, Guillaume Dargaud wrote:
                              > $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
                              > $result=mysql_q uery($query, $DB) or die(mysql_error ());
                              > if ($result) {
                              > $InContext=mysq l_fetch_assoc($ result);
                              > echo $query;
                              >
                              The query gets printed here and it's (for instance):
                              SELECT * FROM ImageList WHERE Name='MaceTrave rse'
                              which when fed straight into PhpMyAdmin returns:
                              Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
                              MaceTraverse Climbing/Arizona/MaceTraverse.jp g Arizona
                              >
                              For reference the table is defined as
                              CREATE TABLE ImageList (
                              Name VARCHAR(100) NOT NULL,
                              Path VARCHAR(255) NOT NULL,
                              Source TEXT,
                              PRIMARY KEY (Name))
                              >
                              I just tried again after resetting Apache, and still no go.
                              >
                              If I add the following code to print everything:
                              >
                              $query="SELECT * FROM ImageList";
                              echo $query."|<BR>";
                              $result=mysql_q uery($query) or die(mysql_error ());
                              while ($InContext=mys ql_fetch_assoc( $result)) { print_r($InCont ext); echo
                              "<BR>"; }
                              mysql_free_resu lt($result);
                              >
                              Then among the output I DO get a line containing:
                              Array ( [Name] =MaceTraverse [Path] =Climbing/Arizona/MaceTraverse.jp g
                              [Source] =Arizona )
                              Great. It's working perfectly then. Your query fetches one row, into
                              array $InContext with named elements Name, Path, Source, which is
                              clearly not empty, and all is wonderful.

                              What problem are you having again?
                              > print_r($InCont ext);
                              > }
                              > mysql_free_resu lt($result);
                              >>
                              >No error but $InContext is always empty, although the $query as displayed
                              >runs fine from PhpMyAdmin...
                              >Something escapes me here.
                              >--
                              >Guillaume Dargaud
                              >http://www.gdargaud.net/
                              >>
                              >
                              >

                              --
                              4. Shooting is not too good for my enemies.
                              --Peter Anspach's list of things to do as an Evil Overlord

                              Comment

                              Working...