Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /www/web/default/caiji.php on line 5
请问高手,如何解决?caiji.php 代码如下
<?php
include("caiji_utils.php");
$DataUtils = new DataListUtils;
$datalist = $DataUtils->getDataList();
while($Row = mysql_fetch_assoc($datalist))
{
$res= $DataUtils->insertArticle($Row["title"],$Row["content"]);
if($res==-1){
print ("[ERROR]error import:". $Row["title"]);
$DataUtils->updateErrorTag($Row["url_hash"]);
continue;
}else{
$DataUtils->thumbImage($res,$Row["target_img"],$Row["target_url"]);
$DataUtils->category($res,$Row["b2c"],$Row["category"]);
$DataUtils->updateTag($Row["url_hash"]);
}
}
caiji_utils.php代码如下。
<?php
class DataListUtils{
var $hostname = '127.0.0.1';
var $userName = 'a0966634701';
var $password = '78235712';
function getDataList(){
session_start();
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "SELECT * from dxb_contents where status<>1 and status<>2";
$res = mysql_query($sql);
mysql_close($con);
return $res;
}
function getUpdateDataList(){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "SELECT * from dxb_contents where status=2";
$res = mysql_query($sql);
mysql_close($con);
return $res;
}
function updateTag($url_hash){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$res = mysql_query("update dxb_contents set status=1 where url_hash='".$url_hash."'");
mysql_close($con);
return $res;
}
function updateErrorTag($url_hash){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$res = mysql_query("update dxb_contents set status=3 where url_hash='".$url_hash."'");
mysql_close($con);
return $res;
}
function removeAll(){
session_start();
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "delete * from dxb_contents";
$res = mysql_query($sql);
mysql_close($con);
}
function thumbImage($id,$image,$url){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$thumbValue = "thumb_value";
$thumbUrl="buylink_value";
$sql = "INSERT INTO `wp_postmeta` ( `post_id`, `meta_key`, `meta_value`) VALUES
( ".$id.", '".$thumbValue."', '".$image."'),
( ".$id.", '".$thumbUrl."', '".$url."');";
$res = mysql_query($sql);
mysql_close($con);
}
function category($id,$b2cCategoryId,$categoryId){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES
(".$id.", ".$b2cCategoryId.", 0),
(".$id.", ".$categoryId.", 0);";
$res = mysql_query($sql);
mysql_close($con);
}
function insertArticle($title,$content){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "INSERT INTO `wp_posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES
( 2, sysdate() , utc_date(), '".$content."', '".$title."', '', 'publish', 'open', 'open', '', 'hello-world', '', '', sysdate() , utc_date(), '', 0, '', 0, 'post', '', 0)
";
$res = mysql_query($sql,$con);
if($res==1){
$insertId = mysql_insert_id($con);
mysql_close($con);
return $insertId;
}
else{
mysql_close($con);
return -1;
}
}
}
?>
请问高手,如何解决?caiji.php 代码如下
<?php
include("caiji_utils.php");
$DataUtils = new DataListUtils;
$datalist = $DataUtils->getDataList();
while($Row = mysql_fetch_assoc($datalist))
{
$res= $DataUtils->insertArticle($Row["title"],$Row["content"]);
if($res==-1){
print ("[ERROR]error import:". $Row["title"]);
$DataUtils->updateErrorTag($Row["url_hash"]);
continue;
}else{
$DataUtils->thumbImage($res,$Row["target_img"],$Row["target_url"]);
$DataUtils->category($res,$Row["b2c"],$Row["category"]);
$DataUtils->updateTag($Row["url_hash"]);
}
}
caiji_utils.php代码如下。
<?php
class DataListUtils{
var $hostname = '127.0.0.1';
var $userName = 'a0966634701';
var $password = '78235712';
function getDataList(){
session_start();
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "SELECT * from dxb_contents where status<>1 and status<>2";
$res = mysql_query($sql);
mysql_close($con);
return $res;
}
function getUpdateDataList(){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "SELECT * from dxb_contents where status=2";
$res = mysql_query($sql);
mysql_close($con);
return $res;
}
function updateTag($url_hash){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$res = mysql_query("update dxb_contents set status=1 where url_hash='".$url_hash."'");
mysql_close($con);
return $res;
}
function updateErrorTag($url_hash){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$res = mysql_query("update dxb_contents set status=3 where url_hash='".$url_hash."'");
mysql_close($con);
return $res;
}
function removeAll(){
session_start();
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "delete * from dxb_contents";
$res = mysql_query($sql);
mysql_close($con);
}
function thumbImage($id,$image,$url){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$thumbValue = "thumb_value";
$thumbUrl="buylink_value";
$sql = "INSERT INTO `wp_postmeta` ( `post_id`, `meta_key`, `meta_value`) VALUES
( ".$id.", '".$thumbValue."', '".$image."'),
( ".$id.", '".$thumbUrl."', '".$url."');";
$res = mysql_query($sql);
mysql_close($con);
}
function category($id,$b2cCategoryId,$categoryId){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES
(".$id.", ".$b2cCategoryId.", 0),
(".$id.", ".$categoryId.", 0);";
$res = mysql_query($sql);
mysql_close($con);
}
function insertArticle($title,$content){
$con=mysql_connect($this->hostname,$this->userName,$this->password) or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('diaoxb');
$sql = "INSERT INTO `wp_posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES
( 2, sysdate() , utc_date(), '".$content."', '".$title."', '', 'publish', 'open', 'open', '', 'hello-world', '', '', sysdate() , utc_date(), '', 0, '', 0, 'post', '', 0)
";
$res = mysql_query($sql,$con);
if($res==1){
$insertId = mysql_insert_id($con);
mysql_close($con);
return $insertId;
}
else{
mysql_close($con);
return -1;
}
}
}
?>
应该是查询失败了!
while($Row = mysql_fetch_assoc($datalist))
这个里面,报错应该就是这个位置.你把$datalist 打印一下,应该没有获取到数据,所以才会报错的!
print_r($datalist); 看看有什么。
$datalist = $DataUtils->getDataList();
var_dump($datalist);