How to do an if-else condition in sql and php? - php

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();
}

Related

Request count sql in symfony return boolean not integer

in symfony I want to get count from table in database but it return boolean !
$sql = "....";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$res = $stmt->fetchAll();
foreach ($res as $key => $value) {
$resultatRequete = "SELECT count(id) from inscriptions where seance_id =
'".$value['seance_id']."' and is_confirmed is not null";
$stmt2 = $em->getConnection()->prepare($resultatRequete);
$result_req = $stmt2->execute();
$res[$key]["count_inscrit"] = $result_req ;
}
return $res;
The execute method return a boolean in order to success/failure, then you should fetch the result, as example:
// you can check on success ...
$success = $stmt2->execute();
$result_req = $stmt2->fetch();
$res[$key]["count_inscrit"] = $result_req ;
Hope this help

MySQL data is not returned in PHP variables

<?php
require("config.inc.php");
$query = "Select 1 FROM dogs WHERE dog_id = :iddog ";
$query_params = array(':iddog'=> $_POST['iddogPHP2']);
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
$dono2 = $result ->fetchColumn(1);
$raca2 = $result ->fetchColumn(2);
$sex2 = $result ->fetchColumn(3);
$estado2 = $result ->fetchColumn(4);
$query = "Select * FROM dogs WHERE dispon = 'Sim' AND dono != '$dono2' AND estado = '$estado2' AND raca = '$raca2' AND sex != '$sexo2' ";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
?>
This is my code and the problem is that the variables dono2, raca2, gender2 are not getting the values from the database. What's wrong in it?
Instead of SELECT 1 you need to use SELECT * in your first query.
$query = "Select * FROM dogs WHERE dog_id = :iddog ";
The following returns an array of results
$rows = $stmt->fetchAll();
So you would need to specify which one to reference.
$dono2 = $rows[0]['dono'];
$raca2 = $rows[0]['raca'];
$sexo2 = $rows[0]['sex'];
$estado2 = $rows[0]['estado'];
$rows = $stmt->fetchAll();
fetch all rows from database you need to specify which row you want if it's multiple row
$dono2 = $rows[0]['dono'];
$raca2 = $rows[0]['raca'];
$sexo2 = $rows[0]['sex'];
$estado2 = $rows[0]['estado'];
Try this code
$stmt = $db->prepare($query);
$stmt->bindParam(':iddog', $_POST['iddogPHP2']);
$stmt->execute($query_params);
$dono2 = $stmt ->fetchColumn(1);
$raca2 = $stmt ->fetchColumn(2);
$sex2 = $stmt ->fetchColumn(3);
$estado2 = $stmt ->fetchColumn(4);

SELECT query with bindParam returns nothing

public function showSingleVisit(){
//echo $this->doctorID; the printed 1111
//$this->doctorID = 1111;
$db = connect_db();
$query = "SELECT * FROM visit WHERE doctorID = :doctorID";
$result = $db->prepare($query);
$result->bindParam(':doctorID', $this->doctorID);
$result->execute();
return $result;
}
This query doesn't return any row but when putting $this->doctorID = 1111 I get the rows that is wanted. I use bindParam in INSERT query in this class and works correctly. What's the problem?
UPDATE:
class visit{
//define public varibles
public function showSingleVisit(){
$db = connect_db();
$query = "SELECT * FROM visit WHERE visit = 0 AND doctorID = :doctorID AND patientID = :patientID";
$result = $db->prepare($query);
$result->bindParam(':patientID', $this->patientID);
$result->bindParam(':doctorID', $this->doctorID);
$result->execute();
return $result;
}
}
Here's how I call the function un the other page:
$visit = new visit;
$visit->doctorID = $auth->user->IDNo;
$visit->caseNo = $_SESSION['caseNo'];
$result = $visit->showSingleVisit();
if($result){
while($row = $result->fetch()){
echo'<p>
<label>Date:</label>
<span>'.$row->visitDate.'</span>
</p>';
}
}
else{
echo "No exists!";
}
Neither it shows any dates, nor it prints "No exists!".
you have to specify the type of the param :
$result->bindParam(':doctorID', $this->doctorID, PDO::PARAM_INT);
look at here :
http://php.net/manual/fr/pdostatement.bindparam.php
since doctorID is integer, you should add data_type to INT. it look like this
$result->bindParam(':doctorID', $this->doctorID, PDO::PARAM_INT);

How to write a string variable inside mysql query?

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";

Function not updating database

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?

Categories