Why Do I Get "Resource Id #4" When I Apply Print_R() to an Array in PHP

Why do I get Resource id #4 when I apply print_r() to an array in PHP?

You are trying to print a mysql resource variable instead of the values contained within the resource it references. You must first try to extract the values you have gotten by using a function such as mysql_fetch_assoc().

You might also try mysql_fetch_array() or mysql_fetch_row(), but I find associative arrays quite nice as they allow you to access their values by the field name as in Mike's example.

PHP retrieving data, but returns Resrouce id#4

Try

if(sqlsrv_fetch_array($stmt) ===false){

Instead of

if(sqlsrv_fetch($stmt) ===false){

Ref:

http://php.net/manual/en/function.sqlsrv-fetch-array.php

http://www.php.net/manual/en/function.sqlsrv-fetch.php

get active resource by the unique id

On PHP7 you could use the code below to fetch a resource given an id:

function fetchResourcePHP7($id){
if(!is_int($id)) return false;
foreach(get_resources() as $v){
if(intval($v)==$id) return $v;
}
return false;
}

On PHP5 you can use this custom recursive function to achieve the same thing

function fetchResource5($id,$array=array(),$restart=true){
static $result=false;
if($restart) $result=false;
if(is_resource($result)) return $result;
if(!is_array($array)) return false;
if(!is_int($id)) return false;
$avoid=['_GET'=>0,'_POST'=>0,'_FILES'=>0,'_REQUEST'=>0,'GLOBALS'=>0,'_COOKIE'=>0];
if(!$array){
foreach($GLOBALS as $k=>$v){
if(is_resource($v)&&intval($v)==$id){
$result=$v;
}
elseif(is_array($v)&&!isset($avoid[$k])){
fetchResource($id,$v,false);
}
}
}
else{
foreach($array as $k=>$v){
if(is_resource($v)&&intval($v)==$id){
$result=$v;
}
elseif(is_array($v)&&!isset($avoid[$k])){
fetchResource($id,$v,false);
}
}
}
return $result;

}

It looks into the GLOBALS variable or into a given $array to retrieve the expected resource if exists.

The usage in function is simple:

function any($var){
$onevar='';
$anothervar='';
//an useless example of fetch row here,you could provide $GLOBALS array instead of local variables
$someresource=fopen('sometext.txt','r');
$getresource=fecthResource5(5,get_defined_vars());//always ignore the third argument ,the id 5 is just an example
return true;
}

in the global context the usage is even more simple:

 var_dump(fetchResource5(36));//if a resource with id 36 exists it will dump it

How do i echo a Resource id #6 from a MySql response in PHP?

You need to use a fetch function. for example:

$result = mysql_query(sprintf("SELECT TIMEDIFF(NOW(), '%s') as time_delta", $row['fecha']));
if($result){
$data = mysql_fetch_assoc($result);
echo $data['time_delta'];
}

However, i wouldnt use the mysql functions unless absolutely necessary. the mysql extension is NOT recommended for use in new projects. Instead you should use PDO with PDO_mysql or mysqli.

Figuring out why I am getting a Resource ID #5 error

This is not an error. This is similar to when you try to print an array without specifying an index, and only the string "Array" is printed. You can access the actual data contained within that resources (which you can think of as a collection of data) using functions like mysql_fetch_array().

In fact, if there were an error here, the value of $id would not be a resource. I usually use the is_resource() function to verify that everything is alright before using variables which are supposed to contain a resource.

I guess what you intend to do is this:

$result = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
if(is_resource($result) and mysql_num_rows($result)>0){
$row = mysql_fetch_array($result);
echo $row["id"];
}

Understanding array notation (specifically mysql_fetch_array and print_r)

Associative arrays use strings for the index, numeric arrays use numbers.

mysql_fetch_array() creates an associative array containing numeric keys as well so that both may be used to access the array.

mysql_fetch_assoc() creates an array containing only the string indices.

mysql_fetch_row() creates an entirely numeric array, and is the fastest to execute of these functions.



Related Topics



Leave a reply



Submit