داخل فایل DB.php چه کدی باید وجود داشته باشد ؟
	
	
		سلام . من میخوام  از کدهای این لینک  استفاده کنم که مثالی است از نحوه ارتباط  Mysql با جی کوئری . من بعد از اجرای فایل تنها خروجی که در صفحه می بینم عدد خطا   this element will be accessed by jquery and this text replaced  هستش .
. و فکر می کنم به این مساله  مربوط می شود  که فایل DB.php رو نساختم . ممنون میشم بگین که  برای استفاده از این کد  چه  تغییراتی در فایل api.php لازم است و اینکه  کد فایل DB.php  رو بهم بدین .
کد api.php
	کد:
	
<?php 
  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $host = "localhost";
  $user = "root";
  $pass = "root";
  $databaseName = "ajax01";
  $tableName = "variables";
  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
  include 'DB.php';
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);
  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------
  $result = mysql_query("SELECT * FROM $tableName");          //query
  $array = mysql_fetch_row($result);                          //fetch result    
  //--------------------------------------------------------------------------
  // 3) echo result as json 
  //--------------------------------------------------------------------------
  echo json_encode($array);
?>
 
کد  client.php
	کد:
	
<!---------------------------------------------------------------------------
Example client script for JQUERY:AJAX -> PHP:MYSQL example
---------------------------------------------------------------------------->
<html>
  <head>
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
  </head>
  <body>
  <!-------------------------------------------------------------------------
  1) Create some html content that can be accessed by jquery
  -------------------------------------------------------------------------->
  <h2> Client example </h2>
  <h3>Output: </h3>
  <div id="output">this element will be accessed by jquery and this text replaced</div>
  <script id="source" language="javascript" type="text/javascript">
  $(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX jQuery.ajax() | jQuery API Documentation
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: 'api.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
        var id = data[0];              //get id
        var vname = data[1];           //get name
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
        //recommend reading up on jquery selectors they are awesome 
        // Selectors | jQuery API Documentation
      } 
    });
  }); 
  </script>
  </body>
</html>
 
	 
	
	
	
		پاسخ : داخل فایل DB.php چه کدی باید وجود داشته باشد ؟
	
	
		اون include'DB.php' رو کامنت کنید و تست کنید .
اینو معولا توش توابع رو می نویسن.
برنامه نویسی بصورت شی گرای هستش .
من مثلا واسه خودم از اینا استفاده می کنم .
فایل  DB.php من
	کد PHP:
	
<?php
    class DB{
        /////connection parametrs
        private $dbhost ="";
        private $user    ="";
        private $pass    ="";
        private $dbname ="";
        private $persistent = false ; 
        ////////////////database connection handle////
        private $conn =null ; 
        ////////////////Qury result//////////////////
        private $result = false ;
        function __construct($dbhost , $user , $pass , $dbname , $persistent = false){
            $this->dbhost = $dbhost ; 
            $this->user   = $user ; 
            $this->pass      = $pass ; 
            $this->dbname = $dbname ; 
            $this->persistent =$persistent ;
        }
        function open(){
            ////choose the appropriate connection function  
            if($this->persistent){
                $this->dbhost = ":p".$this->dbhost;
            }
            $fun = "mysqli_connect" ;
            /////////////connection  to the mysql server ////////
            $this->conn = $fun($this->dbhost  , $this->user , $this->pass, $this->dbname) ;
            $this->conn->set_charset('utf8');
            if(!$this->conn){
                echo "1";
                return false ;
            }
            //////////select the request database /////////
            /*if(@!mysql_select_db($this->dbname , $this->conn)){
                echo "2";
                return false;
            }*/
            return true ; 
        }
        function close(){
            return(@$this->conn->close());
        }
/////////////////////////////QUERY is SQL with in php
        function query($sql){
            //$sql = $this->conn->real_escape_string($sql);
            $this->result = $this->conn->query($sql);
            return($this->result) ; 
        }
        function numRows(){
            return(@$this->result->num_rows) ;
        }
        function fetchObject(){
            return(@$this->result->fetch_object());
        }
        function fetchArray(){
            return (@$this->result->fetch_array());
        }
        function fetchAssoc(){
            return(@$this->result->fetch_assoc());
        }
        function fetchRow($qery){
            return(@$qery->fetch_row());
        }
        function freeResult(){
            return(@$this->result->free_result());
        }
        function escape($s){
            $s = $this->conn->real_escape_string($s);
            return $s;
        }
        function insert_id(){
            return($this->conn->insert_id);
        }
///////////////////////// ERROR  
        function error(){
            echo $this->conn->error;
        }
//////////////////////////////////////////////////////////////PHP5//////////////////////////////
        /*
        function openi(){
            $this->conn = new mysqli($this->dbhost,$this->user,$this->pass,$this->dbname);
            if(!$this->conn){
                return flase;
            }
            return true;
        }
        function mysqli_Query($sql){
            $this->result =$this->conn->mysqli_query($sql);    
            return ($this->result);    
        }
        */
        function mysqli_M_Query($sql){
            $this->result=$this->conn->multi_query($sql);
            return ($this->result);
        }
        function mysqli_Error(){
            return $this->conn->errno;
        }
    }
 یه دونه هم فایل config.php  دارم که تو اونم مشخصات دیتابیس رو می نویسم.
	کد PHP:
	
<?php
require_once ('DB.php');
    $dbhost = "localhost" ; 
    $user   = "root" ; 
    $pass   = "" ; 
    $dbname = "databasename" ; 
$db = new DB("$dbhost", "$user"  , "$pass" , "$dbname" ) ;
?>
 خب الان می تونید یه دیتابیس بسازید و مشخصات رو وارد کنید فایل کانفیگ رو include کنید و استفاده کنید
بصورت زیر:
	کد PHP:
	
<?php
include("config.php");
$db->open();
$db->query("select * from tablename");
while($row = $db->fetchObject()){
    echo $row->fildname_intable;
}
?>
 موفق باشید.
	 
	
	
	
		پاسخ : داخل فایل DB.php چه کدی باید وجود داشته باشد ؟
	
	
		
	نقل قول:
	
		
		
			
				نوشته اصلی توسط 
ms313
				 
			اون include'DB.php' رو کامنت کنید و تست کنید .
اینو معولا توش توابع رو می نویسن.
برنامه نویسی بصورت شی گرای هستش .
من مثلا واسه خودم از اینا استفاده می کنم .
فایل  DB.php من
	کد PHP:
	
<?php
    class DB{
        /////connection parametrs
        private $dbhost ="";
        private $user    ="";
        private $pass    ="";
        private $dbname ="";
        private $persistent = false ; 
        ////////////////database connection handle////
        private $conn =null ; 
        ////////////////Qury result//////////////////
        private $result = false ;
        function __construct($dbhost , $user , $pass , $dbname , $persistent = false){
            $this->dbhost = $dbhost ; 
            $this->user   = $user ; 
            $this->pass      = $pass ; 
            $this->dbname = $dbname ; 
            $this->persistent =$persistent ;
        }
        function open(){
            ////choose the appropriate connection function  
            if($this->persistent){
                $this->dbhost = ":p".$this->dbhost;
            }
            $fun = "mysqli_connect" ;
            /////////////connection  to the mysql server ////////
            $this->conn = $fun($this->dbhost  , $this->user , $this->pass, $this->dbname) ;
            $this->conn->set_charset('utf8');
            if(!$this->conn){
                echo "1";
                return false ;
            }
            //////////select the request database /////////
            /*if(@!mysql_select_db($this->dbname , $this->conn)){
                echo "2";
                return false;
            }*/
            return true ; 
        }
        function close(){
            return(@$this->conn->close());
        }
/////////////////////////////QUERY is SQL with in php
        function query($sql){
            //$sql = $this->conn->real_escape_string($sql);
            $this->result = $this->conn->query($sql);
            return($this->result) ; 
        }
        function numRows(){
            return(@$this->result->num_rows) ;
        }
        function fetchObject(){
            return(@$this->result->fetch_object());
        }
        function fetchArray(){
            return (@$this->result->fetch_array());
        }
        function fetchAssoc(){
            return(@$this->result->fetch_assoc());
        }
        function fetchRow($qery){
            return(@$qery->fetch_row());
        }
        function freeResult(){
            return(@$this->result->free_result());
        }
        function escape($s){
            $s = $this->conn->real_escape_string($s);
            return $s;
        }
        function insert_id(){
            return($this->conn->insert_id);
        }
///////////////////////// ERROR  
        function error(){
            echo $this->conn->error;
        }
//////////////////////////////////////////////////////////////PHP5//////////////////////////////
        /*
        function openi(){
            $this->conn = new mysqli($this->dbhost,$this->user,$this->pass,$this->dbname);
            if(!$this->conn){
                return flase;
            }
            return true;
        }
        function mysqli_Query($sql){
            $this->result =$this->conn->mysqli_query($sql);    
            return ($this->result);    
        }
        */
        function mysqli_M_Query($sql){
            $this->result=$this->conn->multi_query($sql);
            return ($this->result);
        }
        function mysqli_Error(){
            return $this->conn->errno;
        }
    }
 یه دونه هم فایل config.php  دارم که تو اونم مشخصات دیتابیس رو می نویسم.
	کد PHP:
	
<?php
require_once ('DB.php');
    $dbhost = "localhost" ; 
    $user   = "root" ; 
    $pass   = "" ; 
    $dbname = "databasename" ; 
$db = new DB("$dbhost", "$user"  , "$pass" , "$dbname" ) ;
?>
 خب الان می تونید یه دیتابیس بسازید و مشخصات رو وارد کنید فایل کانفیگ رو include کنید و استفاده کنید
بصورت زیر:
	کد PHP:
	
<?php
include("config.php");
$db->open();
$db->query("select * from tablename");
while($row = $db->fetchObject()){
    echo $row->fildname_intable;
}
?>
 موفق باشید.
	 
 سلام بی نهایت ممنونم از زحمتی که کشیدین . اما من راستش منظورتون از اون " اون include'DB.php' رو کامنت کنید و تست کنید . "  رو متوجه نشدم . ببینید اون  لینکی که دادم  اموزش ارتباط جی کوئری و Mysql هست و این  کدی تو پست اول نوشتم اولین اموزش اش بود .  تا جایی که  متوجه شدم باید یه فایل DB.php  وجود داشته باشه . اگر ممکنه فقط بگین  اون فایل  DB.php     چه کدی داخلش بنویسم که
برنامه اجرا بشه .
__
منظورتون اینکه   هر سه تا فایلی که شما  گذاشتین رو استفاده کنم ؟! و تو قسمت مشخصات ، مشخصات سرور خودمو بنویسم ؟
	 
	
	
		1 فایل پیوست  
	
	
		پاسخ : داخل فایل DB.php چه کدی باید وجود داشته باشد ؟
	
	
		من کد هایی که گذاشتم واسه آموزش بود .
فایل رو پیوست می کنم دانلود کنید استفاده کنید .
بدون مشکل کار می کنه .
موفق باشید.