SELECT $x(php variable) FROM - php

I`m trying to echo a single string from mysql database. There are columns in database (en, es, de, it...) representing different languages and I want to select value from one row and from column with current language ($language="en", or $language="es"...). I have tried:
<?php
$result = mysqli_query($con, 'SELECT "' .$language. '" FROM page WHERE
title="findInstructor" LIMIT 1');
$row = mysqli_fetch_row($result);
print_r($row[0]);
?>
The problem is, I am getting on screen value of $language variable, not a value from database. If I try it f.e. for english language, everything if fine:
$result = mysqli_query($con, 'SELECT en FROM page WHERE
title="findInstructor" LIMIT 1');
Or if I could do something like:
$result = mysqli_query($con, 'SELECT * FROM page WHERE
title="findInstructor" LIMIT 1');
....
print_r($row[$language]);
Thanks for an answer!

Because wrap $language with double quotes and SELECT treat it as a string and not column name. Try this:
<?php
$result = mysqli_query($con, 'SELECT ' .$language. ' FROM page WHERE
title="findInstructor" LIMIT 1');
$row = mysqli_fetch_row($result);
print_r($row[0]);
?>

As shown in below code if you put variable name in double quote,it's consider it as a static value.
'SELECT "' .$language. '" FROM page WHERE
title="findInstructor" LIMIT 1'
So instead of this you have to try below code
'SELECT ' .$language. ' FROM page WHERE
title="findInstructor" LIMIT 1'
Hope this will help you.

Related

i want to pass limit value in my select query through user

i want to pass limit value in my select query through user,can anybody help me to do this???? thanks in advance![][1]
$result = mysql_query("select distinct * from tweet_info ".
"where MATCH(tweet) ".
"AGAINST('".$search."')ORDER BY created ", $con);
lets just say that the user input is "LimitInput" then :
$limit = $_POST['LimitInput'];
$result = mysql_query("select distinct * from tweet_info ".
"where MATCH(tweet) ".
"AGAINST('".$search."')ORDER BY created limit 0,".$limit, $con);
$result = mysql_query("SELECT DISTINCT *
FROM tweet_info
WHERE MATCH(tweet)
AGAINST('" . $search . "')
ORDER BY created LIMIT 0,1 DESC", $con);
Just add a limit to the end, this will grab the first result found.
You shouldn't really be using the mysql_query function anyway, it's depreciated, maybe look into a different method, e.g. mysqli or PDO.

PHP don't show double outputs

I'm getting data out of my database with a query that selects Klantvraag numbers.
The output at the moment shows 5x the same output Klantvraag number.
The problem there is that those 5 numbers each have same address but different house numbers.
So basically if there are 5 Klantvraag numbers I want it to only select the number once and output it once in a selectbox.
Can you help me with that ?
<?php
include("config/instellingen.php");
$query = "SELECT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > ''";
if ($result = mysqli_query($connect, $query)) {
while ($get = mysqli_fetch_assoc($result)){
echo '<option value="' . $get['Klantvraag'] . '">' . $get['Klantvraag'] . '</option>';
}
}
?>
Change your query to:
$query = "SELECT DISTINCT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > ''";
This will only add any occuring value once to your result.
use DISTINCT keyword and the query will be
SELECT DISTINCT Klantvraag FROM DWA WHERE Status DWA = 'DBAA' AND Kenmerk > ''
Hope it will help you
use group by and query something like this
$query = "SELECT Klantvraag FROM `DWA` WHERE `Status DWA` = 'DBAA' AND `Kenmerk` > '' group by `Klantvraag`";

Mysql query variable syntax

I am trying to pass a variable to a very basic mysql query. but php doesnt return a true value. nothing.
i have checked everything
the problem is here.
the syntax of $a varible typing into mysql query
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1");
when i change $a to 22 it returns a value otherwise nothing.
exact query is here...
$a=$this->post_id;
$result = mysql_query('SELECT floatingnumber FROM posts WHERE id="'.$a.'" LIMIT 1')or die(mysql_error());
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
$sdfa returns "86 - " without quotes 86 - space
so the problem is on the mysql fetch row please help
Have you tried echoing the query to see what the real value of $a is?
echo "SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1";
Have you tried checking for errors?
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='$a' LIMIT 1") or die(mysql_error());
Also, you shouldn't even be using mysql_* as it's deprecated.
This is how you'd do it in PDO:
$stmnt = $db->prepare("SELECT id,floatingnumber FROM posts WHERE id=:id LIMIT 1");
$stmnt->bindValue( ':id' , $a , PDO::PARAM_INT );
$stmnt->execute();
$result = $stmnt->fetchAll(PDO::FETCH_ASSOC);
typically when I'm writing in double quotes, simply putting in the variable works:
"... $1 ..."
but also, I originally learned it with brackets
"... {$1} ..."
you can try that. also, a handy way to write queries is store the query string in its own variable so you can easily print out the query and see what you wrote before submitting.
$query = "SELECT id,floatingnumber FROM posts WHERE id=$a LIMIT 1";
$result = mysql_query( $query );
This helps identify things like this.
try this
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id='".$a."' LIMIT 1");
if your $a is a number then do like that
$result = mysql_query("SELECT id,floatingnumber FROM posts WHERE id= $a LIMIT 1");
EDIT :
your code is right
$row = mysql_fetch_row($result);
$sdfa=$a.'-'.$row[0];
the problem is in your sql or table because there is no floatingnumber where id is 86 .

PHP not displaying results from MySQL database

I am trying to display an entry from a MySql database which is selected by GET data.
if (isset($_GET["id"])){
$id=$_GET["id"];
$result = getSelectedBlog($id);
while($row = mysqli_fetch_array($result))
{
extract($row);
?>
<div class="headline"><?php echo $headline ?></div>
<div class="subtitle"><?php echo $subTitle ?></div>
<div class="content"><?php echo $content ?></div>
<?php
}
Here is the SQL statement:
function getSelectedBlog($id){
$con = mysqli_connect('localhost', 'root', '', 'michaelWebsite') or die('could not connect');
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "$id"';
$result = mysqli_query($con, $sql) or die('entry does not exist.:' . mysqli_error($con));
return $result;
}
As you can see, I am passing the get data as $id to the method that returns the result. However nothing is being returned. There are three entries at the moment, if I change $id in the SQL statement to either 1, 2 or 3 it will show the corresponding data but it just will not work with the $id variable.
The URL does end with the correct info ?id=1.
Please excuse me if it is something stupid, I have just been stuck on this for hours now!!
All of these answers will solve your problem, but none have mentioned or prevented SQL Injection.
In your case I recommend (assuming articleID is an integer field).
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "' . (int)$id . '"';
I'm also curious why you are using LIKE for an id field.
Note: Since you are using MySQLi, I'd encourage you to look at prepared statements.
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "'.$id.'"';
escape your var in simple quote
Try with this:
$sql = "SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'";
or with
$sql = 'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "' . $id . '"';
You need to use double quotes in order for php to correctly expand your variables :) so change your query to
$sql = "SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'";
Change
'SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE "$id"'
to
"SELECT * FROM tblArticle WHERE tblArticle.articleID LIKE '$id'"
Variables will be evaluated only if they're between double quotes "

mysql select query doesn't work when trying to select a single row

I am trying to create a php script that selects one row from my table by using the WHERE clause. The problem is the mysql query returns no rows. I know the variable is correct (its user submitted).
$title = mysql_real_escape_string($_REQUEST["title"]);
$query = mysql_query("SELECT * FROM links WHERE title ='$title'", $con)
or die ("Error: " . mysql_error());
I'm looking for any ideas that could fix my problem. I know the mysql is working properly because other queries execute fine. The title variable is correct; it is passed from a mysql on another page.
ps - I posted a similar question earlier, but worded it poorly and got results that didn't address the problem
Try this:
$query = mysql_query("SELECT * FROM links WHERE title ='$title' limit 1")
or die ("Error: " . mysql_error());
Sorry, I'm new here and I couldn't find the button to comment on the original question.
But you mentioned the request was user submitted. Are they typing it or is it a selection like from a select box or radio button? I'm asking because does the requested title even exist in the DB?
Anyway, what is your result if you use the following?:
$query = mysql_query("SELECT * FROM links WHERE title LIKE '%".$title."%'")
or die ("Error: " . mysql_error());
If not, then there's definitely no match in the DB.
try this query
mysql_query("SELECT * FROM links WHERE title like '$title%'", $con)
Try this query:
$query = mysql_query("SELECT * FROM links WHERE title LIKE '%{$title}%'");
or maybe this to check formatting:
$sql = sprintf("SELECT * FROM links WHERE title LIKE '%%%s%%'", $title);
$query = mysql_query($sql);
In my case I had empty space before and after the variable name, like this
$query = "select * from user where user_name = ' $user_name ' ";
and this results in comparing userName with [empty_space userName empty_space ] which doesn't exist in the database.
your query should be like this
$query = "select * from user where user_name = '$user_name' ";

Categories