PDA

View Full Version : mysqli و PDO در دیتابیس



crafcrab
یک شنبه 01 آذر 1394, 21:08 عصر
سلام

دوستان من یک کلاس برای کار با دیتابیس تهیه کردم این کلاس در ورژن 2.4 وب سرور wamp بدون مشکل کار میکرد اما وقتی wamp 2.5 ریختم میگه این دستورات منسوخ شده

کلاس دیتابیس من:


<?phpclass database{ var $last_query; //Saved result of the last query made var $last_result; //Results of the last query made var $func_call; //A textual description of the last query/get_row/get_var call var $link; //database link var $lastquery; //last query var $result; //query result // Connect to MySQL database function database($DB_NAME,$DB_USER,$DB_PASS) { $this->link=mysql_connect(DB_HOST, $DB_USER, $DB_PASS) or die('ارتباط با سرور ممکن نیست'.'<br>'.'لطفا چند دقیقه دیگر دوباره سعی کنید'.'<br>'.'در صورت رفع نشدن مشکل به تیم پشتیبانی سایت در قسمت تماس با ما اطلاع دهید'.'<br>'.'با تشکر'); //Set All Charsets to UTF8 mysql_query("SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8"); mysql_select_db($DB_NAME) or die('خطا در برقراری ارتباط'.'<br>'.'لطفا چند دقیقه دیگر دوباره سعی کنید'.'<br>'.'در صورت رفع نشدن مشکل به تیم پشتیبانی سایت در قسمت تماس با ما اطلاع دهید'.'<br>'.'با تشکر'); } /** Query the database. * @param $query The query. * @return The result of the query into $lastquery, to use with fetchNextObject(). */ function query( $query ){ $this->lastquery=$query; $this->result=@mysql_query( $query, $this->link ); return $this->result; } /** Do the same as query() but do not return nor store result. * Should be used for INSERT, UPDATE, DELETE... * @param $query The query. * @param $debug If true, it output the query and the resulting table. */ function execute($query){ @mysql_query($query); } /** Convenient method for mysql_fetch_object(). * @param $result The ressource returned by query(). * @return An ARRAY representing a data row. */ function fetchArray($result){ if ($result == NULL) $result = $this->result; if ($result == NULL || mysql_num_rows($result) < 1) return NULL; else return mysql_fetch_assoc($result); } /** Close the connecion with the database server. * It's usually unneeded since PHP do it automatically at script end. */ function close(){ mysql_close($this->link); } /** Get the number of rows of a query. * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used. * @return The number of rows of the query (0 or more). */ function numRows($result = NULL){ if ($result == NULL) return @mysql_num_rows($this->result); else return mysql_num_rows($result); }}?>




تا انجایی که گشتم فهیدم @ باید حذف کنم مثل اینکه منسوخ شده در ورژن جدید php
مورد دیگه اینکه میگه باید از mysqli یا PDO استفاده کنم برای کار با دیتابیسیه مثال در این لینک زده :
http://php.net/manual/en/mysqlinfo.api.choosing.php
ولی نمیدونم من چطوری کد خودم رو با مثال بالا به روز کنم
نمیخوام نام و ورودی های کلاسم عوض شه چون در این صورت کل قسمتهایی که در پروژه خودم از این کلاس استفاده کردم باید تغییر بدم


اگه میشه کمک کنید آپدیت شده کلاس خودم با کمک mysqli بنویسم

kabootar_y
دوشنبه 02 آذر 1394, 10:31 صبح
من ویرایشش کردم ولی تست نکردم احتمالا خطا نداشته باشه
اگه خطا داشت بگو تا مشکلش رو بر طرف کنم



<?php
class database{ var $last_query;//Saved result of the last query made var $last_result; //Results of the last query made var $func_call; //A textual description of the last query/get_row/get_var call var $link; //database link var $lastquery; //last query var $result; //query result // Connect to MySQL database function database($DB_NAME,$DB_USER,$DB_PASS) { $this->link=mysqli_connect(DB_HOST, $DB_USER, $DB_PASS,$DB_NAME) or die('ارتباط با سرور ممکن نیست'.''.'لطفا چند دقیقه دیگر دوباره سعی کنید'.''.'در صورت رفع نشدن مشکل به تیم پشتیبانی سایت در قسمت تماس با ما اطلاع دهید'.''.'با تشکر'); //Set All Charsets to UTF8 mysqli_query("SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8"); } /** Query the database. * @param $query The query. * @return The result of the query into $lastquery, to use with fetchNextObject(). */ function query( $query ) { $this->lastquery=$query; $this->result=mysqli_query( $query, $this->link ); return $this->result; } /** Do the same as query() but do not return nor store result. * Should be used for INSERT, UPDATE, DELETE... * @param $query The query. * @param $debug If true, it output the query and the resulting table. */ function execute($query) { mysqli_query($query); } /** Convenient method for mysqli_fetch_object(). * @param $result The ressource returned by query(). * @return An ARRAY representing a data row. */ function fetchArray($result) { if ($result == NULL) $result = $this->result; if ($result == NULL || mysqli_num_rows($result) < 1) return NULL; else return mysqli_fetch_assoc($result); } /** Close the connecion with the database server. * It's usually unneeded since PHP do it automatically at script end. */ function close() { mysqli_close($this->link); } /** Get the number of rows of a query. * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used. * @return The number of rows of the query (0 or more). */ function numRows($result = NULL) { if ($result == NULL)return mysqli_num_rows($this->result); else return mysqli_num_rows($result); }}
?>

crafcrab
دوشنبه 02 آذر 1394, 11:19 صبح
من ویرایشش کردم ولی تست نکردم احتمالا خطا نداشته باشه
اگه خطا داشت بگو تا مشکلش رو بر طرف کنم



<?php
class database{ var $last_query;//Saved result of the last query made var $last_result; //Results of the last query made var $func_call; //A textual description of the last query/get_row/get_var call var $link; //database link var $lastquery; //last query var $result; //query result // Connect to MySQL database function database($DB_NAME,$DB_USER,$DB_PASS) { $this->link=mysqli_connect(DB_HOST, $DB_USER, $DB_PASS,$DB_NAME) or die('ارتباط با سرور ممکن نیست'.''.'لطفا چند دقیقه دیگر دوباره سعی کنید'.''.'در صورت رفع نشدن مشکل به تیم پشتیبانی سایت در قسمت تماس با ما اطلاع دهید'.''.'با تشکر'); //Set All Charsets to UTF8 mysqli_query("SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8"); } /** Query the database. * @param $query The query. * @return The result of the query into $lastquery, to use with fetchNextObject(). */ function query( $query ) { $this->lastquery=$query; $this->result=mysqli_query( $query, $this->link ); return $this->result; } /** Do the same as query() but do not return nor store result. * Should be used for INSERT, UPDATE, DELETE... * @param $query The query. * @param $debug If true, it output the query and the resulting table. */ function execute($query) { mysqli_query($query); } /** Convenient method for mysqli_fetch_object(). * @param $result The ressource returned by query(). * @return An ARRAY representing a data row. */ function fetchArray($result) { if ($result == NULL) $result = $this->result; if ($result == NULL || mysqli_num_rows($result) < 1) return NULL; else return mysqli_fetch_assoc($result); } /** Close the connecion with the database server. * It's usually unneeded since PHP do it automatically at script end. */ function close() { mysqli_close($this->link); } /** Get the number of rows of a query. * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used. * @return The number of rows of the query (0 or more). */ function numRows($result = NULL) { if ($result == NULL)return mysqli_num_rows($this->result); else return mysqli_num_rows($result); }}
?>



خطا میده برای




( ! )Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\p\class-database.php on line 14





mysqli_query("SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8");

crafcrab
دوشنبه 02 آذر 1394, 12:10 عصر
اگه برای کلاس من مثال pdo بتونی بزنی ممنون میشم

kabootar_y
سه شنبه 03 آذر 1394, 00:25 صبح
ویرایشش کردم ولی بازم به دلیل کمبود وقت تستش نکردم
اگه مشکل داشت بازم در خدمتم
راستی گفتی PDO اگه بخوای اینو کلاس رو به PDO تغییر بدی از نگاه امنیت و ... فرقی نمی کنه و اگه بخوای بحث های امنیتی رو که معمولا در استفاده از PDO استفاده میشه رو در نظر بگیری باید هرجایی که از کلاس database و متدهاش استفاده کردی رو هم تغییر بدی که فکر می کنم لازم نیست همین که بتونی از mysqli استفاده کنی فکر می کنم کافی باشه (((( البته این پیشنهاد منه بر اساس تجربیات خودم))))




<?phpclass database{ var $last_query;//Saved result of the last query made var $last_result; //Results of the last query made var $func_call; //A textual description of the last query/get_row/get_var call var $link; //database link var $lastquery; //last query var $result; //query result // Connect to MySQL database function database($DB_NAME,$DB_USER,$DB_PASS) { $this->link=mysqli_connect(DB_HOST, $DB_USER, $DB_PASS,$DB_NAME) or die('ارتباط با سرور ممکن نیست'.''.'لطفا چند دقیقه دیگر دوباره سعی کنید'.''.'در صورت رفع نشدن مشکل به تیم پشتیبانی سایت در قسمت تماس با ما اطلاع دهید'.''.'با تشکر'); //Set All Charsets to UTF8 mysqli_query($this->link, "SET character_set_results=utf8 , character_set_client=utf8 , character_set_connection=utf8 , character_set_database=utf8 , character_set_server=utf8"); } /** Query the database. * @param $query The query. * @return The result of the query into $lastquery, to use with fetchNextObject(). */ function query( $query ) { $this->lastquery=$query; $this->result=mysqli_query($this->link, $query, $this->link ); return $this->result; } /** Do the same as query() but do not return nor store result. * Should be used for INSERT, UPDATE, DELETE... * @param $query The query. * @param $debug If true, it output the query and the resulting table. */ function execute($query) { mysqli_query($this->link, $query); } /** Convenient method for mysqli_fetch_object(). * @param $result The ressource returned by query(). * @return An ARRAY representing a data row. */ function fetchArray($result) { if ($result == NULL) $result = $this->result; if ($result == NULL || mysqli_num_rows($result) < 1) return NULL; else return mysqli_fetch_assoc($result); } /** Close the connecion with the database server. * It's usually unneeded since PHP do it automatically at script end. */ function close() { mysqli_close($this->link); } /** Get the number of rows of a query. * @param $result The ressource returned by query(). If NULL, the last result returned by query() will be used. * @return The number of rows of the query (0 or more). */ function numRows($result = NULL) { if ($result == NULL)return mysqli_num_rows($this->result); else return mysqli_num_rows($result); }}?>