Main Page;
Processing page:
<?phpinclude (".. /dbda.class.php "); $db =new Dbda (); $sql =" SELECT * from Info ";//Method One: Normal method Write directly://$attr = $db->query ($sql);//returns an array, To convert to a string//because you want to return a string as a table, you need to split the string//split into this form: "p001^ Zhang San |p002^ John Doe |p003^ Harry";/* $str = ""; foreach ($attr as $v) {$str =$ Str.implode ("^", $v); Stitch each column of data into a string $str= $str. "|"; /stitching per line}*///sunstr: Intercepts a string, gets the string from the nth position in the string s, starting with the length Len,//$str =substr ($str, 0,strlen ($STR)-1);//This step is to remove the end of the "|" Symbol//echo $STR;//Method Two: Use Dbda in the method of encapsulation echo $db->strquery ($sql);
The running results show:
Attachment 1: Converting an array of Ajax into a string wrapper into a classThe AJAX call returns the string public function strquery ($sql, $type =1, $db = "MyDB") {//Build Connection object $conn = new Mysqli ($this->host, $this- UID, $this->password, $db);//Determine if the connection is successful!mysqli_connect_error () or Die ("Connection failed! ");//Execute SQL statement $result = $conn->query ($sql);//Determine SQL statement type if ($type ==1) {$attr = $result->fetch_all (); $str =" ";// If the query statement returns a string for ($i =0; $i <count ($attr), $i + +) {for ($j =0; $j <count ($attr [$i]); $j + +) {$str = $str. $attr [$i] [$j];$ str = $str. " ^";} $str = substr ($str, 0,strlen ($STR)-1); $str = $str. "|";} $str = substr ($str, 0,strlen ($STR)-1); return $str;} else{//if it is a different statement, return TRUE or Falseif ($result) {return "OK";} Else{return "NO";}}}
Attached 2: In the Statistics function (count), it is convenient to use strquery ($sql) to return the string directly
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The returned result is 1.
Converting an array into a string in Ajax