For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > July 2007 > Newbie alert: supplied argument is not a valid MySQL result resource









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2007-07-10, 3:58 am

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)
<?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)
<?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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com