如果有人不能提示我如何用mysqli_query返回资源而不是对象,因为每当我在mysqli_field_name或mysql_fetch_row中使用它时,它都会返回这个错误"mysql_field_name()期望参数1是资源,给定的对象“.And记住你不是真正熟悉mysqli的methods.Some帮助将是appriciated.Below。
<?php
$dns="mysql:dbname=name";
$username="name";
$password="";
$con = new mysqli('localhost', 'root', '', 'oysg');
$export=mysqli_query($con,"SELECT * FROM applicant");
$header = '';
$data ='';
// $export = $stmt;
// extract the field names for header
$fields = 25;
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
echo $header;
}
echo '<br>';
echo '<br>';
// foreach ( $fields2 as $headers )
// {
// $header .= $headers. "\t";
// echo $header;
// }
// export data
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\nNo Record(s) Found!\n";
}
// allow exported file to download forcefully
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xml");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?> 提前感谢
发布于 2014-10-04 00:28:29
您将OOP和过程化混合在一起。
您在编写以下代码时使用OOP:
$con = new mysqli('localhost', 'root', '', 'oysg');$con现在是具有成员属性和方法的mysqli实例。
但是,使用如下语法:
$export=mysqli_query($con,"SELECT * FROM applicant")不是OOP做事情的方式。使用任何mysqli_*函数都意味着您的$con是以下结果的结果:
$con = mysqli_connect('localhost', 'root', '', 'oysg');如果你想坚持使用OOP,我相信你会追求下面的mysqli_query等价物:
$export = $con->query("SELECT * FROM applicant");其中$export现在是an instance of mysqli_result。
使用OOP风格也应该有助于扩展的混合和匹配,例如mysqli_query和mysql_fetch_name,后者在mysqli中缺少i。
https://stackoverflow.com/questions/26182873
复制相似问题