顯示具有 MySQL 標籤的文章。 顯示所有文章
顯示具有 MySQL 標籤的文章。 顯示所有文章

2017年4月5日 星期三

【PHP】解決 SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

記錄本日工作時遇到的詭異問題,PHP 在撈 DB 資料時出現了下列錯誤訊息:
SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

原因是如果參數有兩個名稱重複時,就會造成這個錯誤
```sql select * from student where sid = :id or tid = :id ```

將參數改為不同名稱就能解決這個問題
```sql select * from student where sid = :sid or tid = :tid ```

2015年1月22日 星期四

【SQL】MySQL 避免重複 insert 的方法(ignore, replace, on duplicate key update)

方法一:使用 ignore

```sql insert ignore into user (id, name) values (10, 'Lionel'); ``` 方法二:使用 replace

```sql replace into user (id, name) values (11, 'Luis'); ``` 方法三:使用 on duplicate key update

```sql insert into user (id, name) values (3, 'Mario') on duplicate key update name = 'Gerard'; ```

2014年12月31日 星期三

【SQL】在 Windows 環境中使用 Python 中取得 MySQL 資料

步驟一:先下載並安裝 MySQLdb for Python,例:MySQL-python-1.2.5.win32-py2.7.exe
下載網址:https://pypi.python.org/simple/mysql-python/

步驟二:在 Python 中輸入下列程式碼:
```python import MySQLdb db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="db_test") cursor = db.cursor() cursor.execute("select data from table_test") result = cursor.fetchall() for record in result: print record[0], "
" ```