有没有人知道从mysqli的结果集中设置这个contacts数组的更简单方法?也有人有任何好的mysqli插入PHP函数(程序),如果SQL查询中有语法错误,很容易调试吗?
$result = mysqli_query($link, 'SELECT id, name, contactNo, email, address, county, country, sourceOfContact, type FROM contacts');
while ($row = mysqli_fetch_array($result))
{
$contacts[] = array('id' => $row['id'],
'name' => $row['name'],
'contactNo' => $row['contactNo'],
'email' => $row['email'],
'address' => $row['address'],
'county' => $row['county'],
'country' => $row['country'],
'sourceOfContact' => $row['sourceOfContact'],
'type' => $row['type']
}
答案 0 :(得分:4)
只需阅读相应的PHP手册页和
$result = mysqli_query($link, 'SELECT id, name, contactNo, email, address, county, country, sourceOfContact, type FROM contacts');
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$contacts[] = $row;
}
....如果你看起来很神奇,你可以在手册中看到
答案 1 :(得分:0)
$result = mysqli_query($link, 'SELECT id, name, contactNo, email, address, county, country, sourceOfContact, type FROM contacts');
while ($row = mysqli_fetch_array($result))
{
$contacts[] = $row;
}
要好得多,你会得到相同的结果。
另外,关于你的问题,只需创建一个包装器函数并始终使用它,例如db_mysqli_query(...),它会根据你的意愿自动执行查询的错误处理。当你想进行查询时,只需记住调用查询函数而不是内置的mysql查询函数。
P.S。你的问题有点难以理解,而且有点过于宽泛的问题要问。但无论如何我尽力回答。