unknown column in where clause - mysql

$result = mysql_query("SELECT * FROM Volunteers WHERE Volunteers.eventID = " . $var);
$sql = mysql_query("SELECT * FROM Members WHERE Members.pid = " . $temp);
I am also doing or die(mysql_error()) at the end of both statements if that matter. My problem is that the first statement executes perfectly but in that table I store an attribute called pid. So the second statement is supposed to take that and return the row where it equals that pid so I can get the name. I get an error that says unknown column in 'a2' in 'where clause' where a2 the pid attribute returned from the first statement. Thanks for any help!
EDIT: Figured out what was wrong.
Had to write the code like this:
$sql = mysql_query("SELECT * FROM Members WHERE Members.pid = '$temp'") or die(mysql_error());

I think I see what you are trying to do, you can do this in one query, by JOIN-ing the tables together. the SQL query should be something like
SELECT Members.* FROM Members INNER JOIN Volunteers ON Volunteers.eventID=Members.pid WHERE Volunteers.eventID=" . $var
Check out This for a basic introduction to SQL joins.

Related

Select * from table where id=(a lot a different values)...how to write a query that can do this function?

I have two tables register and bar which are linked with the field id_register.I have the following query
$query="select * from register where register.id_register=(select bar.id_register from bar where bar.confirm='1' AND bar.confirm2='0') ";<br><br>
$run=mysql_query($query);<br>
while ($row=mysql_fetch_array($run))<br>
{
echo " $row[id_register]." ; <br>
echo " $row[another-field]." ;
}
I need help to solve this problem :This query release an error because when search for register.id_register (where register.id_register=(select bar.id_register from bar where bar.confirm='1' AND bar.confirm2='0') founds a lot of id values on DB.How can i solve this? Is there another mysql function that i can use to select data in the right way??
You can use IN clause here.
The IN operator allows you to determine if a specified value matches any one of a list or a subquery. The following illustrates the syntax of the IN operator.
Reference: http://www.mysqltutorial.org/sql-in.aspx
Syntax:
SELECT
column1,column2,...
FROM
table_name
WHERE
(expr|column_1) IN ('value1','value2',...);
Try this:
$query = "SELECT * FROM register WHERE register.id_register IN (SELECT bar.id_register FROM bar WHERE bar.confirm='1' AND bar.confirm2='0') ";
As you need to fetch data from 2 tables "register" and "bar" by joinng two columns you can better use JOIN query .
For reference
http://www.w3schools.com/sql/sql_join.asp
Hope this helps :)
You can use join and a helper table.
SELECT aa.*
FROM `register` AS aa
INNER JOIN (
SELECT bar.id_register
FROM bar
WHERE bar.confirm = '1' AND bar.confirm2 = '0'
) AS bb
ON aa.id_register = bb.id_register;

How to get name from another table when ids match?

Well, I suck in here (although I'm trying to learn). I can't understand how to improve my code when I see when others answer similar question to my (I know it might duplicate) and at the moment I'm stuck at my last step finishing php file. I have a tables called "cities" where there are columns called "id" and "name" and another table called "locations" where there are columns "id" and "location". I also have code:
$query = "SELECT * FROM locations";
$res = mysql_query($query);
while ($arr = mysql_fetch_array($res, MYSQL_ASSOC))
{
echo '<b>'.$arr['id'].'</b><b>'.$arr['location'].'</b>';
}
While my code works and I get id of wanted ID, I need to get name of city and not id. IDs in both tables match. I cannot edit tables or columns. What should I add? Can it be solved without any left, join, in, etc. queries? I can't understand them despite how I'm trying...
Try this out
$r = mysql_query("SELECT l.id, c.name
FROM locations AS l
INNER JOIN cities AS c ON c.id = l.id
");
while($l = mysql_fetch_array($r, MYSQL_ASSOC)){
echo '<b>' . $l['id'] .'</b><b>' . $l['name'] . '</b>';
}

Can you select a table value in a MySql Query and edit it?

For my MySQL query, I need to select a specific value like:
$query = "Select * from playerdata where name = $name"
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
However, I want to take that grabbed specific table value (the * operator) and then edit that one like so:
$newquery = "insert into [I want to insert some values from the
old query's selection] [not the general table]"
I know how to use queries and how to execute them. It would be great if I could condense both the select and the insert into one query.
You can use the SQL INSERT INTO SELECT Statement
INSERT INTO table2
Select * from playerdata where name = 'Pedro';
To use this in your PHP script it would look something like this:
$nameSafe = mysql_real_escape_string($name);
$newquery = "INSERT INTO table2
Select * from playerdata where name = '$nameSafe';";
It's good to understand the aspects of security from early on and for that PHP Security Cheat Sheet is a good read.

MYSQL inner join of two tables with non-unique columns

I have two tables (jos_table1 and jos_table2), which I want to do an inner join on element1 (from table 1) and element2 (from table2). Both tables contain a column called "parent_id" - I can't change this.
I want to count the number of rows which fulfil a certain where clause based on the parent_id element. The statement works fine if I omit the where clause, but when I have it there it doesn't work and returns the error "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given"
Does anyone know why? Any help is most appreciated.
Kind regards,
Paul Hughes
$table="jos_table1";
$table2= "jos_table2";
MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");
$aiders=MYSQL_QUERY('SELECT COUNT($table.element1) AS hpp FROM $table INNER JOIN $table2 ON $table.element1=$table2.element2
WHERE `$table.parent_id` = 134
');
$faid = mysql_fetch_array($aiders);
$total = $faid[0];
echo "The total is (".$total.")";
The backquotes need to go around just the table part and/or just the column part.
For example:
WHERE `$table`.`parent_id`=134

updating mysql table using php gives no change

I'm trying to update a mySQL table using php. Basically I pass an ordernumber (int) and use it to find both isbn and quantityordered which I then use to update another table.
$orderID=$_POST["order_ent_number"];
$query="select ISBN from Orders where orderNumber='$orderID'";
$isbn = mysql_query($query) or die(mysql_error());
$query="select quantityOrdered from Orders where orderNumber='$orderID'";
$quantityordered = mysql_query($query) or die(mysql_error());
$query="UPDATE Books SET inStock='$quantityordered' WHERE inStock='0' AND isbn='$isbn'";
$result = mysql_query($query) or die(mysql_error());
So using the MySQL bench, the query works (if I replace all variables with numbers) and changes it. The problem is when I use the variables in PHP, the code does not work. I tried the mysql_escape_string but that didnt work either. I checked the results of both variables $isbn and $quantityordered and they are right. I get no errors when I run it on the server but there is no change to inStock in the database. After searching around, someone said my variables need to be turned into integers? Not sure if this is correct or not but that is all I came up with. Any suggestions?
Actually you can do it in a single UPDATE statement by joining the tables,
UPDATE Books a
INNER JOIN Orders b
ON a.ISBN = b.ISBN
SET a.instock = a.quantityOrdered
WHERE a.instock = 0 AND
b.OrderNUmber = '$orderID' AND
a.ISBN = '$isbn'
As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.
How to prevent SQL injection in PHP?
maybe you can try
$query="select ISBN from Orders where orderNumber='".$orderID."'";
$query="select quantityOrdered from Orders where orderNumber='".$orderID."'";
$query="UPDATE Books SET inStock='".$quantityordered."' WHERE inStock='0' AND isbn='".$isbn."'";