Rumah > pembangunan bahagian belakang > tutorial php > php通过query查询两个字段的信息就会无法找到网页

php通过query查询两个字段的信息就会无法找到网页

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-23 13:26:55
asal
1040 orang telah melayarinya

<?phpheader("Content-Type:text/html;charset=utf-8");try{$pdo=new pdo('mysql:host=localhost;dbname=lsj','root','lsjhxl123') or die("链接失败");;$pdo->setAttribute(PDO::ATTR_ERRMODE,2);$aa=$pdo->query("set names utf8");//$pdo->exec("insert into test values(2,'bb','cccccc')") or die("失败");$res=$pdo->query("select name from test where id = 11");//修改为查询 name,id两个字段,就错误}catch(PDOException $err){	echo $err->getMessage();}$result=$res->fetchall(PDO::FETCH_ASSOC);print_r($result);;?>
Salin selepas log masuk


执行这个可以正确得到结果Array ( [0] => Array ( [name] => aa ) )。
但是如果select name from test where id = 11变成select name,id from test where id = 11,,就直接连不上localhost,无法找到网页了


回复讨论(解决方案)

你用ID查询的,还要显示ID.你这个id不是主键?

其它字段呢?只要两个任意字段都有问题?

你用ID查询的,还要显示ID.你这个id不是主键?





其它字段呢?只要两个任意字段都有问题?



没有设置主键,只有select × from test 也是查不出来;
任意单个字段都行,任任意多个字段都不行

试试重新设计下你的表结构。。。你的建表语句拉出来看看

你直接跑sql语句可以吗?不用通过php代码来执行

name id中间是不是误用中文标点了

你这个逗号不对呦

header("Content-Type:text/html;charset=utf-8");try{$pdo=new pdo('mysql:host=localhost;dbname=test','root','root') or die("链接失败");;$pdo->setAttribute(PDO::ATTR_ERRMODE,2);$aa=$pdo->query("set names gbk");//$pdo->exec("insert into test values(2,'bb','cccccc')") or die("失败"); $res=$pdo->query("select id, goodprice from price where id = 15");//修改为查询 name,id两个字段,就错误}catch(PDOException $err){    echo $err->getMessage();} $result=$res->fetchall(PDO::FETCH_ASSOC);print_r($result);
Salin selepas log masuk
Salin selepas log masuk


上面的代码你写的代码测试没有问题啊

1.先确定你数据库中是否有id字段。
2.使用这句试,估计你的写法混杂了中文符号。

$res=$pdo->query("select id,name from test where id=11");
Salin selepas log masuk

header("Content-Type:text/html;charset=utf-8");try{$pdo=new pdo('mysql:host=localhost;dbname=test','root','root') or die("链接失败");;$pdo->setAttribute(PDO::ATTR_ERRMODE,2);$aa=$pdo->query("set names gbk");//$pdo->exec("insert into test values(2,'bb','cccccc')") or die("失败"); $res=$pdo->query("select id, goodprice from price where id = 15");//修改为查询 name,id两个字段,就错误}catch(PDOException $err){    echo $err->getMessage();} $result=$res->fetchall(PDO::FETCH_ASSOC);print_r($result);
Salin selepas log masuk
Salin selepas log masuk


上面的代码你写的代码测试没有问题啊



最后也不知道为什么,重装了Apache,php,mysql,最后奇葩的好了
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan