sending query from Android to php - php

I wrote a php file on Raspberry pi, using "$_Get" to receive query query strings including date('Y-m-d').
But I keep getting syntax error and I can't find out which part is wrong....
Here's my php code below:
{
$connect = mysqli_connect("220.133.21.50", "root", "120232", "weather");
$query = 'SELECT * FROM dosensor ORDER BY num ASC';
$result = mysqli_query($connect, $query);
$s="";
while($row = mysqli_fetch_assoc($result))
{$result = mysqli_query($connect, "Select * FROM dosensor ORDER BY num ASC WHERE `recordtime`
<='".$_GET["date('Y-m-d')"]."';");
if ($s=="") $s=$s."['".$row["recordtime"]."',".$row["DO"]."]";
else $s=$s.",['".$row["recordtime"]."',".$row["DO"]."]";
}
}
And here's the query string sending from my Android Studio:
webview.loadUrl("http://"+intent.getStringExtra("ip")+":/linechart.php?
uid="+intent.getStringExtra("uid")+"&pw="+intent.getStringExtra("password")+"&date('Y-m-d')
AND recordtime >= date('Y-m-d',strtotime('-10 day'))");
Could someone tell my which part is causing syntax error?
the error should be on this line
$result = mysqli_query($connect, "Select * FROM dosensor ORDER BY num ASC WHERE `recordtime`
<='".$_GET["date('Y-m-d')"]."';");

Related

LIMIT doesn't work in MariaDB - Tried a lot of things

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "igscript";
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
if(mysqli_connect_errno()) {
die("DB Error: " . mysqli_connect_error() . " ( " .mysqli_connect_errno() . ")");
}
while($row = mysqli_fetch_assoc($result)) {
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1;";
echo '<center><p>'.$row["name"].'</p> </center><hr>';
if(!mysqli_query($con, $query)) {
die('Error : '.mysqli_error($con));
}
}
$result = mysqli_query($con, $query);
Whenever i use either LIMIT 1, or LIMIT 10; at $query, it has no effect at all. Still displays the same amount of rows. I tried also TOP 10 or TOP(10) as I seen on internet, and i'm getting
Error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '10 name FROM lastsearches Order By data DESC LIMIT 1' at line 1
$query = "SELECT TOP 10 name FROM lastsearches Order By data DESC";
-> this was the query;
Also the first query worked properly in phpmyadmin, section SQL.
The query from which you are actually displaying results is this one:
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
If you want to limit the results, you need to edit that query instead i.e.
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1";
$result = mysqli_query($con, $query);
I'm not sure what you are trying to achieve with the query in your while loop, but it is not doing anything inside that loop so you can probably remove it.
TOP 10 is SQL Server syntax, not MySQL. MySQL uses LIMIT 10 with a very similar effect.
Since I don't see TOP 10 in your code, could it be some interface package that is tied to SQL Server?

PHPStorm Encodes Query only sometimes

So i got this query in my File if i run the code
$query = "SELECT * FROM dbo.V_Admin_Hülse ORDER BY Kaliber DESC";
$sql = $conn->query($query);
this is $conn it is in a diffrent file but required:
$conn = new PDO("sqlsrv:server=$serverName;Database = $database;", $uid, $pwd);
$conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
if i run the file in debug and look into $query it reads:
$query = SELECT * FROM dbo.V_Admin_Hülse ORDER BY Kaliber DESC
And there is no problem running it.
In a different file i got another Statment with an umlaut
$query = "SELECT * FROM dbo.T_Geschossform ORDER BY Abkürzung ASC";
$sql = $conn->query($query);
but when i run that in Debug i get
$query = SELECT * FROM dbo.T_Geschossform ORDER BY Abkürzung ASC
and quits with an exception!
Why is that?
And how do i get it to do it properly?
Ok i added
header('Content-Type: text/html; charset=utf-8');
and after i closed and reopend the Projekt it read
$query = "SELECT * FROM dbo.T_Geschossform ORDER BY Abk�rzung ASC"
i replaced the � with an ü and now it works fine....
Strange things are happening...
Can anyone explain?

How to run an sql query inside a while loop of another query

The following works with no problem at all, when the photoId is directly on the statement and not a variable.
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "103"') or die(mysqli_error($con));
but the following just won't work with no error, what might be causing this not to select.
$imageid = '103';
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
This is inside a while loop.
while($row = mysqli_fetch_array($somequery)){
$imageid = $row['photoid'];
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
}
Thanks.
in php a ' and a " are very different and the query syntax is double quote around the query and single quote around variables.. although I would recommend you look at using parameters on your query instead of just putting a variable directly into the query
Per my recommendation you should change your query to this:
$imageid = '103';
$query = $con->prepare("SELECT * FROM imgs WHERE photoid = ?");
$query->bind_param('sssd', $imageid);
$query->execute();
this is just the nuts and bolts of it... if you want more information about the connection.. error handling and everything else read the DOCS
there is a big difference between ' and " in php
Differences
change your query to be
$img_query = mysqli_query($con, "SELECT * FROM imgs WHERE photoid = '$imageid'") or die(mysqli_error($con));
and it should work.

MS SQL Query failed in PHP but not in MS SQL Server Management Studio

I execute this query with php and odbc driver
$sql="DECLARE #Auftrag int;
DECLARE #date_now datetime = getdate();
EXEC #Auftrag=EHS.dbo.SP_ANZEIGE
#Tablet=1,
#Status=0,
#KuNr='K015538';
SELECT 'generatedID'=#Auftrag;";
$res = odbc_exec($db1_link, $sql) or die(odbc_errormsg()); // returns resource(13)
$firstRow = odbc_fetch_array($res); // dies error
If i do odbc_fetch_array the error "No tuples available at this result index" is thrown.
If I run the exact same query in Management Studio everything works fine. It shows me the computed generatedID. What is the difference?
greets Alex
Try to prefix the query with:
set nocount on
That prevents SQL Server from sending rowcount updates, which UNIX clients sometimes mistake for actual rowsets
I had this same problem. In my case I was executing like this
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
while ($data = odbc_fetch_array($resultSet)) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);//failed here
while ($data2 = odbc_fetch_array($resultSet2)) {
//something here
}
}
and I changed like this and it worked
$sql = "SELECT * FROM table1";
$resultSet = odbc_exec($sqllink, $sql);
// Create an array and store the results
$queryResult = array();
while ($data = odbc_fetch_array($resultSet)) {
// push the required content into the array
$queryResult[$data['id']]['name'] = $data[name];
}
foreach($queryResult as $row) {
$sql = "SELECT * FROM table2";
$resultSet2 = odbc_exec($sqllink, $sql);
while ($data2 = odbc_fetch_array($resultSet2)) {
// something here
}
}

Select latest entry from MySQL database error

<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1';
$row = mysql_fetch_array( $query );
echo $row['frase'];
?>
I cant get this to work.
I get this error:
Warning: mysql_fetch_array(): supplied
argument is not a valid MySQL result
resource in
/home/jmvarela/public_html/ihateyourjacket.com/latest/index.php
on line 7
I am trying to select the latest entry to the mysql database.
The table is called "quote"
There are three fields: id, frase and name.
Just to clarify (because this could be VERY bad coding) I am trying to get the "biggest" id and to display it's correspondant "frase".
you have not perform your query
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
try this
Looks like you are not running the query.
// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1';
// run the query..THIS IS MISSING.
$result = mysql_query($query);
Also it's better to change SELECT * to SELECT frase, since you're interested only in the frase column. This will not bring all the unwanted columns from MySql to PHP, making your program perform better.
I´m not sure if this should be done but ill leave the complete running code for future refence.
<?php
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("jmvarela_jacket") or die(mysql_error());
// construct the query.
$query = 'SELECT * FROM `quote` ORDER BY `id` DESC LIMIT 1';
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
echo $row['frase'];
?>
Thanks to everyone!

Categories