Click here to Skip to main content
14,927,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
<?php
mysql_connect("localhost","root","password");
mysql_select_db("db_name");
$towerValue_4bhk = $_GET['towerValue_4bhk']; // from AJAX call
$floor_names_query = MYSQL_QUERY("select $total_4bhk from project_master");
mysql_close();
?>


I want $towerValue_4bhk to be used as a column name in SELECT Query.

When user selects a 4BHK in the front end then I should display the no of 4BHK's available for 15 floors.
Posted
Updated 1-May-21 4:31am
v2

XML
<html>
<head>
   <meta charset="utf-8" />
    <title></title>
</head>
<body>
<?php
print '12544';
?>
</body>
</html>
   
Change your code to below.

PHP
<?php
mysql_connect("localhost","root","password");
mysql_select_db("rohan_iksha");
$towerValue_4bhk = $_GET['towerValue_4bhk']; // from AJAX call
$floor_names_query = MYSQL_QUERY("select ".$total_4bhk." from project_master");
mysql_close();
?>



Run below code to check if your query is returning result.

PHP
<?php

$link = mysql_connect("localhost", "root", "password");
mysql_select_db("rohan_iksha", $link);

$result = mysql_query("select total_4bhk from project_master", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>


See what it is returning.
   
v4
Comments
Chubby Ninja 23-Dec-14 15:04pm
   
where is $total_4bhk defined above? FYI: you can include a variable inside "double quotes" just like the OP has
Janardhanam Julapalli 23-Dec-14 22:03pm
   
sorry! MYSQL_QUERY("select $towerValue_4bhk from project_master") is my requirement
Praveen Kumar Upadhyay 23-Dec-14 22:10pm
   
Yes. So just change variable name there.
Janardhanam Julapalli 23-Dec-14 22:13pm
   
I tried it.But no success.
Praveen Kumar Upadhyay 23-Dec-14 22:20pm
   
Try doing echo of that variable before the select statement. I guess that variable has some invalid data.
Janardhanam Julapalli 23-Dec-14 22:17pm
   
It is giving Resource id #4.I am not sure what it will be.
Janardhanam Julapalli 23-Dec-14 22:18pm
   
Can anyone please explain what exactly the query will do!
Janardhanam Julapalli 23-Dec-14 22:24pm
   
$towerValue_4bhk = "total_4bhk"; Even I tried with hard coded value.
Arun-23 23-Dec-14 23:12pm
   
select ".$total_4bhk." from project_master; this is the query did u get value in $total_4bhk variable??
Janardhanam Julapalli 23-Dec-14 23:16pm
   
$towerValue_4bhk = "total_4bhk";
$floor_names_query = MYSQL_QUERY("select ".$towerValue_4bhk." from project_master");
This is what I am doing.Not getting anything.
Praveen Kumar Upadhyay 24-Dec-14 1:18am
   
So your select query is becoming like "select total_4bhk from project_master"
Can you please execute the query in Mysql and see what output your are getting.
Janardhanam Julapalli 24-Dec-14 2:03am
   
Showing rows 0 - 24 (53 total, Query took 0.0010 seconds.)
Praveen Kumar Upadhyay 24-Dec-14 2:05am
   
How many rows it is returning?
Janardhanam Julapalli 24-Dec-14 2:11am
   
53 rows
Praveen Kumar Upadhyay 24-Dec-14 2:13am
   
Check how many rows your query is returning using below code.

echo mysql_num_rows($conn);

$conn is your connection object.
Praveen Kumar Upadhyay 24-Dec-14 2:18am
   
Check my updated question
Janardhanam Julapalli 24-Dec-14 2:22am
   
It is not returning anything for this.
$result = mysql_num_rows($conn);
echo $result;
Praveen Kumar Upadhyay 24-Dec-14 2:26am
   
What is your database name in which project_master table is there.

mysql_select_db("db_name");

Is "db_name" your database name??
Janardhanam Julapalli 24-Dec-14 2:34am
   
$conn = mysql_connect("localhost", "root", "myfourwalls");
mysql_select_db("rohan_iksha", $conn);
$num_rows = mysql_num_rows($conn);
echo $num_rows;
Praveen Kumar Upadhyay 24-Dec-14 2:36am
   
If your db name is "rohan_iksha" then why you are using "db_name". Just change the db name and your code will work.
Janardhanam Julapalli 24-Dec-14 2:42am
   
No sir.I am using the same db name before itself.
Praveen Kumar Upadhyay 24-Dec-14 2:51am
   
Try my second code again. I have updated the db_name there.
Praveen Kumar Upadhyay 24-Dec-14 2:51am
   
People who is devoting me for this solution. Please hold and see.
Janardhanam Julapalli 24-Dec-14 2:55am
   
It is returning "53 Rows" as result
Praveen Kumar Upadhyay 24-Dec-14 2:57am
   
Means it is now working fine, right?
Janardhanam Julapalli 24-Dec-14 2:59am
   
s
Janardhanam Julapalli 24-Dec-14 3:00am
   
$link = mysql_connect("localhost", "root", "");
mysql_select_db("rohan_iksha", $link);

$test = "total_4bhk";
$result = mysql_query("select $test from project_master", $link);
$num_rows = mysql_num_rows($result);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$row_array['total_4bhk'] = $row['total_4bhk'];

echo $row['total_4bhk'];
}
It is displaying the required values for me.
Praveen Kumar Upadhyay 24-Dec-14 3:02am
   
Now please accept the solution buddy. I have spend a lot time on this, just for a silly mistake by you which I didn't notice.
Janardhanam Julapalli 24-Dec-14 3:02am
   
Thanks for your time and patience.I am grateful to u!
Janardhanam Julapalli 24-Dec-14 3:03am
   
Alright.What mistake exactly I have done here?
Praveen Kumar Upadhyay 24-Dec-14 3:05am
   
Instead of
mysql_select_db("rohan_iksha");
you were trying
mysql_select_db("db_name");

So it was searching for the database name "db_name". Rest everything is ok.
Janardhanam Julapalli 24-Dec-14 3:09am
   
Done Sir!
Praveen Kumar Upadhyay 24-Dec-14 3:09am
   
Thanks.
<?php
mysql_connect("localhost","root","password");
mysql_select_db("db_name");
$towerValue_4bhk = $_GET['towerValue_4bhk']; // from AJAX call
$floor_names_query = MYSQL_QUERY("select ($total_4bhk) from project_master");
mysql_close();
?>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900