I recently was fiddling with my code and ran into an issue and i cannot figure out where it is. I added the "Update site_sync SET test = test+1" code last, which is firing perfectly fine. but everything else seems to be catching somewhere and it's not throwing errors anywhere on my site.
function skynetInfect($db)
{
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
try { $que->execute();
$que->nextRowset();
while($row = $que->fetch(PDO::FETCH_BOTH))
{
if($row[0] == 'on')
{
$que->nextRowset();
$row2 = $que->fetch(PDO::FETCH_BOTH);
$x = $row2[0];
echo $x;
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql);
$que->bindParam('id', $rand);
try{ $que->execute();}catch(PDOException $e) { echo $e->getMessage();}
}
}
}catch(PDOExceptions $e) { echo $e->getMessage();}
}
You are overwriting $que inside the inner if conditional. That happens here:
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql); // use a variable name other than $que here
Also, is there any reason you are actually using PDO::FETCH_BOTH?
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
I don't think you can execute multiple queries using PDO
$que = $db->prepare($sql);
I don't see any parameter in your Query so why the prepare?
Related
What I want to do is a select query and then if it is true set a variable of a table called 'afiliadospadron' to 1 but if it is false (it do not return anything) just set the variable to 0. My code isn't working. Can somebody help me please?
$query= "select nrodoc from oct20 where nrodoc = 05463xx union select nrodoc from dic20 where nrodoc = 054631xx";
$query2= "update afiliadospadron set condVotar = '1' where nroDoc = '5463xxx' ";
//$query3= "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463xxx'";
if ($query) {
$query2;
} else {
echo "next time";
// $query3 = "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463192'";;
}
$statement = $conexion->prepare($query);
$statement = $conexion->prepare($query2);
//$statement = $conexion->prepare($query3);
$statement->execute();
try it
if ($result = $mysqli -> query($query)) {
$query2;
// Free result set
$result -> free_result();
}
code from: https://www.w3schools.com/php/func_mysqli_query.asp
The answer was
$statement = $conexion->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
if ($result = $result ) {
echo "yas";
$statement = $conexion ->prepare($query2);
$statement->execute();
// Free result set
} else {
echo "Wasnt found";
$statement = $conexion ->prepare($query3);
$statement->execute();
}
i am trying to add an if condition to mysql query to only update a certain field if condition is met, here is my code below, but i keep getting this error
ERROR
{ "error": { "message":SQLSTATE[HY093]: Invalid parameter number:
parameter was not defined } }
CODE
$ok = 1;
$sql = "UPDATE users SET
fn = :first,
ln = :last
";
if($ok == 1){
$sql .= ",phone = :phone";
}
$sql .= "WHERE users.id = :id";
Keep space between your concatenation,
$ok = 1;
$sql = "UPDATE users SET
fn = :first,
ln = :last";
if($ok == 1){
$sql .= ", phone = :phone ";
}
$sql .= " WHERE users.id = :id";
I think you are missing a space
$sql =
"UPDATE users SET
fn = :first,
ln = :last
,phone = :phoneWHERE users.id = :id"
I want to implement this query :
if(x=1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set $update where id=100";
mysql_query($query);
but I got an error, the Mysql can't execute the query ?
<?php
if($x==1){
$update = "close = '".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "update table1 set $update where id=100";
mysql_query($query);
?>
use this
<?php
if(x==1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query);
?>
try to use this code........
<?php
if($x==1){ // use $ for variables
$update = "close = '".$date."' "; //always concatenate variables
}
else {
$update = "open = '".$date."' ";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query, $connection); // don`t forget to add mysql connection
?>
Put your query in the quotes. Try following :
if(x==1){
$update = "close='".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "Update table1 set ".$update." where id=100";
mysql_query($query) or die(mysql_error());
Put mysql_error() to check what error are you getting from mysql
Replace
$query = Update table1 set $update where id=100;
to
$query = "Update table1 set ".$update." where id=100";
<?php
$db = new mysqli('localhost', 'root', 'root', 'chatting');
$query = "SELECT * FROM user WHERE state = 1 AND getp = 0";
$result = $db->query($query);
$num_result = $result->num_rows;
$mems = "";
for ($i = 0; $i < $num_result; $i++) {
$row = $result->fetch_assoc();
$mems = $row["userName"] . " " . $mems;
$query = "update `user` set `getp` = 1 where 'userName` = ".' $row["userName"] ';
$result = $db->prepare($query);
}
echo $mems;
?>
What I want I want to get all records that state = 1 and getp = 0, then inside loop and for every record I want to change the value of getp to 1, It's correct but don't know where is the wrong.
You forgot to execute() the update statement..
Also, note that this is not the correct way to prepare statements.. you will have to do something like this:
$query = $db->prepare("update `user` set `getp` = 1 where `userName` = :userName");
$query->bind_param(':userName',$row["userName"]);
$result = $query->execute();
Why doing it in a for loop? You could just do UPDATEuserSET getp = 1 WHERE state = 1 AND getp = 0;
While looping with for $i++? Why not while($row = $result->fetch_assoc()) { ... }?
However change the query to
$query = 'update `user` set `getp` = 1 where userName = "'. $row["userName"] .'"';
and don't forget to execute() it.
Please set your update command to:
$query = "update `user` set `getp` = 1 where `userName` = '". $row["userName"]. "'";
This query runs when I comment out the code above '}else{'. Have done so many like it and way more complex but I can't see where I'm tripping up.
(The page won't run like a syntax mistake. all vars match table)
Thanks.
Allen
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}
}
You're missing the closing } for the while loop.
missing } for while loop
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
}
else {
I think that your last bracket got misplaced. you will also need to move the last } just before else
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}