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.