PHPStorm Encodes Query only sometimes - php

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?

Related

sending query from Android to 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')"]."';");

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.

How to fetch data?

This statement gives error
unable to fetch result from database
$res =$dbcon->exec("select *from tablename ");
$res->fetch();
Give an error.
change only one query
$res =$dbcon->exec("select *from tablename ");
to
$res =$dbcon->query("select *from tablename ");
You need a space in your query:
$res = $dbcon->exec("select * from tablename");
Depending on what $dbcon is, you may need to use the query method instead (part of alok.kumar's answer):
$res = $dbcon->query("select * from tablename");
Referenece: http://www.php.net/manual/en/pdo.query.php

Empty query result in php

I'm having troubles with a PHP code. The problems come when I execute a SQL query with a PHP variable inside it. The result doesn't shows anything, and the field in the database is not empty. Tried out with a static id (not variable) and the query works fine.
Do you know where I'm doing it wrong?
The query code:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion);
Try this:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion) or die(mysql_error());
That will give you an error message.
The problem is that you use both ` and ' as escape characters as the same time.
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';", $conexion);
should work.
Often things will be more clear when you echo the query so you can see the final result.
you are using double quotes on your query so there is no need for the dot . operator
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado'", $conexion);
You are connecting string wrong! You are using ' single quote, but you should use double/none.
Try this query:
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '$videoSeleccionado';", $conexion)
Or
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '{$videoSeleccionado}';", $conexion)
Or
$q = "SELECT * FROM `videos` WHERE `idvideo` = '%s';";
mysql_query(sprintf($q, $videoSeleccionado), $conexion)
Edit:
If it's still not working problem can be in query, try checking if it is using mysql_error()(1) or try dumping query(2).
Example(1):
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '".$videoSeleccionado."';", $conexion) or die(mysql_error());
Example(2):
$q = "SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';";
var_dump($q);
mysql_query($q, $conexion)

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