PHP执行SQL语句的问题

chengdong284 2009-03-17 10:51:55
我在PHP执行一条SQL语句,执行不了,可我把它打印出来,直接在PHPMYADMIN中执行,就可以执行
这是怎么回事
哪位高人帮帮忙
...全文
640 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sese53 2009-03-17
  • 打赏
  • 举报
回复
朋友,这个问题以前我也遇到过。这个是你的字符没有转成mysql的校对字符,
你可以在链接后加上mysql_query("set names utf-8")等类似的语句,就成功了。
chengdong284 2009-03-17
  • 打赏
  • 举报
回复
<?php
set_time_limit(0);

$page = (int)$_REQUEST["page"];
$page = $page>1?$page:1;
$num = 30; //每次导入100条数据
$temp = ($page-1)*$num;
$mssql_db= 'Dbtest1'; //odbc 数据库
$mssql_user = 'sa'; //odbc 数据库用户名
$mssql_pwd = 'admin'; //odbc 数据库用户密码
$mssql_server = 'localhost';

$db_name = 'Dbtest'; //SKYUC PHP数据库
$db_user = 'admin'; //SKYUC PHP数据库用户名
$db_pwd = 'admin';//SKYUC PHP数据库用户密码
*/
$msconn = odbc_connect("Driver={SQL Server};Server=$mssql_server;Database=$mssql_db",$mssql_user,$mssql_pwd) OR die("数据连接出错");
$count = odbc_exec($msconn, "SELECT count(softID) C FROM PE_Soft INNER JOIN PE_DownServer ON PE_Soft.ChannelID = PE_DownServer.ChannelID where PE_Soft.Status = 3 and PE_Soft.Deleted = 0 and PE_Soft.channelid in (1002,1003,1004,1005,1006,1007,1008,1010,1013)" );
while(odbc_fetch_row($count))
{
$count = odbc_result($count,'C');
}
$nums = ($num*$page*100)/($count);
$nums = round($nums,2);
echo "共".$count."条数据,已经导入".$nums."%";
$cur= odbc_exec($msconn, "SELECT TOP $num PE_Soft.*,PE_DownServer.ServerUrl FROM PE_Soft INNER JOIN PE_DownServer ON PE_Soft.ChannelID = PE_DownServer.ChannelID where PE_Soft.Status = 3 and PE_Soft.Deleted = 0 and PE_Soft.channelid in (1002,1003,1004,1005,1006,1007,1008,1010,1013) AND SoftID NOT IN (SELECT TOP $temp SoftID FROM PE_Soft order by SoftID ASC) " );
$num_row=0;
$conn=mysql_pconnect("localhost", $db_user, $db_pwd);
@mysql_select_db($db_name,$conn) or die("无法连接到数据库,请与管理员联系!");
mysql_query("SET character_set_connection='utf8', character_set_results='utf8', character_set_client=binary");
echo "<b>数据库链接成功!</b>" ."<br>";
while(odbc_fetch_row($cur))
{
$num_row++;
$contentid = odbc_result($cur, 'SoftID');
$catid = odbc_result($cur, 'classid');
$title = odbc_result($cur, 'softname');
$keywords = odbc_result($cur, 'keyword');
$decription = odbc_result($cur, 'SoftIntro');
$decription = htmlspecialchars($decription);
$decription = str_replace('"','\\"',$decription);
$decription = str_replace("'",'\\"',$decription);
$decription = str_replace(".",' ',$decription);
$readpoint = odbc_result($cur, 'InfoPoint');
$copytype = $readpoint > 0?'收费版':'免费版';
$filesize = odbc_result($cur, 'SoftSize');
$downserver = odbc_result($cur, 'ServerUrl');
$downurls = odbc_result($cur, 'DownloadUrl');
$downurls = str_replace('@@@',"$title|$downserver",$downurls);;

$sqls = 'SELECT COUNT(*) FROM phpcms_content'. " WHERE contentid='".$contentid."'";
$res = mysql_query($sqls,$conn);
$row = mysql_fetch_row($res);
if($row[0]>0)
{
echo "<font color=red>资料:$title 已存在,跳过!</font><br>";
}
else
{
$sql1 ="INSERT INTO table1 (contentid, template, content, version, classtype, language, copytype, systems, stars, filesize, downurl, downurls, groupids_view, readpoint) VALUES ($contentid , ' ', '$decription', ' ', '国产软件', '简体中文', '$copytype', 'Win2000/WinXP/Win2003', '★★★☆☆', $filesize, ' ', '$downurls', 1, '$readpoint');";
$sql = "INSERT INTO table2 ( contentid, catid, typeid, areaid, title, style, thumb, keywords, description, posids, url, listorder, status, userid, username, inputtime, updatetime, searchid, islink, prefix) VALUES
( $contentid, $catid, 0, 0, '$title', ' ', ' ', '$keywords', ' ', 0, 'show.php?contentid=$contentid', 0, 99, 1, 'phpcms', '1235631364', '1235631364', 0, 0, ' ');";

//echo $sql;
if(mysql_query($sql,$conn))
{
echo "正在导入……资料:<font color=blue>" .$title."</font><font color=green>"."</font>……";
}
else
{
echo "$sql<br>";
}
if(mysql_query($sql1,$conn))
{
echo "正在导入……下载:<font color=blue>" .$title."</font><font color=green>"."</font>……<font color=green>OK!</font><br>";
}
else
{
echo $sql1;
break;
echo "正在导入……下载:<font color=blue>" .$title."</font><font color=green>"."</font>……<font color=red>FAIL!</font><br>";
}

}
if($num_row>=$num)
{
$page++;
echo "<script>window.setTimeout(\"window.location.href='?page=$page'\",1000);</script>";

}
}


?>
SQL语句打印出来在PHPMYADMIN中是可以执行的
而且这中间也只有部分不能执行,大部分是没有问题的
jackydrl 2009-03-17
  • 打赏
  • 举报
回复
如果你把echo 出来的SQL放在phpmyadmin中去执行,并且成功的话,只能说你php的mysql没有配置好!
看看是配置参数,还是mysql连接,执行等等的哪儿块出问题了

还有就是如果执行不了,应该会有错误提示的啊,楼主要是把提示贴出来,肯定很快就会解决的

如果没有错误提示,那肯定PHP的mysql操作对象就没建成功,或根本没有数据更新
ligy19840101 2009-03-17
  • 打赏
  • 举报
回复
把代码贴出来吧
wkl1985411 2009-03-17
  • 打赏
  • 举报
回复
如果确保数据库连接没问题的话,应该就是编码的问题了。你可以只用英文字母试试。
mengshan1986 2009-03-17
  • 打赏
  • 举报
回复
show variables like 'character%';
查看编码如果是gb2312,改为gbk就可以解决繁体问题!
chengdong284 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sese53 的回复:]
朋友,这个问题以前我也遇到过。这个是你的字符没有转成mysql的校对字符,
你可以在链接后加上mysql_query("set names utf-8")等类似的语句,就成功了。
[/Quote]

我加上了以后,也还是不行啊,特别是遇到繁体字时

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧