 |

14-10-2006, 17:12
|
|
|
|
חבר מתאריך: 17.05.04
הודעות: 1,454
|
|
|
בעיה עם eval
כן, אני יודע, אני מדביק הרבה אשכולות בזמן האחרון 
בכל מקרה, עשיתי מחלקה להתחברות mysql ניסיתי לעשות שהשאילתה תשמש כסוג של sprintf בשביל לעשות mysql_real_escape_string על המשתנים
וגם בגלל שיותר נוח עם sprintf יש לי שני נסיונות שאף אחד מהם לא עובד (הבעיה נמצאת בפונקציה query) :
קוד PHP:
<? class mysql{ public $queryResult, $numQuery; protected $query, $db; private $key, $user, $pass; function __construct($db, $user = 'root', $host = 'localhost', $pass = false){ $this->user = $user; $this->pass = $pass; if(!$pass) $this->key = @mysql_connect($host, $this->user); else $this->key = @mysql_connect($host, $this->user, $this->pass); if($this->key){ if (trim($db)) $this->db = @mysql_select_db($db, $this->key); else{ mysql_close($this->key); return false; } return $this->key; } else $this->error(); } public function query($query){ function escapeArray($array){ if(get_magic_quotes_gpc()){ foreach($array as $i) $i = mysql_real_escape_string(stripslashes($i)); } else foreach($array as $i) $i = mysql_real_escape_string($i); return $array; } $args = escapeArray(array_slice(func_get_args(), 1)); unset($this->query); if(trim($query) != ''){ $this->numQuery ++; $query = sprintf($query, eval("implode(',', $args)")); $this->query = mysql_query($query, $this->key) or $this->error; } else return false; return $this->query; } public function fetchResult($stat = 'array'){ switch($stat){ case 'num': $this->queryResult = @mysql_num_rows($this->key) or $this->error(); break; default: $this->queryResult = eval('@mysql_fetch_'.$stat.'($this->key) or $this->error();'); break; } return $this->queryResult; } protected function error(){ die(mysql_error()); } function __destruct(){ unset($this->key); unset($this->user); unset($this->pass); } } $sql = new mysql('demlin'); $table = "userdata"; $id = 1; $sql->query("SELECT * FROM `%d` WHERE ID='%s'", $table, $id); while($row = $sql -> fetchResult()){ var_dump($row); } ?>
כאן זה מחזיר את השגיאות :
Parse error: parse error, unexpected ')', expecting '(' in C:\wamp\www\sql\sqlClass.php(40) : eval()'d code on line 1
Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\sql\sqlClass.php on line 40
בקיצור, ה sprintf לא רואה את זה בתור שלושה ארגומנטים ('שאילתה', 'טבלה', 'ID') אלא רק בתור שניים ('שאילתה','טבלהID')..
כיוון שאני מעדיף את הקוד הזה מהקוד השני האם למישהו יש פתרון איך אני אגרום לsprintf לראות את זה כ שאילתה טבלה ו ID?
הקוד השני :
קוד PHP:
<? class mysql{ public $queryResult, $numQuery; protected $query, $db; private $key, $user, $pass; function __construct($db, $user = 'root', $host = 'localhost', $pass = false){ $this->user = $user; $this->pass = $pass; if(!$pass) $this->key = @mysql_connect($host, $this->user); else $this->key = @mysql_connect($host, $this->user, $this->pass); if($this->key){ if (trim($db)) $this->db = @mysql_select_db($db, $this->key); else{ mysql_close($this->key); return false; } return $this->key; } else $this->error(); } public function query($query){ function escapeArray($array){ if(get_magic_quotes_gpc()){ foreach($array as $i) $i = mysql_real_escape_string(stripslashes($i)); } else foreach($array as $i) $i = mysql_real_escape_string($i); return $array; } $args = escapeArray(array_slice(func_get_args(), 1)); unset($this->query); if(trim($query) != ''){ $this->numQuery ++; $query = "sprintf(".$query.""; for($i = 0; $i <= func_num_args() - 2; $i++) $query = $query.",".$args[$i]; $query = $query.");"; eval("\$query = \"".$query."\";"); $this->query = mysql_query($query, $this->key) or $this->error; } else return false; return $this->query; } public function fetchResult($stat = 'array'){ switch($stat){ case 'num': $this->queryResult = @mysql_num_rows($this->key) or $this->error(); break; default: $this->queryResult = eval('@mysql_fetch_'.$stat.'($this->key) or $this->error();'); break; } return $this->queryResult; } protected function error(){ die(mysql_error()); } function __destruct(){ unset($this->key); unset($this->user); unset($this->pass); } } $sql = new mysql('demlin'); $table = "userdata"; $id = 1; $sql->query("SELECT * FROM `%d` WHERE ID='%s'", $table, $id); while($row = $sql -> fetchResult()){ var_dump($row); } ?>
מוציא את השגיאה:
Parse error: parse error, unexpected '`' in C:\wamp\www\sql\aa.php(35) : eval()'d code on line 1
ובכן הקוד הזה הרבה פחות יעיל ונחמד, אבל הוא עושה את העבודה יותר טובה..
בסוף יוצא ש:
קוד PHP:
$query = "sprintf("SELECT * FROM `%d` WHERE ID='%s' ,'userdata','1'")
אבל ה eval לא הופך את ה sprintf לקוד PHP - גם פה אני לא מבין למה, זה ממש דומה להסבר בPHP.net..
את הקוד השני אני פחות מעדיף, אבל אשמח לפתרון גם בשבילו 
תודה מראש.
_____________________________________
EVERYTHING SHOULD BE MADE AS SIMPLE AS POSSIBLE, BUT NOT ONE BIT SIMPLER
ALBERT EINSTEIN
נערך לאחרונה ע"י Rs3k בתאריך 14-10-2006 בשעה 17:26.
|
|