PHP - Mysql: Create variable for undefined index - php

I know how to select from a database, my question is i want to set a variable for a where clause in my query, so lets say select * FROM Foo WHERE A = '$a', but the variable has not been set previously and is from the table i am trying to select from.
So how would i do this.
PHP
$FC_ID = mysqli_real_escape_string($dbc,[]);
$FOOD_sel = $dbc->query("SELECT * FROM Food_Cat WHERE Food_Cat_ID = '$FC_ID'");
That is how far i have got so far.

Try to use sub query for that $FC_ID value

sub query method
$FC_ID = mysqli_real_escape_string($dbc,[]);
$FOOD_sel = $dbc->query("SELECT * FROM Food_Cat WHERE Food_Cat_ID = (select FC_ID from your table )");

Related

Using a variable name after the where clause in mysql and php

I'm trying to write a PHP string to create an SQL statement which uses a variable for a field name after the WHERE clause. The problem is that it doesn't use the variable name, I'm not good with PHP so not sure if it's my string concatenation or something else... any help would be appreciated! I'm using PHP 5.5 with mysql, as mysqli isn't available with my current host.
echo $sql_subject;
$sqlString2 = "SELECT * FROM tableName WHERE ". $sql_subject . " = '$set'";
However it keeps outputting:
S
SELECT * FROM tableName WHERE '' = '1'
I know the $sql_subject has a value because it's printing above the SQL output... I'd like it to say:
SELECT * FROM tableName WHERE S = '1'
Thanks

Sending SELECT # variable from SQL to PHP

I have this statement that's working well in SQL:
SET #cnt = (SELECT `reg_date` FROM logindb WHERE `userlogin`='urs');
SET #var = (SELECT TIME (#cnt));
SELECT #var
But I don't know how to get that data if I don't have a specific column output.
In php I'm trying with the following code:
$sql = "SET #cnt = (SELECT `reg_date` FROM logindb WHERE `userlogin`='urs');
SET #var = (SELECT TIME (#cnt));
SELECT #var"
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo($row["#var"]);
Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\DAN\autentificare.php on line 62
The line 62 :
$row = $result->fetch_assoc();
I don't know how to store or view that variable in PHP.
Explanation of the code:
When I created the table named logindb, I use a variable reg_date declared as TIMESTAMP that put the server time every time another column from the same line is filled. To resume the entire code is used to get only the TIME from reg_date : Ex. 2015-01-06 00:39:56 output that I get in #var is 00:39:56
Try this:
SELECT #var as colName
....
echo($row["colName"]);
UPDATE
When I read the post first time I think that the question is something simplified. Anyway, to solve your issue you can use this code.
$sql = "SELECT TIME(`reg_date`) FROM logindb WHERE `userlogin`='urs'";
$result = $conn->query($sql);
echo $result->fetch_row[0];
The following code(assuming your $sql statement is valid)
$row = $result->fetch_assoc();
Returns an associative array. To access the value the key would look likethis:
$myresult = $row['column'];
*column is the column you selected in your statement, in your case 'reg_date'

WHERE clause doesn't seem to work in PHP

The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.

Query based on enum value

I am trying to query based on the value of an enum field. Essentially I have 3 values (categories) for the enum field, a, b and c respectively. I am attempting to populate a select field based on the results of the following query:
SELECT * WHERE pid = $id AND group = 'a';
I have tried this with and without single quotes and get the error You have an error in your SQL syntax; and have narrowed it down to being a lack of knowing how to query based on a specific enum value. I assumed this would work and see no reason for it not to so if I can be enlightened I would greatly appreciate it.
Group is a reserved keyword use it like this
SELECT * FROM mytable WHERE pid = $id AND `group` = 'a';
Use tablename
SELECT * From tablename WHERE pid = $id AND `group` = 'a';
Try This..
SELECT * FROM tbname WHERE pid = $id AND `group` = 'a';

PHP: Where clause will not execute when using a variable

For the user I am testing with, their org_id column value is "student_life"
I am trying to have this function display whatever rows have the student_life column = 1. (so yes there is a column student_life which is a boolean, and then I also have a separate column named org_id and in this case has the value student_life)
I am pretty sure there is a syntax error but I cannot figure it out.
function org_id_users_table()
{
$org_id = mysql_real_escape_string($_POST["org_id"]);
$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE '$org_id' = '1'");
$result = $sql['sql'];
$num_rows = $sql['num_rows'];
$this->create_table($result, $num_rows);
}
(when I replace $org_id in the "$sql=..." line with student_life the code works.
You're quoting the column name, which makes MySQL think it's a string.
$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE $org_id = '1'");
Edit:
Based on your comments, I think what you actually want is this:
$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE org_id = '$org_id'");
Change quotes.
$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE `$org_id` = '1'");
P.S. Why shouldn't I use mysql_* functions in PHP?
Where is this coming from? $_POST["org_id"]
Do you have a form on the page posting that? Or are you just trying to get that from the database? If so, wouldn't you need another query to obtain that first?
$row_MyFirstQuery['org_id']
Otherwise if it is $_POST["org_id"], wouldn't it be single quotes not double? $_POST['org_id']

Categories