Speechless error, mysql_fetch_array () MySQL PHP database connection query
The code is as follows. please take a look!
GetConn ();} public function getConn () {$ db = require_once 'config/config. php '; $ this-> dbhost = $ db ['dbhost']; $ this-> dbuser = $ db ['dbuser']; $ this-> dbpsw = $ db ['dbpsw']; $ this-> dbchar = $ db ['dbchar ']; $ this-> dbname = $ db ['dbname']; $ this-> tablepre = $ db ['tablepre']; $ this-> conn = mysql_connect ($ this-> dbhost, $ this-> dbuser, $ this-> dbpsw) or die (mysql_error ()."
Mysql connection failed! "); Mysql_select_db ($ this-> dbname, $ this-> conn) or die (mysql_error ()."
Database access error "); mysql_query (" set names ". $ this-> dbchar, $ this-> conn);}/*** run SQL */public function query ($ SQL) {return mysql_query ($ SQL, $ this-> conn) or die (mysql_error ()."
SQL execution error: $ SQL ");}/*** multiple records returned */public function getDataArrays ($ SQL, $ type = MYSQL_BOTH) {$ result = $ this-> query ($ SQL); $ refArr = array (); while ($ row = mysql_fetch_array ($ result, $ type )) {$ refArr [] = $ row;} return $ refArr;}/*** close Database link */public function closeConn () {mysql_close ($ this-> conn );}}
The call is as follows:
$conn = new Conn();$sql = "select * from qj_content";$contentList = $conn->getDataArrays($sql,MYSQL_ASSOC);$conn->closeConn();
The execution result is as follows:
Warning: mysql_fetch_array (): supplied argument is not a valid MySQL result resource in D: \ AppServ \ www \ qjcentury \ conn. php on line 45
45th lines of code are marked in red.
Reply to discussion (solution)
The piece marked in red is converted into php code. I'm dizzy.
Yes /**
* Multiple records are returned.
*/
Public function getDataArrays ($ SQL, $ type = MYSQL_BOTH ){
$ Result = $ this-> query ($ SQL );
$ RefArr = array ();
While ($ row = mysql_fetch_array ($ result, $ type )){
$ RefArr [] = $ row;
}
Return $ refArr;
}
Why don't anyone come?
No one has asked me a few questions, whether simple or difficult,
When has CSDN become so deserted?
It is estimated that an SQL problem has occurred.
Echo mysql_error (); on while to see
LZ: Let's take a look at the W3C example above.
Syntax
Mysql_fetch_array (data, array_type)
Parameter description
Data is optional. Specifies the data pointer to be used. This data pointer is produced by the mysql_query () function.
Array_type
Optional. Specifies the result to be returned. Possible values:
MYSQL_ASSOC-associated array
MYSQL_NUM-number array
MYSQL_BOTH-default. Generate join and numeric arrays at the same time
Change the query method to this.
Public function query ($ SQL) {$ rs = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ()."
SQL execution error: $ SQL "); return $ rs ;}
Because mysql_query ($ SQL, $ this-> conn) or die (mysql_error ()."
SQL execution error: $ SQL ")
It is a logical expression and can only be a logical value if it is directly returned.
So write
$ Rs = mysql_query ($ SQL, $ this-> conn) or die (mysql_error ()."
SQL execution error: $ SQL ");
Because the = priority is higher than or, $ rs = mysql_query ($ SQL, $ this-> conn) is executed first.
Formula changed
$ Rs or die (mysql_error ()."
SQL execution error: $ SQL ");
Of course it is still a logical expression, but the result is discarded.