开发者

How to load the link stored in a cell from sql table PHP [duplicate]

This question already has answers here: 开发者_运维技巧 Closed 10 years ago.

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);                            


$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...


$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;
}


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']);


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.


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


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜