I'm trying to use the answer given at : How to get ID of the last updated row in MySQL?
I want the ID of the row which was updated. But the following doesn't work giving the error
Fatal error: Call to a member function fetch_assoc() on a non-object.
$query = "
SET #update_id := 0;
UPDATE locations SET owner_player_id='$player_id', id=(SELECT #update_id := id)
WHERE game_id='$game_id' LIMIT 1;
SELECT #update_id;
";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
$location_id = $row['update_id'];
Thanks
Edit:
$mysqli->error gives You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE locations SET owner_player_id='5', id=(SELECT #update_id := id) WHERE gam' at line 1
Your SQL has failed, that is why your subsequent fetch_assoc() method is also failing.
You will want to look into running your query without using the variables (#update_id) as you are intending, and look at using mysqli::$insert_id.
Without knowing what it is you are trying to do exactly, it's hard to be more specific.
Have to use mysqli::multi_query not mysqli::query. Also then have to use mysqli::next_result and mysqli::store_result to get the correct data.
$query = "SET #update_id := 0; UPDATE locations SET owner_player_id='$player_id', id = ( SELECT #update_id := id ) WHERE game_id='$game_id' LIMIT 1; SELECT #update_id;";
$mysqli->multi_query($query) or die($mysqli->error);
$mysqli->next_result();
$mysqli->next_result();
$result = $mysqli->store_result();
$row = $result->fetch_assoc();
$location_id = $row['#update_id'];
Related
I am trying to update 1 row of a mysql database and then get its content. without a chance of 2 threads updating at the same row
UPDATE models
SET `modelInfoProcessing` = (#cur_value := `id`)*0 + 1
WHERE (`modelInfoProcessing` = -1 OR `modelInfoProcessing` = 0) AND `modelInfoAge`<1
LIMIT 1;
SELECT #cur_value;
works. It updates modelInfoProcessing to 1 so no other thread will update and then returns the id. However when I try
UPDATE models
SET `modelInfoProcessing` = (#cur_value := `id`)*0 + 1
WHERE (`modelInfoProcessing` = -1 OR `modelInfoProcessing` = 0) AND `modelInfoAge`<1
LIMIT 1;
SELECT `id`,`shop`,`file`,`modelInfo`,`modelInfoAge`
FROM models
WHERE `id`=#cur_value
LIMIT 1;
to get more then just the id back it works from phpmyadmin but not from php mysqli. Any idea what the best way to do this is? I can't just lock the entire row or table as I need the rest of the row to be editable by different threads just need to make sure I have a way of knowing if things are trying to access to handle collisions programmatically.
My PHP Code:
$query='UPDATE models SET `modelInfoProcessing` = (#cur_value := `id`)*0 + 1 WHERE (`modelInfoProcessing` = ? OR `modelInfoProcessing` = 0) AND `modelInfoAge`<' . MODEL_INFO_AGE . ' LIMIT 1; SELECT `id`,`shop`,`file`,`modelInfo`,`modelInfoAge` FROM models WHERE `id`=#cur_value LIMIT 1;';
$stmtNextTask = $conn->stmt_init();
$stmtNextTask->prepare($query);
$stmtNextTask->bind_param('i',$minLevel);
$stmtNextTask->bind_result($currentTask,$shop,$file,$modelInfo,$modelInfoAge);
echo $stmtNextTask->error;die();
returns
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT `id`,`shop`,`file`,`modelInfo`,`modelInfoAge` FROM models WHERE `id`=#cur' at line 1
but as I said it does run in php my admin
The solution to this problem might be a simple over sight of mine.
I am trying to run a MYSQL query stored as a string in PHP. The query runs fine using DBM tool like Navicat but returns false in my PHP development enviorment. Is there something I've over looked?
SET #running_sum = 0;
SELECT
TID,
SumTotal,
T.`Freight`,
T.`Insurance`,
T.`Discount`,
CONCAT(
'$',
FORMAT(
#running_sum :=#running_sum + SumTotal + T.`Freight` + T.`Insurance` - T.`Discount`,
2
)
) AS 'Running Total'
FROM
(
SELECT
TID,
SUM(Quantity * UnitNetValue) AS SumTotal,
T.`Freight`,
T.`Insurance`,
T.`Discount`
FROM
Transactions T
JOIN `Transactions_Products` P ON T.TransactionID = P.TID
WHERE
(
T.TemplateName = ''
OR T.TemplateName IS NULL
)
AND T. STATUS = 1
GROUP BY
TransactionID
) AS T;
I am executing the query like this;
$result = mysql_query($this->query);
$this->query is a string which holds the above query, as it is displayed to you above.
The problem is mysql_query() doesn't support multiple queries. Your SET #running_sum = 0; is considered a separate query and so you'll have to execute that first:
$result1 = mysql_query("SET #running_sum = 0;");
$result2 = mysql_query($this->query); // <-- without the SET ... query
From the Manual:
mysql_query() sends a unique query (multiple queries are not supported)
Side note: The mysql_* library is deprecated, it is recommended to upgrade to a modern MySQL library such as PDO or MySQLi.
There is also possible to use multi_query method instead of query of MySQLi:
$query = "SET #running_sum = 0; SELECT ...";
$db_link->multi_query($query);
put variable like this. i guess it should work.
mysql_query("SELECT #i:=0");
mysql_query("UPDATE table_name SET id = #i:=#i+1");
I'm using pdo and I have a query like this
$stmt=$db->query("SET #update_id := 0;
UPDATE table SET column = something, id = (SELECT #update_id := id)
WHERE condition
LIMIT 1;
SELECT #update_id;");
It is supposed to update a row and return the id.
I'm sure the query itself is working because I ran it in phpmyadmin and it returned a column named #updated_id with the value of the updated row like this:
| #updated_id |
|------------------|
| *correct id* |
I want to fetch the value of #updated_id and store it in a php variable.
I tried $stmt->fetchColumn(); and $stmt->fetchColumn(); but I get SQLSTATE[HY000]: General error.
I've been searching for something to work but I can't find anything.
so anybody knows how to store the value of #updated_id in a php variable?
thanks
As Petah suggested in the comments, you need to iterate through each rowset until you get to the SELECT statement you want to fetch.
Given you example above, this should work:
$stmt=$db->query("SET #update_id := 0;
UPDATE table SET column = something, id = (SELECT #update_id := id)
WHERE condition
LIMIT 1;
SELECT #update_id;");
$stmt->nextRowset();
$stmt->nextRowset();
$update_id = $stmt->fetchColumn();
<?php
$id = 123;
$query = "UPDATE table SET column = something, id = ? WHERE condition LIMIT 1;"
$st = $db->prepare($query);
$st->execute(array(
$id
));
$result = $st->fetch(PDO::FETCH_ASSOC);
var_dump($result);
Do you tried something like this?, fetching the modified statement
In my PHP file, I use this line to pull data from my mySQL database:
$query = "SET #rank=0; SELECT #rank:=#rank +1 as rank, Blah Blah...";
If I check the SELECT statement in phpMyAdmin's SQL window (without $query= ) it works fine.
But, if I use it in PHP, then I get an error. It doesn't like the "SET #rank=0;" bit. Is there a way to use "SET #rank=0;" when it's in "$query=" ? Is there a workaround?
The rest of the code is standard stuff for pulling data from a db:
public function getmyData() {
$mysql = mysql_connect(connection stuff);
$query = "SELECT #rank:=#rank +1 as rank, formatted_school_name, blah blah";
$result = mysql_query($query);
$ret = array();
while ($row = mysql_fetch_object($result)) {
$tmp = new VOmyData1();
$tmp->stuff1 = $row-> stuff1;
$tmp->stuff2 = $row->stuff2;
$ret[] = $tmp;
}
mysql_free_result($result);
return $ret;
}
Update: I'm trying to use Amerb's suggestion of using multi-query. I concatenated the query like so:
$query = "SET #rank = 0";
$query .= "SELECT #rank:=#rank +1 as rank...
I changed the result to:
$result = $mysqli_multi_query($query);
But, it's failing for some reason. I'm on a machine running PHP 5.2. Any suggestions?
This guy here seems to have a way of setting the variable in the same query to zero. I don't have MySQL set on up on this machine to try it, though.
Here's the query he suggests in his blog post:
select #rownum:=#rownum+1 ‘rank’, p.* from player p, (SELECT #rownum:=0) r order by score desc limit 10;
(Is there some homework assignment coming due somewhere having to do with computing ranks? This is the third question I've seen on this in two days.)
Are you checking for duplicate scores?
Try executing it as 2 separate successive queries.
You have to enable the use of multiple queries in one, but i forgot how do do this at the moment. It's a security feature.
Use mysql_multi_query() or rather mysqli_multi_query() instead of mysql_query()
I have a query:
$result = mysql_query("CREATE VIEW temporary(IngList) AS (
SELECT DISTINCT (r1.Ingredient)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'
AND r1.Ingredient = r2.Ingredient)
SELECT COUNT(*) FROM temporary");
I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. I know this code works without the SELECT COUNT(*) because I checked my database and the view is created.
Yet this code throws the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) FROM temporary' at line 1
I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.
From the mysql_query documentation:
mysql_query() sends a unique query (multiple queries are not supported)...
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
WHERE r.country = '%s'
AND EXISTS(SELECT NULL
FROM recipes r2
WHERE r2.Country = '%s'
AND r1.Ingredient = r2.Ingredient)",
$temp, $temp2);
$result = mysql_query($sql);
For starters you have two statements. What you're writing looks more like a stored procedure. Even if it worked, you would need a semicolon at the end of the first statement. And another statement somewhere saying "DROP VIEW ...." when you are done.
And a temp view is a bit of a non sequitur. I can't find any reference to "CREATE VIEW temporary". Or maybe it's to create a view named temporary with an argument? Views don't take arguments.
I think you might get what you want with a semi-simple SQL statement something like:
$result = mysql_query(
"SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'");