php + msqli 面向对象方式读取并显示数据问题
看清楚要求:1,是mysqli,不是mysql;2,是面向对象的方式;3,id是唯一的,我只需要按照这个条件来显示出来;代码内容如下:------------------...
看清楚要求:
1,是 mysqli,不是mysql;
2,是面向对象的方式;
3,id是唯一的,我只需要按照这个条件来显示出来;
代码内容如下:
--------------------------------------------------
<?php
//数据库连接信息
$dbhost = 'localhost';
$dbname = 'data';
$dbuser = 'root';
$dbpwd = '123456';
date_default_timezone_set('Asia/Shanghai');
$conn = new mysqli($dbhost, $dbuser, $dbpwd, $dbname);
$conn->set_charset('utf8');
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$id=12;
$sql = 'select * from [users] where ID='.$Id.' limit 1';
if ($rs = $conn->query($sql)) {
while($row = $rs->fetch_array(MYSQL_ASSOC)){
$id = $row["id"];
$cd = $row["cd"];
$dd = $row["dd"];
$gp = Trim($row["gp"]);
$rs->close();
}
}
echo $id . $cd . $dd . $gp
//输出是空的,数据库中却有数据,除id为数字型,其他都为字符型
?> 展开
1,是 mysqli,不是mysql;
2,是面向对象的方式;
3,id是唯一的,我只需要按照这个条件来显示出来;
代码内容如下:
--------------------------------------------------
<?php
//数据库连接信息
$dbhost = 'localhost';
$dbname = 'data';
$dbuser = 'root';
$dbpwd = '123456';
date_default_timezone_set('Asia/Shanghai');
$conn = new mysqli($dbhost, $dbuser, $dbpwd, $dbname);
$conn->set_charset('utf8');
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$id=12;
$sql = 'select * from [users] where ID='.$Id.' limit 1';
if ($rs = $conn->query($sql)) {
while($row = $rs->fetch_array(MYSQL_ASSOC)){
$id = $row["id"];
$cd = $row["cd"];
$dd = $row["dd"];
$gp = Trim($row["gp"]);
$rs->close();
}
}
echo $id . $cd . $dd . $gp
//输出是空的,数据库中却有数据,除id为数字型,其他都为字符型
?> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询