Trouble executing transaction in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble getting a mysql transaction to work through PHP. I'm fairly new to PHP and very new to mysql.
If I take the var_dump of $query and try to run it through phpmyadmin it works fine.
$description =
mysql_real_escape_string($_REQUEST['description']);
$query = 'BEGIN;
INSERT INTO indiespark.tasks
(description, owner_user_id)
VALUES ("' . $description . '", '
. $user->user_id . ');
SET #task_id = LAST_INSERT_ID();
INSERT INTO indiespark.projecttasks
(task_id, project_id)
VALUES (#task_id, ' . $project->project_id . ');
COMMIT;';
$result = mysql_query($query);
var_dump($query);
var_dump($result);
if ($result) {
return viewproject();
} else {
throw new Exception('database error');
}

mysql_query doesn't support sending multiple queries in one call. Use separate calls.

Related

Php execute lines [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to execute these lines When I click on the button:
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
It will pick another word from the database using that query.
The button in the HTML page:
echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
How can I do this?
To actually run the query you can use MySQLi. Here is a good beginners guide.
//Connect to database.
$db = new mysqli('localhost', 'user', 'pass', 'database');
//The code you provided.
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
//Run the query.
$result = $db->query($sql);
//Get the first (and presumably only) row
$row = $result->fetch_assoc();
//Output the word.
echo $row['word'];
//Free up some memory.
$result->free();
To get the link to work, you need to have the name of the page. You can either hardcode it, or use $_SERVER['PHP_SELF']:
echo 'Play again!'
(I don't know what you use the $n for so I just left it in there.)

Why my queries work weirdly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I get this two queries with the same variables :
$query = 'UPDATE payee SET payee="oui", datePaiement=\'' . $datePaiement . '\',paiement="'.$paiement.'", typePaiement="' . utf8_decode($moyenPaiement) . '" WHERE id_commande=' . $commande->getNum() . '';
$connexion->exec($query);
$query2 = 'UPDATE commande SET mpaiement="' . utf8_decode($moyenPaiement) . '",pxttc="'.$paiement.'" WHERE noCommande=" . $commande->getNum() . "';
$connexion->exec($query2);
For the first one, my $paiement isn't save in my DB. I get a $paiement = 0 while in my second one $paiement is save as I want.
I have the same pattern in my second query $moyenPaiement, moyenPaiement is save as I want but he is not save in my first query.
Sorry for my explanation, it's maybe confused.
You should not combine both single and double quotes, it will be confusing. Try this:
$query = 'UPDATE payee SET payee="oui", datePaiement="' . $datePaiement . '", paiement="'.$paiement.'", typePaiement="' . utf8_decode($moyenPaiement) . '" WHERE id_commande="' . $commande->getNum() . '"';
$connexion->exec($query);
$query2 = 'UPDATE commande SET mpaiement="' . utf8_decode($moyenPaiement) . '", pxttc="'.$paiement.'" WHERE noCommande="' . $commande->getNum() . '"';
$connexion->exec($query2);
Later edit:
Don't forget to print your queries if something weird happens. These queries can be tested in phpMyAdmin too.
print_r($query);

pdo query not running [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i can't seem to figure out why this query isn't running
if ( $productName && $productDescription && $productPrice ) {
// SQL
// UPDATE `prostud_tristurion`.`products` SET `product_title` = 'ajax test', `product_description` = 'Was certainty remaining engrossed applauded sir how discovery.', `product_price` = '524' WHERE `products`.`product_id` = 10;
try {
$query = "update products set product_title = :pName, product_description = :pDescription, product_price = :pPrice, where product_id = :pid";
//prepare query for excecution
$stmt = $con->prepare($query);
//bind the parameters
$stmt->bindParam(':pid', $id);
$stmt->bindParam(':pName', $productName);
$stmt->bindParam(':pDescription', $productDescription);
$stmt->bindParam(':pPrice', $productPrice);
// echo "$productPrice / $productDescription / $productName / $id\n $stmt";
var_dump($_POST);
// Execute the query
if ($stmt->execute() ) {
echo "Record was updated.";
} else {
die('Unable to update record.');
}
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
all i get is Unable to update record.
var_dump($_POST);
is looking good
You have an errant comma at product_price = :pPrice, where
If your code reaches the die statement then you have exceptions turned off (not recommended) but you can get the error message from the database (to log or echo) with $stmt->errorInfo()

Send message forward along the info [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Send message forward along the info. the info that I select in shell or worse than for the bare lies into the database again what I need .. but the problem is such that it appears with this error:
error 1: Commands out of sync; you can't run this command now
PHP/MYSQLI
if($stmt = $this->mysqli->prepare(' SELECT id, idunik, fra, message, datoTime FROM pm WHERE id = ?' ))
{
$stmt->bind_param('i', $id);
$id = $_GET['id'];
$stmt->execute();
$stmt->bind_result($id, $idunik, $fra, $message, $datoTime);
while($stmt->fetch())
{
if ($stm = $this->mysqli->prepare('INSERT INTO pm (idunik, title, fra, til, message, datoTime) VALUES (?, ?, ?, ?, ?, NOW())')) {
$stm->bind_param('issss', $idunik, $title, $fra, $til, $message);
$idunik = $idunik;
$title = $title;
$fra = $_SESSION["id"];
$til = $fra;
$message = $_POST["tekst"];
header('Location: /besked/' . $_SESSION["id"] . '/');
$stm->execute();
$stm->close();
} else {
echo 'error 1: ' . $this->mysqli->error;
}
}
$stmt->close();
}
else
{
echo 'error 2: ' . $mysqli->error;
}
so the problem is such that when carrying it into the database then comes the error.
Unfortunately your question it not perfectly understandable, so I'm just going to give some general info related to the the error you receive (as I presume this is what you want to resolve):
error 1: Commands out of sync; you can't run this command now
Because Mysqli uses unbuffered queries by default on prepared statements, while Mysqli has previous results to be fetched you cannot call another procedure.
To fix this, and avoid the error you are getting, use mysqli_store_result() to get all rows first, then perform the next query.
mysqli_store_result():
http://php.net/manual/en/mysqli.store-result.php
Transfers the result set from the last query on the database
connection represented by the link parameter to be used with the
mysqli_data_seek() function.
And more info on "commands-out-of-sync":
http://dev.mysql.com/doc/refman/5.1/en/commands-out-of-sync.html

Store Facebook user data in database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to store this in the database but it's not working. Is there a better way to do this? It's picking up the user session but it won't insert the data into the database.
if($user)
{
$user_interest = $facebook->api('/me/interests');
for($i = 0; $i < sizeof($user_interest[data]); $i++)
{
$name = $user_interest[data][$i]['name'];
$category = $user_interest[data][$i]['category'];
$categoryId = $user_interest[data][$i]['id'];
$created_time = $user_interest[data][$i]['created_time'];
$strsql = "INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES(\"$fbId\",\"$categoryId\",\"$category\",\"$name\",\"$created_time\")";
mysql_query($strsql, $connect) or die(mysql_error());
$chkrow1 = mysql_affected_rows($connect);
}
"INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES(\"$fbId\",\"$categoryId\",\"$category\",\"$name\",\"$created_time\")";
Strings in MySQL (and other SQL) use single quotes.
"INSERT INTO `interests`(`fbId`,`categoryId`,`category`,`name`,`created_time`)
VALUES('$fbId', '$categoryId', '$category', '$name', '$created_time')";
Also, escape all of those values using mysql_real_escape_string before interpolating them, then stop using the deprecated mysql_ extensions and use parametrized queries with PDO.

Categories