No words of error, mysql_fetch_array ()

Source: Internet
Author: User
MySQL PHP Database Connection query

Code below, please help to look at it!
 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);} /** * Execute SQL */public function query ($sql) {return mysql_query ($sql, $this->conn) or Die (Mysql_error (). "
SQL execution error: $sql ");} /** * Returns multiple records */public function getdataarrays ($sql, $type = mysql_both) {$result = $this->query ($sql); $refArr = Array (); W Hile ($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 results of the implementation are 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


The 45th line of code is marked in red above.


Reply to discussion (solution)

That piece of red was made into PHP code, and I blacked out.
It's/**.
* Return multiple records
*/
Public Function Getdataarrays ($sql, $type = Mysql_both) {
$result = $this->query ($sql);
$REFARR = Array ();
while ($row = Mysql_fetch_array ($result, $type)) {
$REFARR [] = $row;
}
return $REFARR;
}

How come there's no one here?
A few questions, no matter how simple or difficult, no one to help answer,
When did csdn become so deserted?

Estimating the SQL problem
Echo Mysql_error () on the while; Look

LZ look at the above example



Grammar

Mysql_fetch_array (Data,array_type)

Parameter description
Data is optional. Specifies the data pointer to use. The data pointer is the result of the mysql_query () function.
Array_type

Optional. Specifies what kind of results to return. Possible values:

MYSQL_ASSOC-Associative arrays
Mysql_num-numeric array
Mysql_both-Default. Simultaneous generation of associative and numeric arrays




 
  

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 ")
is a logical expression that can only be returned if it is a logical value.
So to write
$rs = mysql_query ($sql, $this->conn) or Die (Mysql_error ().
SQL execution error: $sql ");
$rs = mysql_query ($sql, $this->conn) is executed first because = has a higher precedence than or
The calculation becomes
$rs or Die (Mysql_error ().
SQL execution error: $sql ");
Of course it's a logical expression, but the result is abandoned.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.