PHP Mysql concurrent - php

I am recently coding PHP with mysql. I have a problem with concurreny with Mysql
For example , if I got a result set from mysql like
$result = $data -> where("id > 5");
and meanwhile another page submit a query like "delete table ...".
Are there any data in $result which I have already got before?

I am not sure what your question is.
But if you set $result with data from the DB and then delete the corresponding data in the DB while the PHP code is still running, it will still be in the variable. But if you do another request to the DB it will not find it anymore.

Related

Avoid updating a same value in MYSQL and displaying the error in the client side

I have come across many questions and answers related to this kind of questions, but not for exactly this question.
Well, according to the official MYSQL documentation it says
If you set a column to the value it currently has, MySQL notices this
and does not update it.
But practically, when I update a same value to a MYSQL column, the query successfully executing and displaying "Your query has been successfully executed ".
So How do I exactly know that the query hasn't updated, when I update a column with the same value?
Also Is it possible to get an error message to the client (browser), when I try to update a same value, from the browser via a submit form to the MYSQL server via a backend language like PHP?
When updating a Mysql table with identical values nothing's really affected so rowCount will return 0.
Just create your PDO object with
<?php
$p = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
?>
and rowCount() will tell you how many rows your update-query actually found/matched.
Check this link also.
you may use like to avoid updating same value
UPDATE users
SET first_name = 'Michael'
WHERE id = 123
AND first_name IS DISTINCT FROM 'Michael';

How to output the MySQL EXPLAIN data in php?

I wonder if there is a way to output the EXPLAIN data from an MYSQL statement to PHP.
For studying I need to write a small application in PHP that outputs data via a SELECT query from a MYSQL DB. I have to output the EXPLAIN data, like searched rows etc., from this query as well. If I just set EXPLAIN in front of the SELECT, I get an Error.
Unfortunately I couldn't find a satisfying answer on the internet.
Due to I'm new to PHP, I would appreciate if someone can give me an example or smth like this.
Thank you guys!
You should be able to grab it using standard array tools. By using $result->fetch_array(MYSQLI_NUM); we can just grab the first return without a column name
$sql = 'EXPLAIN SELECT * FROM table WHERE condition = "condition"';
$result = $mysqli->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];

Why is SQL Server Express replying with replacement character 65533 in php?

I have a php script running on linux which queries a MSSQL Server Express database running on my dev machine (windows) using the php mssql driver.
I'm able to connect and select a database, and get no errors.
I put the query directly into a query window in MS SQL Server Management Studio and I get the correct data back. However, if I run the same query from my PHP script, the data that comes back has a bunch of "replacement characters", described here: http://www.fileformat.info/info/unicode/char/0fffd/index.htm
It looks like this: �
For instance, when querying for a list of IDs, eg "SELECT DISTINCT PageId FROM Pages", the query window is showing a list of IDs in the following format:
C961277D-D8BE-4337-82CF-003F6E7951E2
However, when I run the same query in PHP, this is the result:
'���K�#�t��#/
I AM getting the same number of results, so the SQL Server seams to be interpreting the query correctly, but in the results, the character length is wrong, none of the characters match, and the format is wrong. Anybody have any idea what is going on?
Thank you for your help.
Relevant code here:
$this->connection = mssql_connect(
$this->configuration['servername'],
$this->configuration['username'],
$this->configuration['password']);
$query = 'SELECT DISTINCT PageId FROM sf_CmsCtrlLinks';
$result = mssql_query($query);
while ($row = mssql_fetch_object($result)){
// Results in "page ID: 7��"�O�5,���"
echo "page ID: " . $row->PageId;
}
SQL Server returns GUIDs to PHP in binary format.
If you want it as a readable format, you can just make MSSQL pass it back to you as a string;
SELECT DISTINCT CONVERT(VARCHAR(38), PageId) as PageId FROM pages
If you don't need them readable, not casting them and just using them as an "opaque" id should work without problems.
I can't test it myself, but mssql_guid_string() may also work to convert them to readable form.

MySQL, PHP and failing UPDATE Command

I am experiencing something so basic, yet so annoying that I thought I had to put it out to the wider community to save my sanity.
I am using a table within a database to store some very basic data. There is only two columns, Id and Campaign. I only want to use a single row of the table, however, campaign will be updated at various points. I have set up the table as follows:
$sql = "CREATE TABLE IF NOT EXISTS TestCampaign(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Campaign CHAR(20))";
Initially I write to the table to insert a null CHAR in campaign:
$sql = "INSERT INTO TestCampaign (Campaign) VALUES ('None')";
The based on a specific text field being filled in on an html form followed by a submit button press I intended to do the update of the campaign field:
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = '1'";
$Test is the POSTED campaign name from the form. Unfortunately although the INSERT works fine the UPDATE doesn't. I have checked the permissions and I have ALL on this database. I have also checked the syntax with various sites and it seems that it is fine.
Interestingly I do not get an error when I echo:
echo " ".mysqli_error($con);
I'm sure I have made some basic error somewhere but I have been looking at it for so long and changing the syntax that I can't seem to spot it.
Any help would be appreciated.
UPDATE:
I have played around with the code and it seems as though the UPDATE code does work, however, It only works when it is the next line of code after the INSERT. In fact I have found that it works as long as it is not where I need it to be. I have it placed in 'if' statement that is run only on a specific button press on the form:
if(isset($_POST['TestID']))
{
Some Code;
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = '1'";
Some More Code;
}
I have checked the rest of the code in the 'if' statement and it seems solid.
Is this odd behaviour or have I missed something?
SOLVED
Finally found out what the problem was, it ended up that when exiting the first 'if' statement as expected the html form code was revisited which must have closed the connection to the database, when the button was pressed to run the second 'if' statement there was a connection to MySQL but no connection to the database I needed access to. A quick fix to re-connect and all works fine.
$sql = "UPDATE TestCampaign SET Campaign = '$Test' WHERE id = 1";
Is the only thing I think is wrong... What do you get with :
Select * from TestCampaign
have you set up any triggers based on this table name? After update, before update triggers may prevent you from storing the required data on the table.
Also, MySQL may use 0 as id value if you don't specify the value of the id at sending insert command. Are you sure the value of your id is 1 in the record you want to update? If you have run insert statements before on your table, the id may be a larger number than 1, because of the MySQL indices (I guess this may be the problem).
I don't know how exactly PHP statements are processed when sending them to MySQL, but I would recommend you to use PDO statements, the PHP syntax would look something like this:
$sql = $pdo->prepare("UPDATE TestCampaign SET Campaign = ? WHERE id = ?");
$sql->bindParam(1, $Test);
$sql->bindParam(2, 1);
$sql->execute();
Tutorial: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
I would also echo your $Test variable to check what is stored in it.
Hope it helps...
Finally found out what the problem was, it ended up that when exiting the first 'if' statement as expected the html form code was revisited which must have closed the connection to the database, when the button was pressed to run the second 'if' statement there was a connection to MySQL but no connection to the database I needed access to. A quick fix to re-connect and all works fine.

Oracle PHP update no response

I got a very weird problem with oracle today.
I setup a new server with xampp for developing, i activated mssql and oracle and everything was just fine until i tried to execute an update statement.
Every select, insert, etc is working fine with PHP 5.3.
I also can parse the statement and get a ressource id back, but when i try to execute the statement my whole site is not responding.
no error, nothing. just timeout until i restart the apache.
here the code... it's the test code, so there should be no problem at all.
$conn = oci_connect('***', '***', '***');
$query ="UPDATE CHAR*** SET TPOS = 14, ID = 5, DIFF = 'J' WHERE ***NR = '3092308' AND LA*** = '5'";
echo $query;
echo '<br>';
echo $stid = oci_parse($conn, $query);
oci_execute($stid, OCI_DEFAULT);
oci_free_statement($stid2);
Any hints or ideas? :-(
I already tried to reinstall the oracle instant client and another version. I am using 10g like our db at the moment.
best regards
pad
The row may be locked by another session. If this is the case, your session will hang until the other transaction ends (commit/rollback).
You should do a SELECT FOR UPDATE NOWAIT before attempting to update a row (pessimistic locking):
If the row is locked, you will get an error and can return a message to the user that this record is currently being updated by another session. In most cases an explicit message is preferable to indefinite waiting.
If the row is available, you will make sure no session modifies its content until you commit (and thus you will prevent any form of lost update).
There are other reasons why a simple update may take a long time but they are less likely, for instance:
When you update an unindexed foreign key, Oracle needs to acquire a lock on the whole parent table for a short time. This may take a long time on a busy and/or large table.
There could be triggers on the table that perform additional work.
For further reading: pessimistic vs optimistic locking.

Categories