1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I am trying to load/generate the link stored inside a cell from my sql table via php. But I am getting an error:

Warning: simplexml_load_file() expects parameter 1 to be string, resource given in ... on line 78

Could you guys please tell me where I did wrong?

Here is my code:

$sql = "SELECT link FROM previousbroadcast WHERE id=21";             
$result=mysql_query($sql);

$xml = simplexml_load_file($result);                            
Community
  • 1
  • 1

6 Answers6

0

$result variable is a SQL Result set, not a string. You have to use a function like mysql_fetch_array to get the value of the individual columns from your SQL results.

Perhaps something like:

$sql = "SELECT link FROM previousbroadcast WHERE id=21";
$result=mysql_query($sql);

while($row = mysql_fetch_array($result)){
    $xml = simplexml_load_file($row[0]);
    break;
}
user470714
  • 2,858
  • 1
  • 28
  • 34
0

mysql_query returns a statement handle. You have to fetch a row from that handle and extract the value:

$sql = "SELECT ..."
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$xml = simplexml_load_file($row['link']);
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

$result is not the value of the link field in your table, but the ressource id. You want something like

$sql = "SELECT link FROM previousbroadcast WHERE id=21";
$ressource=mysql_query($sql);  // remember, it's just the ressource
$result = mysql_fetch_array($ressource); // let's get this into an associative array

$xml = simplexml_load_file($result['link']); // and use the respective field in the array to fetch the XML

Be aware that this is very simple code, and you should make sure that the file exists before you try to load it...

ty812
  • 3,293
  • 19
  • 36
  • thank you so much for helping. i tried this, but instead of loading the link in only row 21, it loads all the links present in the table. why is that? – questiongirl Aug 25 '11 at 18:27
  • Without seeing more of your code, there is no way to tell. – ty812 Aug 25 '11 at 18:32
0

You need to get the result first. In your $result variable you only have a SQL resource stored. To get the actual content of the query, use $result_array = mysql_fetch_array($result) which will return an array with your data. You can then call the simplexml_load_file($result_array[0]) function.

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
0

mysql_query returns a resource. use mysql_fetch_array to get to the content. They have examples in the documentation.

Andrea
  • 1,057
  • 1
  • 20
  • 49
0
$sql = "SELECT link FROM previousbroadcast WHERE id = '21'";

$result = mysql_query($sql);
if(mysql_num_rows($result) >= 1) {
$row = mysql_fetch_array($result);
$xml = simplexml_load_file($row['previousbroadcast']);
}

Firstly, you should encapsulate your SQL parameters with single quotes ('). Secondly using mysql_query only returns a handle, you need to actually generate an array of that result using mysql_fetch_array(). You should also add in some error-catching as shown above too.

Ashley
  • 1,459
  • 2
  • 12
  • 25