SQL to JSON converts my int to string - mysql

I have this in my PHP.
$sql="select * from defaulttime";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
$row_set[] = $row;
}
echo trim(json_encode($row_set));
In my SQL some columns are Integer. but in JSON it shows as String.
can someone please tell me what I am doing wrong?
when I run the PHP, I get the below results. (Note Hour and Minute should be Integers)
[{"name":"Test","Hour": "6" ,"Minute":"45"}],
I want it to show string as string and Int as Int.
{"name":"Test","Hour": 6 ,"Minute":45}

You have to use JSON_NUMERIC_CHECK option in order to get numerics value,
look here: https://lornajane.net/posts/2011/php-returning-numeric-values-in-json
echo trim(json_encode($row_set,JSON_NUMERIC_CHECK));

Thanks all,
I got it working by using this code:
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
$row_set[] = $row;
}
$json=json_encode($row_set,JSON_NUMERIC_CHECK);
echo $json;

Related

Error while accessing MySQL resultset

When I executed this code,
while($row = mysql_fetch_array($res))
there was an error of the following plan:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in String of treatment:while($row =
mysql_fetch_array($res))
$res should be an resource , for example
$res = mysql_query("SELECT * FROM table;");
after that only use mysql_fetch_array. Just for information
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Would be better to use mysqli_query instead of mysql_query. Read this answer and this PHP documentation about differences between them. So I advice you to use mysqli:
$result = mysqli_query($connection, 'SELECT id, name FROM some_table');
if($result){
while($row = mysqli_fetch_assoc($result)){
//extract values from row
$id = $row['id'];
$name = $row['name'];
}
}

Mysql query is working in SQLyog while not read by browser

what is error in this code at mysql query???
i think it is very easy question but error could not found :( while this mysql query is working in SQLYog
$result =mysql_query(SELECT NUMPOINTS((i.geom))COUNT FROM district i WHERE district_name="D.G.KHAN";)
compete code is
//$type="R";
$result =mysql_query(SELECT NUMPOINTS((i.geom))COUNT FROM district i WHERE district_name="D.G.KHAN";)
$row2 = mysql_fetch_array($result);
$count=$row2['count'];
$x_points = array();
$y_points = array();
/* $row1 ['color']="00000"; */
for($c=1;$c<=$count;$c++){
$res=mysql_query("SELECT district_name,X ( POINTN ( i.geom ,$c))xx ,Y( POINTN ( i.geom ,$c))yy ,color FROM district i where dis_id='".$id."'" );
while($row1 = mysql_fetch_array($res)){
array_push($x_points['xx']);
array_push($y_points['yy']);
}
}
print_r ($x_points);
print_r ($y_points);
?>
You need to add as a string:
$result =mysql_query("SELECT NUMPOINTS((i.geom)) FROM district i WHERE district_name='D.G.KHAN'";)

no output in DISTINCT in mysql php

I cannot display in my distinct query in mysql please help.
<?php
mysql_connect("localhost","root","123");
mysql_select_db("sarangani");
$result = mysql_query("SELECT DISTINCT year(posted) AS year FROM news ORDER BY posted");
while($row = mysql_fetch_array($result)) {
echo "$row[year]";
}
?>
If you're sure the query should deliver a result, you need to use the proper way to echo out the result:
while ($row = mysql_fetch_array($result)) {
echo $row['year']; // So don't put it inside double quotes
}

search mysql column with similar_text() matching percentage

i want to echo one value from mysql column which is 90% similar to php variable.
echo only the value that is 90% similar to php value.
i think like clause wont help in this case.
<?php
$x = $_GET[x];
$con=mysqli_connect("example.com","peter","abc123","my_db");
$res = mysqli_query($con,"SELECT * FROM text");
while ($row = mysql_fetch_assoc($res))
{
$string[] = $row['text'];
}
foreach ($string as $y)
{
similar_text($x, $y, $percent);
}
if ($percent>"90")
{
echo $string ;
?>
i think the above code has many mistakes. i welcome some new solution for my task.

Having a MYSQL query within a PHP fwrite function

basically I am try to get the php file to create another php file which has XHTML, PHP and MYSQL within it. I am trying to include a MYSQL query but the problem is that the apostrophe (') that I use to start the fwrite string get confused when it reaches a MYSQL query because it too also has an apostrophe but I just want that string to continue not end. What should I do for this to work? Thanks in advance for any help.
The code (just the relevant bit):
$filename = "websites/".$firstpage.".php";
$filehandle = fopen($filename, 'w') or die("error opening file");
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID='$webID'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row['webID'];
';
fwrite($filehandle, $datawrite);
fclose($filehandle);
You should escape the ' in the query with \' This will not exec and end the string
$filename = "websites/".$firstpage.".php";
$filehandle = fopen($filename, 'w') or die("error opening file");
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID=\'$webID\'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row[\'webID\'];
';
fwrite($filehandle, $datawrite);
fclose($filehandle);
Check this out for reference
http://php.net/manual/en/language.types.string.php
Just use proper escaping and place a \ before the apostrophe.
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID=\'$webID\'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row[\'webID\'];
';