This is interesting and by that I mean incredibly frustrating.
I am passing this data to my php file via an ajax call: {"html":"<div>I'm a div!!</div>"}
I want to preface this next statement by saying that I do understand the reasons for not saving json to a database, but it does have a use here.
When I save this data to the database field, the field is empty. Now see this:
$in1 = file_get_contents('php://input'); //from ajax
var_dump($in1);
$in2 = '{"html":"<div>I\'m a div!!</div>"}';
var_dump($in2);
value of my ajax call:
string(33) "{"html":"<div>I'm a div!!</div>"}"
string(33) "{"html":"<div>I'm a div!!</div>"}"
Perfectly the same! Yet, $in2 will save to the database just fine!! While $in1 yields an empty field!!
To be certain, consider this:
if ($in1 === $in2) { echo "They're equal!"; }
Go figure...they're exactly equal, yet one will save correctly and the other won't. Amazing.
Further: mysqli does not have this issue, so that narrows it down to being a PDO issue.
$query = "UPDATE plugin_instances SET config=(?) WHERE id=2";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $in1);
$stmt->execute(); //correct value in the db!!
I have now removed everything and this is the whole php file.
Non-working output
Working output
The only difference between these two is the result of $stmt->rowCount().
The sample that correctly updates the field says int(0) and the one that empties it says int(1).
$db = new PDO('mysql:host=localhost;dbname=disarray', 'root', 'temp');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
var_dump($db);
$params = [':foo'=>'{"html":"<div>I\'m a div!!</div>"}'];
var_dump($params);
$params = [':foo'=>file_get_contents('php://input')];
var_dump($params);
$query = "UPDATE plugin_instances SET config=:foo WHERE id=2";
var_dump($query);
try {
$stmt = $db->prepare($query);
var_dump($stmt);
$stmt->execute($params);
var_dump($stmt);
var_dump($stmt->rowCount());
}
catch (PDOException $e) {
echo $e->getMessage();
}
Massive sigh. The issue is with my javascript, not php. I was testing this out in my api, which several things make calls to throughout my somewhat large app. There was an additional ajax call being made that I was unaware of (it was left in the code by error) and it wasn't sending any data, so my test script in the api was running with no data and thus emptying the database field right after I wrote to it. Of course, entering the data directly into the script worked out great because both calls were doing the same thing.
It is unfortunate that when I stripped down my php code, I used the same filename/location.
More unfortunate is that the success of the mysqli threw me even further off, solidifying in my mind that the issue was with PDO. It turns out that it was just changing which ajax call was responded to last.
And thus, I have hopefully learned not to have so much tunnel-vision when going through the debugging process.
Related
I have a simple form that needs a list of stops in the textarea and returns an id for each on the right hand side. This is my screenshot on localhost...I have the same table names, column names, number of records on both localhost and live server.
Here's the screenshot of the same page with same query on live server...
Here's the code I am using on both pages
$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);
if(isset($_POST["busnumber"], $_POST["busroute"])){
$stops = explode(PHP_EOL, $_POST["busroute"]);
$sql = 'SELECT * FROM stops WHERE stop_name LIKE :stop';
$statement = $conn->prepare($sql);
$statement->setFetchMode(PDO::FETCH_ASSOC);
foreach($stops as $stop){
$statement->bindValue(':stop', $stop);
$statement->execute();
$results = $statement->fetchAll();
foreach($results as $result){
echo $result['stop_id'].' '.$result['stop_name']."</br>";
}
}
}
As you can see, it returns the ID of the last row only on the live server. Can someone please tell me how this is possible and what I am missing?
EDIT 1
Notice what happens when I reverse the data entered in the text area
The localhost shows both the ids now
Guess what the server shows after reversing? Only the LAST ROW!
You don't need setFetchMode(). In the time I've used PDO I always had the best results with just using bindParam() and fetch() with the most default setup of PDO, which means just setting the errormode to exception and charset to utf8 like this:
try
{
$con = new PDO("mysql:host=".$host.";dbname=".$db_name, $user, $password);
}
catch(PDOException $e){
die("ERROR ". $e->getMessage());
}
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->exec("SET NAMES utf8");
Fetching any results like this
while($r = $statement->fetch())
{
echo $r['id'];
}
Any time when someone has used a different set up, I've noticed they've faced problems.
Try this, perhaps.
This is very simple. Please check your live db via phpmyadmin if you have access and from phpmyadmin run your queries like you are running it from php code. May be you have some restrictions of mysql or php on live. And also check your db versions on localhost and live with php versions too. Let me know the results of phpmyadmin queries thanks!
Just guessing the problem. I don't really think if this answer is correct. So please pardon me in advance.
PDOStatement::fetchAll() returns an array that consists of all the rows returned by the query. From this fact we can make two conclusions:
This function should not be used, if many rows has been selected. In
such a case conventional while loop ave to be used, fetching rows
one by one instead of getting them all into array at once. "Many"
means more than it is suitable to be shown on the average web page.
This function is mostly useful in a modern web application that
never outputs data right away during fetching, but rather passes it
to template.
Source: PDO Tutorial
I FIXED the error. I have answered it in detail on a different post and I am linking to that post from HERE Thank you all for your time and answers
I made a simple update query using PDO in PHP :
$pdos = connect_db();
$pdos->beginTransaction();
try {
$query = "UPDATE `myo`.`question` SET `intitule` = 'Question azeerrr' WHERE `question`.`id` = 1";
$pdo = $pdos->prepare($query);
$pdo->execute();
return $pdo->rowCount();
catch (Exception $e) { print_r ($e); exit (); }
Which doesnt seem to work (the data is not changed in the database) even though i get no error message and even receive "1" from rowCount() meaning the update was successful.
And also, if i copy and paste this query in PHPmyadmin and run it, it works and the row is modified,
Could it be something about rights to execute an UPDATE from my website and not beeing the same as when you are logged into PHPmyadmin?
EDIT :
FIXED : thanks for you help : PDO::commit(); was needed to close my PDO::beginTransaction();
As you begin a transaction with $pdos->beginTransaction(); I'm pretty sure that you need commit it using $pdos->commit();
See the documentation for more information.
If you are using PDO's transaction, you must commit your changes to apply the modifications in your database.
See PDO::commit() function.
Even if you update a record with new values or with existing values, MySql always returns 1 in both the cases....
I am using a PHP PDO driver for an application that uses Apache Cassandra and I cannot fetch the information I need. Is anything obviously wrong?
$db = new PDO('cassandra:host=localhost;port=9160');
$db->exec("USE project");
$st = $db->prepare("SELECT fname FROM users WHERE email=:em;");
$st->bindValue(':em', 'email1#gmail.com', PDO::PARAM_STR);
$st->execute();
print_r($st->fetch(PDO::FETCH_ASSOC));
Nothing is printed to the window. The table users was created with the email column as the primary key. I had no trouble inserting and updating information to the users table in my app, but still cannot figure out how to fetch single values successfully. Similarly, when I fetchAll() with some query I can print the arrays (rows) to the screen but cannot index them to grab specific values. Maybe there is some detail about cassandra that I am missing?
Do the following:
Check the return values of each method.
Check if there is an error after executing the statement.
Check the error log of your php interpreter.
$db = new PDO('cassandra:host=localhost;port=9160');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->exec("USE project");
$st = $db->prepare("SELECT fname FROM users WHERE email=:em;");
if ($st == false) {
print_r($db->errorInfo())
exit;
}
if ($st->bindValue(':em', 'email1#gmail.com', PDO::PARAM_STR) == false) {
print_r($db->errorInfo())
exit;
}
if ($st->execute() == false) {
print_r($db->errorInfo())
exit;
}
print_r($st->fetch(PDO::FETCH_ASSOC));
I had a similar problem. I used YACassandraPDO. Unfortunately when use it with php 5.4 I get error 502. Decided to write his or her library that allowed to work with Cassandra binary protocol without experiencing similar problems.
In the future, I plan to speed it up.
Maybe it will help you. Syntax like PDO.
I'm a beginner programmer, and I've installed XAMPP with the intention of learning a bit of PHP. I have a working knowledge of SQL.
I've been following the PHP tutorial at w3schools. The problem I am currently having is this: I'm using the script here to create a database and a table within it. What I'm using is almost verbatim, except I've replaced the user with "root" and I've deleted the password.
After running the script through the browser, the database my_db appears in the datafile for mysql in XAMPP.
However, there is no sign of a table, and when I try to select the table, I get
Table 'my_db.persons' doesn't exist
What is going on? Is there something wrong with the code I took verbatim, or is it something with permissions?
It's weird that the database is created but not a table...
What has happenned is that your "CREATE TABLE" will have failed.
Get into the habit of checking for errors after EVERY mySQL query (in your code): there are some times you can ignore errors, but it's rare. So program alonghte lines of:
Create query: $sql =
Set parameters: $aParams = (or bind paramters)
Execute query
If errors
If debug: Show error and query
If live: Log error and query
I'm not giving the solution is code, as one problem with the tutorial you follow is that it uses mysql_() functions that are going to be depreciated shortly. You should use PDO (PHP Database Objects) or mysqli() functions otherwise your code will not work in a few releases time.
With PDO, you can set error handling to use exceptions, and you wrap every call in try {} catch {} and this makes the habit of catching and reporting errors very easy.
$sql = 'CREATE TABLE....';
$aParams = array(
':param_name' => $param_value,
':param_name2' => $param_value2
);
try {
$stmnt = $db->prepare($sql);
$stmnt->execute($aParams);
$stmnt = null;
} catch (Exception $e) {
// Error log here; $e contins line of error and the actual error, you have $sql and $aParams
LogDBError($e, $sql, $aParams);
}
I'm trying to get into PDO details. So I coded this:
$cn = getConnection();
// get table sequence
$comando = "call p_generate_seq('bitacora')";
$id = getValue($cn, $comando);
//$comando = 'INSERT INTO dsa_bitacora (id, estado, fch_creacion) VALUES (?, ?, ?)';
$comando = 'INSERT INTO dsa_bitacora (id, estado, fch_creacion) VALUES (:id, :estado, :fch_creacion)';
$parametros = array (
':id'=> (int)$id,
':estado'=>1,
':fch_creacion'=>date('Y-m-d H:i:s')
);
execWithParameters($cn, $comando, $parametros);
my getValue function works fine, and I get the next sequence for the table. But when I get into execWithParameters, i get this exception:
PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in D:\Servidor\xampp_1_7_1\htdocs\bitacora\func_db.php on line 77
I tried to modify the connection attributes but it doesn't work.
These are my core db functions:
function getConnection() {
try {
$cn = new PDO("mysql:host=$host;dbname=$bd", $usuario, $clave, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
$cn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
return $cn;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
function getValue($cn, $comando) {
$resul = $cn->query($comando);
if (!$resul) return null;
while($res = $resul->fetch()) {
$retorno = $res[0][0];
break;
}
return $retorno;
}
function execWithParameters($cn, $comando, $parametros) {
$q = $cn->prepare($comando);
$q->execute($parametros);
if ($q->errorInfo() != null) {
$e = $q->errorInfo();
echo $e[0].':'.$e[1].':'.$e[2];
}
}
Somebody who can shed a light for this? PD. Please do not suggest doing autonumeric id, cause i am porting from another system.
The issue is that mysql only allows for one outstanding cursor at a given time. By using the fetch() method and not consuming all the pending data, you are leaving a cursor open.
The recommended approach is to consume all the data using the fetchAll() method.
An alternative is to use the closeCursor() method.
If you change this function, I think you will be happier:
<?php
function getValue($cn, $comando) {
$resul = $cn->query($comando);
if (!$resul) return null;
foreach ($resul->fetchAll() as $res) {
$retorno = $res[0];
break;
}
return $retorno;
}
?>
I don't think PDOStatement::closeCursor() would work if you're not doing a query that returns data (i.e. an UPDATE, INSERT, etc).
A better solution is to simply unset() your PDOStatement object after calling PDOStatement::execute():
$stmt = $pdo->prepare('UPDATE users SET active = 1');
$stmt->execute();
unset($stmt);
The problem seems to be---I'm not too familiar with PDO--- that after your getValue call returns, the query is still bound to the connection (You only ever ask for the first value, yet the connection returns several, or expects to do so).
Perhaps getValue can be fixed by adding
$resul->closeCursor();
before the return.
Otherwise, if queries to getValue will always return a single (or few enough) value, it seems that using fetchAll will be preferred.
I just spend 15 minutes googling all around the internet, and viewed at least 5 different Stackoverflow questions, some who claimed my bug apparently arose from the wrong version of PHP, wrong version of MySQL library or any other magical black-box stuff...
I changed all my code into using "fetchAll" and I even called closeCursor() and unset() on the query object after each and every query. I was honestly getting desperate! I also tried the MYSQL_ATTR_USE_BUFFERED_QUERY flag, but it did not work.
FINALLY I threw everything out the window and looked at the PHP error, and tracked the line of code where it happened.
SELECT AVG((original_bytes-new_bytes)/original_bytes) as saving
FROM (SELECT original_bytes, new_bytes FROM jobs ORDER BY id DESC LIMIT 100) AS t1
Anyway, the problem happened because my original_bytes and new_bytes both where unsigned bigints, and that meant that if I ever had a job where the new_bytes where actually LARGER than the original_bytes, then I would have a nasty MySQL "out of range" error. And that just happened randomly after running my minification service for a little while.
Why the hell I got this weird MySQL error instead of just giving me the plain error, is beyond me! It actually showed up in SQLBuddy (lightweight PHPMyAdmin) when I ran the raw query.
I had PDO exceptions on, so it should have just given me the MySQL error.
Never mind, the bottom line is:
If you ever get this error, be sure to check that your raw MySQL is actually correct and STILL working!!!
A friend of mine had very much the same problem with the xampp 1.7.1 build. After replacing xampp/php/* by the 5.2.9-2 php.net build and copying all necessary files to xampp/apache/bin it worked fine.
If you're using XAMPP 1.7.1, you just need to upgrade to 1.7.2.