Stumping Sql query...

Stumping Sql query...

am 22.04.2008 23:20:06 von Guillaume Dargaud

I must be tired tonight:

$query="SELECT * FROM ImageList WHERE Name='$value[0]'";
$result=mysql_query($query, $DB) or die(mysql_error());
if ($result) {
$InContext=mysql_fetch_assoc($result);
echo $query;
print_r($InContext);
}
mysql_free_result($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/

Re: Stumping Sql query...

am 22.04.2008 23:37:56 von sheldonlg

Guillaume Dargaud wrote:
> I must be tired tonight:
>
> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
> $result=mysql_query($query, $DB) or die(mysql_error());
> if ($result) {
> $InContext=mysql_fetch_assoc($result);
> echo $query;
> print_r($InContext);
> }
> mysql_free_result($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($InContext)
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).

Re: Stumping Sql query...

am 23.04.2008 01:59:26 von Jerry Stuckle

Guillaume Dargaud wrote:
> I must be tired tonight:
>
> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
> $result=mysql_query($query, $DB) or die(mysql_error());
> if ($result) {
> $InContext=mysql_fetch_assoc($result);
> echo $query;
> print_r($InContext);
> }
> mysql_free_result($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@attglobal.net
==================

Re: Stumping Sql query...

am 23.04.2008 10:59:57 von Guillaume Dargaud

> 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
http://www.gdargaud.net/

Re: Stumping Sql query...

am 23.04.2008 11:09:28 von luiheidsgoeroe

On Wed, 23 Apr 2008 10:59:57 +0200, Guillaume Dargaud
wrote:
>> 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

Re: Stumping Sql query...

am 23.04.2008 14:03:01 von colin.mckinnon

On 23 Apr, 09:59, "Guillaume Dargaud"
wrote:
> > 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.

Re: Stumping Sql query...

am 23.04.2008 15:12:08 von venti

> 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.

Re: Stumping Sql query...

am 23.04.2008 16:02:12 von hellsop

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

Re: Stumping Sql query...

am 23.04.2008 16:38:11 von luiheidsgoeroe

On Wed, 23 Apr 2008 16:02:12 +0200, Peter H. Coffin =

wrote:

> 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 integ=
er =

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=3D"SELECT * FROM ImageList WHERE Name=3D'$value[0]'";
$result=3Dmysql_query($query, $DB) or die(mysql_error());
if ($result) {
$InContext=3Dmysql_fetch_assoc($result);
echo $query;
print_r($InContext);
}

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 fo=
r =

giving us the exact output.
-- =

Rik Wasmus

Re: Stumping Sql query...

am 23.04.2008 16:40:53 von Captain Paralytic

On 23 Apr, 14:02, "Peter H. Coffin" wrote:
> 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"

Re: Stumping Sql query...

am 23.04.2008 17:37:36 von venti

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.

Re: Stumping Sql query...

am 23.04.2008 22:04:54 von Guillaume Dargaud

> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
> $result=mysql_query($query, $DB) or die(mysql_error());
> if ($result) {
> $InContext=mysql_fetch_assoc($result);
> echo $query;

The query gets printed here and it's (for instance):
SELECT * FROM ImageList WHERE Name='MaceTraverse'
which when fed straight into PhpMyAdmin returns:
Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
MaceTraverse Climbing/Arizona/MaceTraverse.jpg 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."|
";
$result=mysql_query($query) or die(mysql_error());
while ($InContext=mysql_fetch_assoc($result)) { print_r($InContext); echo
"
"; }
mysql_free_result($result);

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


> print_r($InContext);
> }
> mysql_free_result($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/
>

Re: Stumping Sql query...

am 23.04.2008 22:20:52 von luiheidsgoeroe

On Wed, 23 Apr 2008 22:04:54 +0200, Guillaume Dargaud =

wrote:

>> $query=3D"SELECT * FROM ImageList WHERE Name=3D'$value[0]'";
>> $result=3Dmysql_query($query, $DB) or die(mysql_error());
>> if ($result) {
>> $InContext=3Dmysql_fetch_assoc($result);
>> echo $query;
>
> The query gets printed here and it's (for instance):
> SELECT * FROM ImageList WHERE Name=3D'MaceTraverse'
> which when fed straight into PhpMyAdmin returns:
> Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
> MaceTraverse Climbing/Arizona/MaceTraverse.jpg 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=3D"SELECT * FROM ImageList";
> echo $query."|
";
> $result=3Dmysql_query($query) or die(mysql_error());
> while ($InContext=3Dmysql_fetch_assoc($result)) { print_r($InContext);=
echo
> "
"; }
> mysql_free_result($result);
>
> Then among the output I DO get a line containing:
> Array ( [Name] =3D> MaceTraverse [Path] =3D> =

> Climbing/Arizona/MaceTraverse.jpg
> [Source] =3D> 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] =3D 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

Re: Stumping Sql query...

am 23.04.2008 22:36:59 von Guillaume Dargaud

> 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]|
";
$query="SELECT * FROM ImageList WHERE Name='$value[0]'";
echo $query."
";
$result=mysql_query($query) or die(mysql_error());
if ($result) { $InContext=mysql_fetch_assoc($result); echo
"|$InContext|
"; }
mysql_free_result($result);

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


> And if that still doesn't work, examine the _source_ of a possibly html
> browser

|MaceTraverse|
SELECT * FROM ImageList WHERE
Name='MaceTraverse'
||

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

Thanks Rik and Co.
--
Guillaume Dargaud
http://www.gdargaud.net/

Re: Stumping Sql query...

am 23.04.2008 23:01:50 von hellsop

On Wed, 23 Apr 2008 22:04:54 +0200, Guillaume Dargaud wrote:
>> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
>> $result=mysql_query($query, $DB) or die(mysql_error());
>> if ($result) {
>> $InContext=mysql_fetch_assoc($result);
>> echo $query;
>
> The query gets printed here and it's (for instance):
> SELECT * FROM ImageList WHERE Name='MaceTraverse'
> which when fed straight into PhpMyAdmin returns:
> Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
> MaceTraverse Climbing/Arizona/MaceTraverse.jpg 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."|
";
> $result=mysql_query($query) or die(mysql_error());
> while ($InContext=mysql_fetch_assoc($result)) { print_r($InContext); echo
> "
"; }
> mysql_free_result($result);
>
> Then among the output I DO get a line containing:
> Array ( [Name] => MaceTraverse [Path] => Climbing/Arizona/MaceTraverse.jpg
> [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($InContext);
>> }
>> mysql_free_result($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

Re: Stumping Sql query...

am 23.04.2008 23:03:32 von hellsop

On Wed, 23 Apr 2008 22:20:52 +0200, Rik Wasmus wrote:
> On Wed, 23 Apr 2008 22:04:54 +0200, Guillaume Dargaud
> wrote:
>
>>> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
>>> $result=mysql_query($query, $DB) or die(mysql_error());
>>> if ($result) {
>>> $InContext=mysql_fetch_assoc($result);
>>> echo $query;
>>
>> The query gets printed here and it's (for instance):
>> SELECT * FROM ImageList WHERE Name='MaceTraverse'
>> which when fed straight into PhpMyAdmin returns:
>> Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
>> MaceTraverse Climbing/Arizona/MaceTraverse.jpg 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."|
";
>> $result=mysql_query($query) or die(mysql_error());
>> while ($InContext=mysql_fetch_assoc($result)) { print_r($InContext); echo
>> "
"; }
>> mysql_free_result($result);
>>
>> Then among the output I DO get a line containing:
>> Array ( [Name] => MaceTraverse [Path] =>
>> Climbing/Arizona/MaceTraverse.jpg
>> [Source] => Arizona )
>
> Which is NOT the formatting print_r will deliver if you view it in the
> source. It will have newlines.

It'll looks like that if the output is being viewed in a browser. The
whole "
" thing is a bit of a hint that this is the case.

> 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.


--
Either way, it'll remind the clued that there's only one letter
difference between 'turkey' and 'turnkey'.
-- Mike Andrews

Re: Stumping Sql query...

am 24.04.2008 02:23:45 von sheldonlg

Guillaume Dargaud wrote:
>> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
>> $result=mysql_query($query, $DB) or die(mysql_error());
>> if ($result) {
>> $InContext=mysql_fetch_assoc($result);
>> echo $query;
>
> The query gets printed here and it's (for instance):
> SELECT * FROM ImageList WHERE Name='MaceTraverse'
> which when fed straight into PhpMyAdmin returns:
> Showing rows 0 - 0 (1 total, Query took 0.0004 sec)
> MaceTraverse Climbing/Arizona/MaceTraverse.jpg 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."|
";
> $result=mysql_query($query) or die(mysql_error());
> while ($InContext=mysql_fetch_assoc($result)) { print_r($InContext); echo
> "
"; }
> mysql_free_result($result);
>
> Then among the output I DO get a line containing:
> Array ( [Name] => MaceTraverse [Path] => Climbing/Arizona/MaceTraverse.jpg
> [Source] => Arizona )
>
>
>> print_r($InContext);
>> }
>> mysql_free_result($result);
>>
>> No error but $InContext is always empty, although the $query as displayed
>> runs fine from PhpMyAdmin...
>> Something escapes me here.
>> --

I told you this before as the first reply to your post.
Try echoing $InContext['Path'] and $InContext['Source'] instead of
$InContext and see what happens.

$InContext is a ROW.

Re: Stumping Sql query...

am 24.04.2008 09:45:33 von luiheidsgoeroe

On Thu, 24 Apr 2008 02:23:45 +0200, sheldonlg wrote:
>>> print_r($InContext);

> I told you this before as the first reply to your post.
> Try echoing $InContext['Path'] and $InContext['Source'] instead of
> $InContext and see what happens.

Please don't confuse the issue more because you don't know what print_r()
is.
--
Rik Wasmus

Re: Stumping Sql query...

am 24.04.2008 13:09:50 von sheldonlg

Rik Wasmus wrote:
> On Thu, 24 Apr 2008 02:23:45 +0200, sheldonlg wrote:
>>>> print_r($InContext);
>
>> I told you this before as the first reply to your post.
>> Try echoing $InContext['Path'] and $InContext['Source'] instead of
>> $InContext and see what happens.
>
> Please don't confuse the issue more because you don't know what
> print_r() is.

I missed that. His original had "echo".

Re: Stumping Sql query...

am 24.04.2008 13:15:39 von luiheidsgoeroe

On Thu, 24 Apr 2008 13:09:50 +0200, sheldonlg wrote:

> Rik Wasmus wrote:
>> On Thu, 24 Apr 2008 02:23:45 +0200, sheldonlg wrote:
>>>>> print_r($InContext);
>>
>>> I told you this before as the first reply to your post.
>>> Try echoing $InContext['Path'] and $InContext['Source'] instead of =

>>> $InContext and see what happens.
>> Please don't confuse the issue more because you don't know what =

>> print_r() is.
>
> I missed that. His original had "echo".

No, it didn't. His original echo'ed the query, and print_r'ed the result=
=

of mysql_fetch_assoc:

Message-ID: :
[quote]
I must be tired tonight:

$query=3D"SELECT * FROM ImageList WHERE Name=3D'$value[0]'";
$result=3Dmysql_query($query, $DB) or die(mysql_error());
if ($result) {
$InContext=3Dmysql_fetch_assoc($result);
echo $query;
print_r($InContext);
}
mysql_free_result($result);
[/quote]
-- =

Rik Wasmus

Re: Stumping Sql query...

am 24.04.2008 16:04:15 von sheldonlg

Rik Wasmus wrote:
> On Thu, 24 Apr 2008 13:09:50 +0200, sheldonlg wrote:
>
>> Rik Wasmus wrote:
>>> On Thu, 24 Apr 2008 02:23:45 +0200, sheldonlg wrote:
>>>>>> print_r($InContext);
>>>
>>>> I told you this before as the first reply to your post.
>>>> Try echoing $InContext['Path'] and $InContext['Source'] instead of
>>>> $InContext and see what happens.
>>> Please don't confuse the issue more because you don't know what
>>> print_r() is.
>>
>> I missed that. His original had "echo".
>
> No, it didn't. His original echo'ed the query, and print_r'ed the result
> of mysql_fetch_assoc:
>
> Message-ID: :
> [quote]
> I must be tired tonight:
>
> $query="SELECT * FROM ImageList WHERE Name='$value[0]'";
> $result=mysql_query($query, $DB) or die(mysql_error());
> if ($result) {
> $InContext=mysql_fetch_assoc($result);
> echo $query;
> print_r($InContext);
> }
> mysql_free_result($result);
> [/quote]

You are correct. I read it too quickly and remembered correctly what I
had incorrectly read.