SQLi Query not working - php

I have a mysqli query given below
$content = mysqli_real_escape_string($link,$_GET['content']);
$query = "SELECT id,siteName,address,latitude,longitude,info,budget,tts,markerType FROM maptable WHERE siteName = '". $content ."'";
$result = mysqli_query($link, $query);
I'm currently confused right now because I am using this script for my ajax call but when i tried to remove the WHERE clause, the query function works, when I include the WHERE clause looking for the Id it still works but when I tried to use the WHERE clause on any database fields starting form siteName,address,etc it fails and returns an empty value how is that happening? is there something wrong with my query?

Related

PHP MySQL Query using a variable, not diplaying results, nor error

The variable I use for the query prints properly, so it is there.
<?php
print $product_main["ean"];
//The variable is there, since it prints
?>
If I execute this query, it works
$query = ("SELECT * FROM graph ;") or die(mysql_error());
But not with the one where I use a variable
$query = ("SELECT * FROM graph WHERE ean IN ".$product_main["ean"]." group by DAY(created_at);") or die(mysql_error());
Does not work.
I belibe the problem is in how I include the array variable into the query
".$product_main["ean"]."
How has this to be done?
Ajith got it right. I was missing (' ')
$query = ("SELECT * FROM graph WHERE ean IN ('".$product_main["ean"]."') group by DAY(created_at);");
Thanks

PHP Does not let me query a table

I have a database where I can make queries to all tables without problems except for the 'employees' table. I tried making this basic query in php:
<?php
error_reporting(0);
require "init.php";
$sql = "SELECT * FROM empleados;";
$result = mysqli_query($con, $sql);
$response = array();
while($row = mysqli_fetch_array($result)){
$response[]=$row;
}
echo json_encode($response);
?>
... and I do not get any results. However when I run this query for other tables, it works well, what can it be?
This same query works fine from phpmyadmin
NEWS: if you use SELECT DNI FROM empleados it works, if I use SELECT * FROM empleados it doesn't.... (DNI is the key, is it possible that I can only access the primary key?)
Be careful with special characters such as Spanish accents, if you remove these the query works correctly
It seems you are not connecting to database.
Try this.
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con, $sql);
Do not forget to change the credentials.

Problems using a PHP string variable in a MySQL select query

I've found many similar questions regarding this but anything I try won't work.
I'm trying to run a MySQL query using the variable $epost. When I echo this variable it displays correctly, but the query returns nothing. Entering a fixed value for $epost like:
$epost='email#email.com'
Returns the correct query from the database.
$epost=mysqli_real_escape_string($conn,$_POST['email']);
echo $epost;
$sql = "SELECT memberID FROM Member WHERE email = '$epost' limit 1";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
echo $row["memberID"];

Select an id in php script from a DB (Android)

I'm developing an app for android that uses a DB on a server.
I wrote some script php to create new rows in some tables and get all elements from a table (using JSON to exchange data between android and mysql).
Now I have a problem:
i need to select an id from a table and then use this to insert a row in anothere table that has this foreign key.
Well, when I try to select my id, i don't know why, but look like it doesn't work.
Here a simple example how I select this id:
//connect to DB...
$result = mysql_query (*SELECT id FROM 'table' WHERE name = $name );
$row = mysql_fetch_assoc($result);
$id = $row['id'];
When i use this to select an id, and put it in another query (always on the same connectio) nothing is stored.
if I force the value manually, and so in the same second query I put a number of a preesisting id, the insert works, so the problem is in this piece of code.
Hope someone could help me.
Thank you!
The code that you have put on the question, contains syntax errors.
- Remove * from the start of query
- put the query inside " "
- remove single quote ('table') from table name
Here is the modified code:
//connect to DB...
$result = mysql_query ("SELECT id FROM table WHERE name = $name" );
$row = mysql_fetch_assoc($result);
$id = $row['id'];
Also you should escape the parameter $name in query. And you should use mysqli or PDO instead of mysql extension.
try this:
$result = mysql_query (*SELECT id FROM 'table' WHERE name = $name );
$row = mysql_fetch_assoc($result);
while($row > 0){
$id = $row['id'];
}

Get subject from Table in PHP

OK So I'm trying to access a table called emg_quote I have the Quote ID so Im trying to get the Column Subject from the same row as this ID but for some reason All I'm getting is the first row in the entire table? Can any one figure out what I'm doing wrong? Here is my coding:
$row['quote_id'] = quoteTitle($row['quote_id']);
function quoteTitle($quoteid){
global $db;
$sql = "SELECT subject FROM emg_quote WHERE ".$quoteid."";
$res = $db->query($sql);
$row = $db->fetch_row();
$output = $row['subject'];
return $output;
}
Are you using a custom object to wrap the native API's?
Either way it doesn't look right to me. You don't seem to be using the result of the query.
i.e.
$result = $mysqli->query($query);
$row = $result->fetch_row();
You have few bad practices in your code.
A. You lie on $quoteid to give you the correct where syntax. ie: ID=123
This is an highly unsafe method, because the user can change the it to Some-Important-Details='bla'
To extract more details from this table or others.
B. You should ALWAYS escape characters when receiving data from user, otherwise you easily subjected to SQL-Injections. And believe me you don't want it.
you have to use the checking after where.
use you column name before your $quoteid variable
$row['quote_id'] = quoteTitle($row['quote_id']);
function quoteTitle($quoteid){
global $db;
$sql = "SELECT subject FROM emg_quote WHERE quoteid=".$quoteid." LIMIT 1 ";
$res = $db->query($sql);
$row = $db->fetch_row();
$output = $row['subject'];
return $output;
}
Remember : USE limit 1 when you search with primary key and you know that only 1 record will be searched. it reduce your processing time.
You might be missing the where column.
$sql = "SELECT subject FROM emg_quote WHERE quote_id=".$quoteid."";
^^^^^^^^
We also do not see weather something with your Db class is wrong.
You should in any case not directly put request variables into a database query.
$sql = "SELECT subject FROM emg_quote WHERE ID='".$quoteid."'";
You had not wrote your db fieldname in where condition

Categories