9小时前小编发现微信小程序用户:【翔宇】提出了一个关于“初学请教大家:php+mysql连接问题?”的问题,在问题中开发者翔宇对该问题做了如下阐述:
一个简单的登录画面,选择部门后,筛选该部门的用户,写这个getuser.php文件的时候,如果采用include "conn.php";这种方式连接数据库的话,电脑上测试是能读到用户给picker的,但是测试版或者真机调试时,picker只能读出一堆null了,但是真机调试,在Network菜单下看到数据确实已经取到了。如果把连接写死在这个文件里,就完全没问题。很疑惑,请帮忙看看.因为好些个文件都有类似错误,所以得改动多个文件比较麻烦,所以来求助大家
以下这样出错:
include "conn.php";
if (!$con){ die('Could not connect: ' . mysqli_connect_error());}
mysqli_query($con,"set names utf8");
$dept = $_GET['dept'];
$grade=$_GET['grade'];
$sql1=" select * from user where dept='$dept' and grade=1 order by username ;";
$result1 = mysqli_query($con,$sql1);
$data=$result1->fetch_all(PDO::FETCH_LAZY);
echo json_encode($data);
mysqli_close($con);
以下这样没问题:
header("Content-Type: text/html;charset=utf-8");
$con=mysqli_connect("150.xx.xx.xx","wx","xxxxxxxx","xxx");
if (!$con){ die('Could not connect: ' . mysqli_connect_error());}
mysqli_query($con,"set names utf8");
$dept = $_GET['dept'];
$grade=$_GET['grade'];
$sql1=" select * from user where dept='$dept' and grade=1 order by username ;";
$result1 = mysqli_query($con,$sql1);
$data=$result1->fetch_all(PDO::FETCH_LAZY);
echo json_encode($data);
mysqli_close($con);
conn.php如下:
header("Content-Type: text/html;charset=utf-8");
$con=mysqli_connect("150.xx.xx.xx","wx","xxxxxxx","xxx");
if (!$con)
{
die('Could not connect: ' . mysqli_connect_error());
}
mysqli_query($con,"set names utf8");
写picker部分是这样:
success: function (res) {
var array = res.data; //
var username = [];
var length = array.length;
for (var i = 0; i < length; i++) {
username.push(array[i].username)
}
if (username.length > 0) {
userpick = username[userindex];
}
that.setData({
array: array,
username: username,
userindex: userindex
})
}
注:以上是微信小程序开发者:"翔宇"对于本问题的一些阐述,这里做一个引用,我们将实时关注微信小程序助手对该问题提出的解决方案。
【12分钟前】微信小程序技术运营专员对用户翔宇提出的初学请教大家:php+mysql连接问题?给予如下回复
看了半天,唯一的区别就是 方案一多了个 mysqli_query($con,"set names utf8");
微信官方智能微信小程序将提供超过60个AI接口和超过20个NA化组件给开发人员,包括语音、文字、图像和人脸识别等。进入智能化时期,微信官方不是空喊口号,已经在大步实施。
如果您也在使用微信小程序,请关注我们,如果您有关于微信小程序的使用问题请关注我们,如果您要找百度小程序模板、微信小程序模板请关注小程序模板网或者去微信开放社区找官方人员给予解决,相信微信官方在小程序上的发力会是一个里程碑!