如何在下面的代码中将mysql_num_fields()转换成面向对象的php。
$objConnect = mysql_connect("localhost","root","john123");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM `cman_tickets`";
print_r($strSQL);
//$strSQL = "SELECT * FROM `cman_tickets`";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
echo json_encode($resultArray);发布于 2015-02-19 14:59:46
要使用面向对象的php,你必须使用mysqli而不是mysql。
mysql_num_fields() 可以替换为
mysqli_result::$field_count由于mysql_*函数已被弃用,因此最好避免这种情况
$objConnect = new mysqli("localhost","root","john123","mydatabase");
$strSQL = "SELECT * FROM `cman_tickets`";
print_r($strSQL);
$objQuery = $objConnect->query($strSQL);
$intNumField = $objQuery->field_count; // to get the field count
$resultArray = array();
while($obResult = $objQuery->fetch_array())
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[$objQuery->fetch_field_direct($i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
echo json_encode($resultArray);https://stackoverflow.com/questions/28600226
复制相似问题