mysql_fetch_array() expects parameter 1 to be resource ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

mysql_fetch_array() expects parameter 1 to be resource ???

Post by harshilshah »

heres my php god

Code: Select all

<?php
set_include_path("." . PATH_SEPARATOR . get_include_path());
require 'ArchiveExtractor.class.php';
 
// database code
$dbh=mysql_connect ("localhost", "root",
"") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("wwwgodn_godnels");
echo "Connection Successfull";
 
$sql="SELECT fldUsername FROM tblusers where fldUsername='".$_POST["txtusername"]."'";
$result = mysql_query($sql);
 
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
echo "Username already exists!!!";
 exit(1);}
 
mysql_free_result($result);
 
$rs = @mkdir( $_POST["txtusername"], 0777 );
echo "make directory sucessfull";
 
if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "application/x-rar-compressed")|| ($_FILES["file"]["type"] == "application/rar")
|| ($_FILES["file"]["type"] == "application/octet-stream")|| ($_FILES["file"]["type"] == "application/zip")
||($_FILES["file"]["type"] == "application/x-zip-compressed")||($_FILES["file"]["type"] == "application/octet-stream")
||($_FILES["file"]["type"] == "application/x-compress")||($_FILES["file"]["type"] == "application/x-compressed")
||($_FILES["file"]["type"] == "multipart/x-zip")|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    //echo "Type: " . $_FILES["file"]["type"] . "<br />";
    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists($_REQUEST["txtusername"]. "/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {move_uploaded_file($_FILES["file"]["tmp_name"],
      $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"]);
      echo "Stored in: " . $_REQUEST["txtusername"]."/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  
  if ((($_FILES["file"]["type"] == "application/zip")
|| ($_FILES["file"]["type"] == "application/rar")
|| ($_FILES["file"]["type"] == "application/zip")
||($_FILES["file"]["type"] == "application/x-zip-compressed")
||($_FILES["file"]["type"] == "application/octet-stream")
||($_FILES["file"]["type"] == "application/x-compress")
||($_FILES["file"]["type"] == "application/x-compressed")
||($_FILES["file"]["type"] == "multipart/x-zip")
|| ($_FILES["file"]["type"] == "application/octet-stream")))
{
    $archExtractor=new ArchiveExtractor();
    $extractedFileList=$archExtractor->extractArchive($_REQUEST["txtusername"]."/" . $_FILES["file"]["name"],$_REQUEST["txtusername"]."/");
    chmod($_POST["txtusername"]."/" . $_FILES["file"]["name"],0777);
    unlink($_POST["txtusername"]."/" . $_FILES["file"]["name"]);
    echo "album submitted";
    
}
$sql="Insert Into tblusers (fldUsername,fldPassword) Values ('".$_POST["txtusername"]."','".$_POST["txtpassword"]."')";
 
$result = mysql_query($sql);
mysql_close($dbh);
?>

here's the error i get

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\TEST\upload.php on line 16

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\TEST\upload.php on line 20

Secondly the data is not inserted into the database. but the new directory with user name is created and the zip file is uploaded and extracted to that folder. but archive file is not deleted after extraction of the file is done.
and the user id and password are not added to the database table
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: mysql_fetch_array() expects parameter 1 to be resource ???

Post by jackpf »

You do actually need to use mysql_query() when running a query.
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: mysql_fetch_array() expects parameter 1 to be resource ???

Post by DaiLaughing »

I'm not sure what jackpf is talking about but it might be worth doing some error checking on the $result = mysql_query($sql); line. I suspect that query is failing for some reason. I still use:

Code: Select all

$result = mysql_query($sql) or die("Query to get blah failed with error: ".mysql_error());
but have been told I should use better error checking. For this it won't matter.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: mysql_fetch_array() expects parameter 1 to be resource ???

Post by superdezign »

@harshilshah: mysql_query() returns a resource when successful or the value FALSE when there is an error. You should make use of the is_resource() function and the mysql_error() function.

Code: Select all

$result = mysql_query($query);
if (!is_resource($result)) {
    // There has been an error
    echo mysql_error();
}
Also, as a side note, mysql_array() uses MYSQL_BOTH as the default for the second parameter. Specifying that is unnecessary.
And mysql_free_result() is also unnecessary. All results are freed after the page is done loading. The function is there for when you may need to run a large amount of queries in one page load and memory is an issue.

@jackpf: The OP did use mysql_query(). Nice try, though.

@DaiLaughing: "Better error checking" might mean that you need to have more validation. If it actually is in reference to the error checking, the problem is likely the fact that you print the error to the screen. Normally, I have my errors sent to the database unless the current user is myself, the highest level administrative user. Printing the error exposes your query, and exposing your query exposes your databases. Obscurity is an extra layer of security that one should never underestimate.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: mysql_fetch_array() expects parameter 1 to be resource ???

Post by jackpf »

Ahh yeah, I didn't even see that. My mom woke me up early this morning :oops:

You have my profuse apologies.

Yeah, if I had have seen that, I would suggest you stick "or die(mysql_error())" on the end of the query, and upon doing so you would probably be greeted with an error...which, as superdezign explained so beautifully explained, will return FALSE, rather than a valid resource handler...meaning you get that error.

Guess I'm too late now though... :x
harshilshah
Forum Newbie
Posts: 16
Joined: Fri Jul 17, 2009 5:13 am

Re: mysql_fetch_array() expects parameter 1 to be resource ???

Post by harshilshah »

by adding this code

Code: Select all

$result = mysql_query($sql) or die("Query to get blah failed with error: ".mysql_error());
the code was working fine.
Thank you everyone for your help.
Post Reply