pdo

MySQL Query: insert if entry is duplicate otherwise update the existing row values

风流意气都作罢 提交于 2019-12-24 09:03:18
问题 i have a table called bag (again): +--------+----------+---------+----------+ | bag_id | chara_id | item_id | item_qty | +--------+----------+---------+----------+ | 1 | 1 | 2 | 22 | | 2 | 1 | 1 | 55 | | 3 | 3 | 1 | 2 | | 6 | 3 | 4 | 2 | | 7 | 4 | 4 | 2 | | 8 | 5 | 4 | 2 | | 9 | 6 | 4 | 2 | | 10 | 1 | 5 | 1 | | 14 | 1 | 8 | 1 | | 15 | 1 | 6 | 1 | | 18 | 1 | 4 | 1 | | 19 | 1 | 3 | 1 | | 29 | 8 | 1 | 1 | | 30 | 8 | 7 | 1 | | 33 | 6 | 2 | 1 | +--------+----------+---------+----------+ and i have

Use fetch_column and fetch(PDO::FETCH_ASSOC) together?

梦想的初衷 提交于 2019-12-24 08:53:19
问题 I will admit php is a new language to me. Now I can get each of these working individually. In my prepare query SELECT * FROM... will allow my PDO fetch assoc while loop to work, but then fetch column doesn't work. And then I use SELECT COUNT(*) my fetch column works but then my fetch assoc doesn't. So Is there away around this so both will work? As I need the fetch column to return how many rows there are as an integer value (1 or 0) to determine if the user has entered log in information.

PDO bug: prepare() with multi-queries doesn't work inside a transaction

瘦欲@ 提交于 2019-12-24 08:36:22
问题 Searching for the solution to my previous question I've come across a weird fact - PDO prepare() with multi-queries does not work correctly inside a transaction. You get no warning, no exception, nothing - just silence and no commit/rollback. Furthermore, you get no exception even if some of the queries except for the first one contain errors. Although if the first query contains an error you get an exception as expected. If there is no transaction everything works fine. If you put several

PHP PDO dynamic WHERE clause

瘦欲@ 提交于 2019-12-24 08:30:36
问题 I have a simple function that returns a count from a database table, based on some criteria. function MyCount($strTable, $strCriteria) { $strSQL = "SELECT COUNT(*) FROM " . $strTable . " "; if (trim($strCriteria) != "") $strSQL .= "WHERE " . $strCriteria; $results = mysql_query($strSQL, $objConn); $row = mysql_fetch_array($results); return $row[0]; } Its very useful for quickly getting a value in 1 line of code, e.g: $Users = MyCount("Users", "Deleted = 0"); However, I'm now trying to move to

pdo prepared statement multiple execution?

余生长醉 提交于 2019-12-24 08:22:41
问题 I'm just getting to implement PDO in my site but I was wondering if it is possible to execute prepared statement multiple times? $SQL = $dbh->prepare("SELECT * FROM user WHERE id=? AND users=?"); $SQL -> execute(array($id,$userid)); while($check = $SQL -> fetchObject()){ and then I would use a while loop for this SQL Can I use the same $SQL for another execution within the while loop? So I don't have to type in the prepared statement again? $SQL -> execute(array($id2,$userid2)); while($check2

Inserting multiple rows at once with prepared statements

做~自己de王妃 提交于 2019-12-24 08:17:34
问题 I would like to know how can I insert multiple values in an array via prepared statements. I've looked at these two (this question and this other one ) questions but they don't seem to do what I'm trying. This is what I have: $stmt = $this->dbh->prepare("INSERT INTO t_virtuemart_categories_en_gb (category_name, virtuemart_category_id) VALUES (:categoryName, :categoryId) ;"); foreach($this->values as $insertData){ $categoryName = $insertData['categoryName']; $categoryId = $insertData[

How to handle PDO connection errors properly?

 ̄綄美尐妖づ 提交于 2019-12-24 08:17:34
问题 I try to handle or catch possible errors with PHP/MySQL for security reasons and would like to know if I'm doing it right. The first case: I use it as a function and call it always when I need a database connection. The second case: I am not sure how to handle it. I put all prepared statements in if conditions but that's pretty awkward. And what about $stmt->execute ? This could also fail and to handle this also in an if condition can really get confusing. I hope there is a better way to go.

Unable to connect to MySQL through PHP script when using mysqli or PDO BUT mysql works

你说的曾经没有我的故事 提交于 2019-12-24 08:16:05
问题 I am facing a weird situation. When I am trying to connect to MySql database using a "mysql" connection, it works. mysql connection string -> mysql_connect($HOST, $USER, $PASSWORD, $DB); But the connection fails immediately fails when I use either "mysqli" or "PDO" mysqli connection string -> mysqli_connect($HOST, $USER, $PASSWORD, $DB); PDO Connection string -> new PDO("mysql:host=$HOST;dbname=$DB", $USER, $PASSWORD); The specific error that it throws is, SQLSTATE[HY000] [2003] Can't connect

PDO returns no results while MySQL command line returns expected result

大兔子大兔子 提交于 2019-12-24 08:07:03
问题 I was working with database, running dynamically generated query. Everything was just fine, when I noticed problems with encoding in couple of rows in one table, so I repopulated it from backup and rebuilt the table. After this action, PDO returns empty result and doesn't throw any exceptions, however, the same query with the same parameters executes successfully (returns some rows) from the command line. I copy-pasted both query and parameters from var_dump() output to run from command line.

mysql pdo connection is not closing?

强颜欢笑 提交于 2019-12-24 07:39:45
问题 In my php script, When I am executing query on certain PDO mysql connection, on checking mysql logs, I am not able to see the connection getting closed. Php Code: ?php $db = new PDO('mysql:host=HOST;dbname=DB',USER,PASSWORD); $db->exec("SHOW TABLES"); $db = null; ?> Mysql logs: 180312 18:31:45 9048429 Connect USER@HOST on DB 9048429 Query SHOW TABLES Though, when I remove query, I can see the mysql connection closed on the Mysql log. php code: ?php $db = new PDO('mysql:host=HOST;dbname=DB'