Varchar values transform into 0 in php mysql query - php

I have a MySQL database containing tables with Arabic data (Varchar type exactly), I try to print a select query string thought php but it prints the values as zeros, Here's my query :
SELECT *,(
CASE WHEN `name_singular`= "قَائِدان" THEN "name_singular"
WHEN `name_twoA`= "قَائِدان" THEN "name_twoA"
WHEN `name_pluralA`= "قَائِدان" THEN "name_pluralA"
WHEN `name_twoO`= "قَائِدان" THEN "name_twoO"
WHEN `name_pluralO`= "قَائِدان" THEN
"name_pluralO" else NULL END) AS `field`
FROM `name` WHERE `name_singular`= "قَائِدان" OR
`name_twoA`= "قَائِدان" OR
`name_pluralA`= "قَائِدان" OR
`name_twoO`= "قَائِدان" OR
`name_pluralO`= "قَائِدان" LIMIT 0, 10 ;
and that's what i got as a result of printing the query string.
it works fine when executing it thought mysql directly, but when using php it returns null
SELECT *,
( CASE
WHEN `name_singular` = "0" THEN "name_singular"
WHEN `name_twoa` = "0" THEN "name_twoa"
WHEN `name_plurala` = "0" THEN "name_plurala"
WHEN `name_twoo` = "0" THEN "name_twoo"
WHEN `name_pluralo` = "0" THEN "name_pluralo"
ELSE NULL
end ) AS `field`
FROM `name`
WHERE `name_singular` = "0"
OR `name_twoa` = "0"
OR `name_plurala` = "0"
OR `name_twoo` = "0"
OR `name_pluralo` = "0"
LIMIT 0, 10
And that's the php code :
<?php $query = 'the query string above ';
echo $query; $array=mysql_fetch_object(mysql_query($query));
var_dump($array);
?>

Related

SQL Server query on php

I'm begginner in this field so I need help.
I want to get integer value from this select. When I run this query in sql server - it returns correct value (so that part is okay)
My idea is to get integer value of $select -> put it in $value variable and use it after that.
Here is working sql expression:
$select = "SELECT COUNT (*) broj (SELECT a.CaseNo as CaseNo,
b.Naruc as Naruc,
b.Opis as Opis,
b.Vrijednost as cijena,
CONVERT(VARCHAR(50),b.Datumpn, 127) as datum_predaje,
(case when b.Kn IS NULL then 0 when b.Kn IS not null then b.Kn end) as kat1,
(case when b.Kn2 IS NULL then 0 when b.Kn2 IS not null then b.Kn2 end) as kat2,
(case when b.Kn3 IS NULL then 0 when b.Kn3 IS not null then b.Kn3 end) as kat3,
a.DateInserted as datum_unosa
FROM [Therefore].[dbo].[za_slanje_maila] a, [Therefore].[dbo].[TheCase1] b
WHERE CONVERT(date,a.DateInserted) = '".$today."'
AND a.DateInserted < '".$today3."'
AND a.CaseNo = b.CaseNo
AND b.Naruc is not null
AND b.Opis is not null
AND b.Vrijednost is not null
AND b.Datum_predaje is not null) broj";
After that, I tryed this
$data_api = sqlsrv_query($conn, $select, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
And then I try to call
$value = $data_api['broj']
- but that is empty.
How can I assign that value to the variable $value?
Thanks.
sqlsrv_query() doesn't return an array yet, but statement resource (or false on error). You can then use this resource to get the data array with sqlsrv_fetch_array() function.

Yii2 issue to get the result for db query

I am using this command to get if the record already exist in the database;
$query = Yii::$app->db->createCommand("SELECT IF(EXISTS(SELECT * FROM `order_item`
WHERE `date` = '$date' AND `start_time` = '$starttime'), 1, 0)");
$result=$query->queryAll();
var_dump($result);exit;
Now the result I am getting for dump is like:
array(1) { [0]=> array(1) { ["IF(EXISTS(SELECT * FROM `order_item`
WHERE `date` = '2018-12-03' AND `start_time` = '10:15:00'), 1, 0)"]=> string(1) "0" } }
whereas I want the result as just 1 or 0
like
if ($result==1){
//do something;
}
as if I am running the same query in phpmyadmin - I am getting the result as 0 or 1
How I can achieve the same from the Query in Yii2.
queryAll() returns all fields from all rows as arrays. If you want to get single value from first field of first row, you need to use queryScalar().
$result = $query->queryScalar();

Validate mysql query if NULL

Is this query possible in mysql?
If query returns null, then set x = false else set x = true
Query
SET #s1 = (SELECT * FROM `wo_ticket_details_replay`
WHERE message NOT LIKE "[ SYSTEM%" AND `sv_number`='0715201569998')
CASE WHEN #s1 = NULL THEN
x = 'false'
ELSE
x = 'true'
END CASE;
You can use a subquery with EXISTS.

Mysql query option param

There is my query string, are there any problems about the bindParam?
$str = "select A.option_id,count(*) as sum
from tb_feedback A,tb_question B,tb_group C
where (A.question_id=B.id)
and (:question is null or B.id=:question)
and (B.group_num=C.id)
and (:group is null or C.name=:group)
and (:fromdate is null or A.date >= CAST(:fromdate AS DATE))
and (:todate is null or A.date <= CAST(:todate AS DATE))
group by A.option_id";
$sql = $this->conn->prepare($str);
$sql->bindParam(':question', $obj['question']);
$sql->bindParam(':group', $obj['group']);
$sql->bindParam(':fromdate', $obj['fromdate']);
$sql->bindParam(':todate', $obj['todate']);
I have found the reason that parameter's type not NULL, it is string, so it is not succeed when execute the next statement
(:question is null or B.id=:question)

counting null columns Mysql using PHP

I am wanting to count the null columns or with a certain value
$q = mysql_query("
SELECT SUM(
(`foto` = 'avatar_small.png') +
(`email` IS NULL) +
(`cidade` IS NULL) +
(`estado` IS NULL) +
(`endereco` IS NULL) +
(`numero` IS NULL) +
(`cep` IS NULL) +
(`telefone` IS NULL)
) FROM `info_complementos` WHERE id_user = ".$_SESSION['usuario_cargo']) or die(mysql_error());
$retorno = mysql_num_rows($q);
return $retorno[0];
the problem is that it shows the result
You can use the COUNT function in mysql to count the rows returned. So you want to filter out the rows that you do not want in the WHERE clause. This will count all the rows that match ALL the conditions. You can change the AND to OR if you want to match rows that contain some NULL values.
<?php
$q = mysql_query("
SELECT COUNT(*) FROM `info_complementos`
WHERE
id_user = $_SESSION['usuario_cargo'] AND
`foto` = 'avatar_small.png' AND
`email` IS NULL AND
`cidade` IS NULL) AND
`estado` IS NULL) AND
`endereco` IS NULL AND
`numero` IS NULL AND
`cep` IS NULL AND
`telefone` IS NULL");
$result = mysql_fetch_array($q);
echo $result[0];
Hope this helps.

Categories