here is my prob. :
I got a users table with a column "music_style" who get value from a select input
example : rock, metal, pop,...
I want to select all other users who have the same "music_style" as me.
Example : If I put "metal" on my profile, i want to see all other profile with metal.
I hope to have been clear, sorry for my bad english :s
Assuming that you are using an sql based dbms, you may use the following sql statement:
SELECT * FROM `USERSTABLE` WHERE `music_style` = '$myStyle';
$myStyle is the php variable where you are storing the input.
Thanks for your answer, it work perfectly. But it select all user with the same music_style, including me. I tried to correct this but it didn't work .
I tried this :
$id = $_GET['id']
("SELECT * FROM users WHERE `music_style` = '$myStyle' AND 'id' != '$id' ");
Related
I am using PHP to create a website whereby a user inputs values into a new row on one page (values for Awinner, Arunner and so on) and on the next page those values are retrieved from the database. For this to work I need to find the most recent id which will always be the highest. What code can I put in between the hashtags below in order to do this?
$sql = "SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage";
$sql .= " WHERE id = #the maximum id#";
Then after this, I need to put each value (Awinner, Arunner, Bwinner, etc.) into its own variable in PHP so that I can display them all separately on the page. How can I do this? Sorry for lack of detail here but I am a newbie to PHP and SQL and am working on an important school project.
try like below by using subquery
"SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage where id= ( select max(id) from groupStage)"
Don't use a where use an order by with a limit to only return the 1 row.
SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner
FROM groupStage
order by `the maximum id` desc
limit 1
I have a table filter feature in PHP club membership webpage. I made it so the user can filter the table and choose which members to display in a table. For example, he can choose the country or state where the member is from then hit display. I am using a prepared statement.
The problem is, I need to use wildcards to make the coding easier. How do I use a wildcard in PHP MySQL query? I will use wildcards for example if the user does NOT want specific country but instead he wants to display all members from all countries.
I know not specifying the WHERE country= will automatically select any countries but I already constructed it so each controls like the SELECT control for country already has a value like "CA" or "NY" and "*" if the user leaves that control under "All Countries". This value when submitted is then added to the query like:
$SelectedCountry = $_POST["country"];
sql .= " WHERE country=" . $SelectedCountry;
But the problem is using WHERE country=* doesn't seem to work. No errors, just doesn't work. Is "*" the wildcard in PHP MySQL?
The * is not a wildcard in SQL when comparing with the = operator. You can use the like operator and pass a % to allow for anything.
When doing this the % should be the only thing going to the bind. $Bind_country = "'%'"; is incorrect because the driver is already going to quote the value and escape the quotes. So your query would come out as:
WHERE country ='\'%\''
The = also needs to be a like. So you want
$bind_country = '%';
and then the query should be:
$sql = 'select * from table where country like ?';
If this were my application I would build the where part dynamically.
Using * in WHERE clause is not right. You can only give legit value. For example:
// looking for an exact value
SELECT * FROM table WHERE column = 'value'
// you can also do this when looking for an exact value
// it works even if your $_POST[] has no value
SELECT * FROM table WHERE column = 'value' OR '$_POST["country"]' = ''
// looking for a specific or not exact value
// you can place % anywhere in value's place
// % denotes the unknown characters of the value
// it works also even if your $_POST[] has no value
// results will not be the same when you're using AND or OR clause
SELECT * FROM table WHERE column LIKE '%val%'
I think below link can solve your problem.
Just have a look and choose what you need.
Thanks.
http://www.w3schools.com/sql/sql_wildcards.asp
been googling for hours and I'm quite new to this.
I have two identical tables in one MySQL database:
One named "users" and one named "keys".
They are identical for testing purposes.
When I query "users" I get a response, when I query "keys" I get nothing.
Querying users I get the expected response:
<?php
require('../db/connect.php');
$query = mysql_query("
SELECT name
FROM users
WHERE can_share = '".$_POST['URLkey']."'
");
echo mysql_result($query, 0);
?>
Querying keys I get nothing:
<?php
require('../db/connect.php');
$query = mysql_query("
SELECT name
FROM keys
WHERE can_share = '".$_POST['URLkey']."'
");
echo mysql_result($query, 0);
?>
I guess there must be some basic understanding of databases that has slipped by me, but still, after hours of searching I can't figure it out. Maybe I'm becoming retarded.
I think that might be due to table name being 'keys'.
Have a look here:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
You have to understand while designing your tables and naming the attributes that some words are reserved by MySQL itself.
So, if you name your table 'WHERE' you will have troubles in usual query. Why?
'SELECT * FROM WHERE'
Such a query obviously doesn't work, as it will ask you to provide table name.
Now, when you change format situation also changes:
'SELECT * FROM `WHERE`'
As you can see I added some backwards commas. In MySQL they are used to denote names of tables or fields. If you use them - the server processes and reads your query correctly.
So, that's why your edited query worked fine in the end.
Thanks to enabeling debugging, I got this message:
mysql_result() expects parameter 1 to be resource, boolean given in
...
And figured out that I had to query "keys" like this:
<?php
require('../db/connect.php');
$query = mysql_query("
SELECT `name`
FROM `keys`
WHERE `can_share` = '".$_POST['URLkey']."'
");
echo mysql_result($query, 0);
?>
Now it works, but I still don't understand why only one of the tables needed that formatting. And I have learned that I should rewrite the whole thing to not be vulnerable to SQL injection attacks. ...
EDIT: It seems like the words "key" and "keys" and some more are reserved by MySQL, so to use them, they have to be formatted like that.
Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.
I'm creating my own blogging site. I cant seem to figure out whats wrong with this for 2 days. What I'm trying to do is get the author id from a table and then get author information from another table.
// Get post author
$post_id = $_GET["view"];
$post_row = mysql_fetch_assoc(mysql_query("SELECT post_author FROM posts WHERE post_id='$post_id'"));
$post_author = $post_row["post_author"];
// Get post author information
$author_row = mysql_fetch_assoc(mysql_query("SELECT id FROM users WHERE id='$post_author'"));
$author_username = $author_row["user_name"];
echo $author_username;
However, I'm getting this error message, Notice: Undefined index: user_name in... I'm sure I've spelled the field name right. There's nothing wrong with the first part of the code. How is the second part a problem? There exactly the same.
You do
SELECT id
but then you look for the row user_name. It's not there, so you get the warning that the index isn't there.
You are selecting column id in your query, but then you are using $author_row["user_name"].
change it to $author_row["id"] or use SELECT user_name in your query
You are selecting only field id from the database. user_name is not there because you haven't selected it.
$author_row = mysql_fetch_assoc(mysql_query("SELECT id, user_name FROM users WHERE id='$post_author'"));
Debugging tip: Always do print_r( $author_row ) to see what the query actually returns in cases like this. Not applicable in this case but equally important is to do echo mysql_error() to check that there wasn't an error in the query itself.
$author_row = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id='$post_author'"));
use this query to get all information of the user.