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

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

morsrh 30-01-11 12:47

בניית שאילתה [דחוף].
 
שלום ,
אני בונה מחלקה שתעבוד על מאגרי נתונים מסוג MYSQLI ובניתי פונקציה שאמורה ליצור מחרוזת של שאילתה ואז לשלוח אותה , הנה מה שכתבתי:

PHP קוד:

$val[] = 'VH1';
         
$val[] = 'VH2';
         
$val[] = 'VH3';
         
$col[] = 'CL1';
         
$col[] = 'CL2';
         
$col[] = 'CL3';
         
$this->mysqliInsert($val 'users' $col);
         
         
    function 
mysqliInsert($value $table $column){
        if(
is_array($column)){
            if(
count($column) != count($value)){
                echo 
'SERVER ERROR: while insert VALUE count does not equal COLUMN count';
                exit;
            }
            
$this->syntax '"INSERT INTO `'.$table.'` ( ';
            for(
$i count($column)-$i >= $i--){
                
$this->syntax .= '`'.$column[$i].'`';
                if(
$i != )
                    
$this->syntax .= ' , ';
                else
                    
$this->syntax .= ') VALUES (';
            }
            for(
$i count($column)-$i >= $i--){
                
$this->syntax .= '\''.$value[$i].'\'';
                if(
$i != )
                    
$this->syntax .= ' , ';
                else
                    
$this->syntax .= ')"';
            }
            echo 
$this->syntax;
        }
        
$this->sqli->query->($this->syntax);
    } 

מה שיוצא בסוף זה :

קוד:

"INSERT INTO `users` ( `CL3` , `CL2` , `CL1`) VALUES ('VH3' , 'VH2' , 'VH1')"
בדיוק מה שתכננתי שיצא.
אבל כנראה שזה לא עובד..
השגיאה היא :

קוד:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in C:\wamp\www\rebi\server\mysqli_c.php on line 52
שורה 52 :

קוד:

$this->sqli->query->($this->syntax);
מה הבעיה פה חברה?
תודה.

Ender 30-01-11 12:53

זה לא אמור להיות ככה:
קוד:

$this->sqli->query($this->syntax);
?

morsrh 30-01-11 13:00

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

Ender 30-01-11 13:01

כן.. בשורה של ה- INSERT יש " מיותר.

ובכלל, אני במקומך הייתי מממש את הפונקציה הזאת ככה:
PHP קוד:

<?php
    $col 
= array("col1" => "val1""col2" => "val2");
    
$this->mysqliInsert('users' $col); 
    function 
pad_key($val) {return "`".mysql_real_escape_string($val)."`";}      
    function 
pad_val($val) {return "'".mysql_real_escape_string($val)."'";}  
    function 
mysqliInsert($table $column)
    { 
        if(
is_array($column))
        { 
            
$arr_keys array_map('pad_key'array_keys($column));
            
$arr_vals array_map('pad_val'array_values($column));
            
$this->syntax 'INSERT INTO `'.mysql_real_escape_string($table).'`';
            
$this->syntax .= '( '.implode(','$arr_keys).') VALUES ( '.implode(','$arr_vals).')'
            echo 
$this->syntax
            
$this->sqli->query($this->syntax); 
        }
    }  
?>


morsrh 30-01-11 13:15

כן בדיוק מצאתי את המרכאות האלה..
אני גם אבחון את הפונקציה שלך ,
באמת תודה.


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

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