Newbie alert: supplied argument is not a valid MySQL result resource

Newbie alert: supplied argument is not a valid MySQL result resource

am 10.07.2007 04:51:56 von Matt Leonhardt

Okay, so I get that the mysql_query() function here is not returning a valid
resource identifier. I just can't figure out why...here's the pertinent
code:

(db.php)

$db = @mysql_connect('www.myhost.edu', 'user', 'pass');

if(!$db || !@mysql_select_db('osa', $db))
{
die('unable to connect to mysql server: ' . mysql_error());
}
?>

(auth.php)

// Load database
require_once("db.php");

// are we trying to authenticate?
if (isset($_POST["login"]) && isset($_POST["passwd"])) {
// get password hash from database
$query = mysql_query("SELECT EmpPassEncrypt FROM Employees WHERE
EmpEmail='" . $_POST["login"] . "'");
$passwd = mysql_fetch_array($query);

// check for entry
if (!$passwd[0]) {
// do stuff
}

// Authenticate against given password
if ($passwd[0] == crypt($_POST['passwd'], substr($passwd[0], 0, 2))) {

// Success!
// Set session info
$skey = crypt($passwd[0], substr(time(), -2));
$query = mysql_query("INSERT INTO Sessions(SKey, STimeStamp) VALUES ('" .
$skey . "', FROM_UNIXTIME(" . time() . "))")
|| die ('Could not write session data: ' . mysql_error());

// get sid and write cookies
$query = mysql_query("SELECT MAX(SID) FROM Sessions")
|| die ('Could not query database: ' . mysql_error());
$sid = mysql_fetch_array($query) || die ('Could not fetch from database:
' . mysql_error()); // <--- line 49
setcookie("user", $_POST["login"]);
setcookie("sid", $sid[0]);
setcookie("skey", $skey);

This yields the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
resource in E:\OSA\functions\auth.php on line 49
Could not fetch from database:

Okay, so $query is not returning a valid resource identifier, but it's also
not returning false, because it doesn't die at line 48... so I try:

(between lines 48 & 49): die($query);

And I get: 1

wtf? As I understand it, SELECT statements should return 0 or a resource
identifier only. I have no idea what a result of "1" means, or why it's
happening. And yes, the SQL line works fine in MySQL. I tried to Google
this one, but all I got were people with bad syntax in their SQL, or who
were getting false from mysql_query() but not error checking it. Also, is
there any way to tell PHP to throw an error instead of a warning when I try
an operation on an invalid resource like this?

Thanks a bunch,
Matt

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Newbie alert: supplied argument is not a valid MySQL result resource

am 10.07.2007 05:57:05 von Niel Archer

Hi

On my way to bed so too tired too look closely, but at a guess I'd say
it's a logic error. I would guess this:

> $sid = mysql_fetch_array($query) || die ('Could not fetch from database:
> ' . mysql_error()); // <--- line 49

isn't evaluating in the order you expect.. You're using a logic
statement as an assignment, and the result of that get's assigned to your
variable not the resource you expect. Try it like this to see if it works:

$sid = mysql_fetch_array($query);
if ($sid === false) die ('Could not fetch from database: ' . mysql_error());

If that's not the problem, I haven't got a clue until after sleep and
coffee.

Niel

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Newbie alert: supplied argument is not a valid MySQL result resource

am 11.07.2007 11:48:13 von lameck kassana

------=_Part_1935_28701591.1184147293386
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

hey just make this
$sid = mysql_fetch_array($query) or die ('Could not fetch from database:
> ' ." mysql_error().");
try this

------=_Part_1935_28701591.1184147293386--

Re: Newbie alert: supplied argument is not a valid MySQL result resource

am 11.07.2007 13:04:03 von niel

Hi

> On my way to bed so too tired too look closely, but at a guess I'd say
> it's a logic error. I would guess this:
>
> > $sid = mysql_fetch_array($query) || die ('Could not fetch from database:
> > ' . mysql_error()); // <--- line 49
>
> isn't evaluating in the order you expect.. You're using a logic
> statement as an assignment, and the result of that get's assigned to your
> variable not the resource you expect. Try it like this to see if it works:
>
> $sid = mysql_fetch_array($query);
> if ($sid === false) die ('Could not fetch from database: ' . mysql_error());
>
> If that's not the problem, I haven't got a clue until after sleep and
> coffee.

I've had some sleep, and coffee, and I see I missed something silly.

I should of suggested this

// get sid and write cookies
$query = mysql_query("SELECT MAX(SID) FROM Sessions");
if ($query === false) die ('Could not query database: ' . mysql_error());
$sid = mysql_fetch_array($query)
if ($sid === false) die ('Could not fetch from database: ' . mysql_error());


--
Niel Archer

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Newbie alert: supplied argument is not a valid MySQL result resource

am 11.07.2007 22:27:57 von Matt Leonhardt

""lameck kassana"" wrote in message
news:967cd34f0707110248w50d29f31hde718e66e54c9e2a@mail.gmail .com...
> hey just make this
> $sid = mysql_fetch_array($query) or die ('Could not fetch from database:
>> ' ." mysql_error().");
> try this

Yep. I figured that out yesterday. Hit the nail right on the head. As
I've learned from experimentation, the pipe operator takes precedence to the
assignment operator so I was basically evaluating whether my query or a die
statement returned a true value (which the query statement was, so it never
processed the die()) and then assigning "true," or 1 to $query. The 'or'
operator by contrast processes after the assignment, so the following line:

$query = mysql_query("some SQL") or die();

first assigns a handle to $query (assuming the query is valid, which in my
case it was) and then evaluated the value of query. This leaves me with two
remaining questions. I'm coming from a Perl background, so I'm wondering if
there's a documentation page somewhere for PHP operators (something along
the likes of perlop) and as an experiment I tried the following code, and it
behaved exactly like the code I first posted, which I'm not sure why:

($query = mysql_query("valid SQL")) || die();

Shouldn't everything within the first set of parentheses here evaluate as
the first || condition?

Thanks for everyone's help,
Matt

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php