הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   [PHP] צריך עזרה בשאילתה (https://hosts.co.il/forums/showthread.php?t=28661)

Elad-A 13-08-06 12:10

[PHP] צריך עזרה בשאילתה
 
שלום,

יש לי את הקוד הבא:
PHP קוד:

<? 
class db{  

var 
$db;  

function 
connect($host$username$password$db_name){  

    
$this->db mysql_connect($host,$username,$password)  
        or die(
"שגיאה");  
      
    
mysql_select_db($db_name,$this->db)  
    or die(
"שגיאה");  
      
    }  
function 
query($query
    { 
        
$res mysql_query("blabla"); 
    return 
$res
    } 
function 
fetch_rows() 
    { 
        return 
mysql_fetch_array($res); 
    } 
function 
num_rows() 
    { 
        return 
mysql_num_rows($res); 
    } 

}   
$db = new db;  
$db->connect("localhost","root","","ea2"); 
$db->query("SELECT * FROM ibf_thanks"); 
$rows $db->num_rows(); 
if(
$rows == 0

echo 
"no"

else 

$users=$db->fetch_rows(); 
echo 
$users 

?>

ההתחברות למסד עובדת אך יש בעיה עם השאילתה. תודה לעוזרים.

drowkid 13-08-06 12:14

תעשה מה שאמרתי לך...
PHP קוד:

global $res

אמממ
PHP קוד:

function fetch_rows() 
    { 
global 
$res;
        return 
mysql_fetch_array($res); 
    } 
function 
num_rows() 
    { 
global 
$res;
        return 
mysql_num_rows($res); 
    } 

הסבר:
גלובל עושה שתוכל להישתמש במישתנים שהוגדרו מחוץ לפונקציה...

Elad-A 13-08-06 12:30

ניסתי לעשות ככה:
PHP קוד:

class db

var 
$db

function 
connect($host$username$password$db_name){ 

    
$this->db mysql_connect($host,$username,$password
        or die(
"שגיאה"); 
     
    
mysql_select_db($db_name,$this->db
    or die(
"שגיאה"); 
     
    } 
function 
query($query)
    {
        
$res mysql_query("blabla");
    return 
$res;
    }
function 
fetch_rows() 
    { 
global 
$res;
        return 
mysql_fetch_array($res); 
    } 
function 
num_rows() 
    { 
global 
$res;
        return 
mysql_num_rows($res); 
    }

}  

$db = new db;  
$db->connect("localhost","root","","ea2");
$db->query("select * from ibf_thanks");
$db->fetch_rows();
$db->num_rows();
while(
$row $db->fetch_rows()) 
{
@
$ida$row['user_id'];
@
$namea $row['user_name'];
@
$datea $row['thank_date'];
echo <<<END
, <a href="index.php?showuser=$ida">$namea</a>
END;


זה השגיאה..
PHP קוד:

Warningmysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:program fileseasyphp1-8wwwtestindex.php on line 52

Warning
mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:program fileseasyphp1-8wwwtestindex.php on line 57

Warning
mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:program fileseasyphp1-8wwwtestindex.php on line 52 


amirs_5 13-08-06 12:44

שניה עורך טעות..

עריכה :
HTML קוד:

function query($query)
    {
global $res;
        $res = mysql_query("blabla");
    return $res;
    }

צריך גם בפונקציה הזאתי להכיר אותו כ global.

lalamen 13-08-06 12:45

מצאתי לך מחלקה של DATEBASE תסתכל איך הם עשו

PHP קוד:

class mysql {

  function 
Connect($host$name$pass$db){

  
$connection mysql_connect("$host",
  
"$name",
  
"$pass");
  
mysql_select_db("$db"$connection);

  }
//ends the connection function

  
function Close(){

  
mysql_close($this->connection);

  }
//ends the close function

  
function FetchRow($query){
  
$rows mysql_fetch_row($query);
  return 
$rows;
  }

  function 
FetchArray($query){
  
$array mysql_fetch_array($query);
  return 
$array;
  }

  function 
FetchNum($query){
  
$num mysql_num_rows($query);
  return 
$num;
  }

  function 
Query($sql){
  
$query mysql_query($sql) or die(mysql_error());
  return 
$query;
  }
//ends the query function

  
}//ends the class 

דוגמא לשימוש במחלקה

PHP קוד:

 include("mysql_class.php");

  
$DB = new mysql();

  
$host "localhost";
  
$name "username";
  
$pass "password";
  
$db "dbname";

  
$connection $DB->Connect($host$name$pass$db);

  
//define an SQL statement and execute it
  
$sql "SELECT title,author FROM news";
  
$query $DB->Query($sql);

  
//fetch a single row and output it
  
$newsrow $DB->FetchRow($query);
  
$title $newsrow[0];
  
$author $newsrow[1];
  echo 
"<b>Single Row</b><br /><br />Title: $title<br>
  Author: $author<br><br>"
;

  
//output all rows from the statement
  
while($array $DB->FetchArray($query)){
  
extract($array);
  echo 
"<b>All rows</b><br /><br />Title: $title<b>
  Author: $author<br />"
;
  }

  
//find the number of rows
  
$num $DB->FetchNum($query);
  echo 
"Number of rows: $num";

  
//close the connection
  
$DB->Close(); 


Elad-A 13-08-06 12:58

תודה עכשיו עוד בעיה קטנה עם זה. זה שולף את הנתונים אבל מציג שגיאה:
PHP קוד:

NoticeUndefined propertyconnection in c:program fileseasyphp1-8wwwtestindex.php on line 44

Warning
mysql_close(): supplied argument is not a valid MySQL-Link resource in c:program fileseasyphp1-8wwwtestindex.php on line 44 


amirs_5 13-08-06 13:10

טוב הסתכלתי בקוד ובשגיאה ואיו בקוד mysql_close ככה שהשגיאה קצת מוזרה..
השגיאה בשורה 44...
להלן :
PHP קוד:

echo <<<END 
, <a href="index.php?showuser=$ida">$namea</a
END

אני לא מכיר את END , וגם אני לא חושב שמה שאני יגיד יעזור אבל תוכל לנסות לרשום שניה
PHP קוד:

echo ", <a href=\"index.php?showuser=$ida\">$namea</a>"

תנסה ותגיד לי אם עזר לך אם לא אני ינסה את המחלקה אצלי בשרת ונראה מה הבעיה בה כבר..

Elad-A 13-08-06 13:38

שיניתי כבר לפני זה את החלק הזה ועדיין השגיאה אותו דבר.

RS324 13-08-06 18:30

טוב מספיק עם השטויות שמדברים פה, בכללי בגלל שזה CLASS זה עובד קצת אחרת

קודם כל תגדיר משתנה שנקרא לדוגמא CON ואז
mysql_query($query,$this->con)

ופה נגמר הסיפור .

אין שום קשר ל GLOBAL או לשטויות אחרות שנאמרו פה


כל הזמנים הם GMT +2. הזמן כעת הוא 22:10.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ