I have a database that stores the users name, number, and carrier in a table called user.
I need to know how to write a query that if my value is equal to name - it will fetch the number and carrier associated with that name. I am writing this in php and will use javascript if necessary.
I should prefer you to use "SELECT * FROM user WHERE name LIKE '{$value}'"
because using = will search for the exact value
For example: if in database the value is john and u searched for John it will not display the result but if you use LIKE it will display all the related results like JOHN, john, John, jOHN etc.
Thanking You,
Megha
You can just put one of the following:
1)
$value = "John";
$result = mysql_query( "SELECT * FROM user WHERE name LIKE '$value'" );
above query will return rows matching name like JOHN, john, John,etc as suggested by Megha.
2)
$value = "John";
$result = mysql_query( "SELECT * FROM user WHERE name='$value'" );
above query will return rows matching name with the value 'John'.
Your SQL query will look like this:
"SELECT * FROM user WHERE name = '{$value}'"
This selects all columns in the table user where the name column has a value of the PHP variable $value
You can execute this query using PHP's MySQL functions: http://php.net/manual/en/book.mysql.php
Example
$value = "John";
$result = mysql_query( "SELECT * FROM user WHERE name = '{$value}'" );
while( $row = mysql_fetch_array( $result ) )
print( "Column with name columnName has a value of " . $row['columnName'] );
Related
I have this mysql table.
name | total
chris | 5
jan | 3
bob | 2
eric | 4
chris and jan were selected using this code
$query = " select * from table where name = '$given_name' ";
// &given_name = result from another query
I want to store the result of the query to a variable.
while($row=mysql_fetch_assoc($query)){
$result = $row['name'];
} // i want to store both chris and jan to $result
Then i will use the result of the query to another query. I want to select the remaining names. Not the ones on the first query. I want chris and jan not to be selected with this query because it is stored in $result
select * from table where name != $result ;
But one name was just stored in $result. I want them both to be stored in $result.
You could use FIND_IN_SET to see if the names had been fetched before. Firstly you need to make $result an array of all the names:
$result = array();
while ($row=mysql_fetch_assoc($query)) {
$result[] = $row['name'];
}
Then you can write your query to exclude the names in $result:
$sql = "SELECT * FROM table WHERE NOT FIND_IN_SET(name, '" . implode(',', $result) . "')";
You can use database query like below to exclude chris and jan:
select * from table where name NOT IN( $result );
Assuming that you do not know the names of the resultset you can simply (a) select the first two names from the resultset, (b) concatenate them in a string and finally (c) use "NOT IN" as your query parameter.
$numPicks = 2; // decide how many names you want in the list
// OR if you want to exclude ALL names found in the first query
$num_rows = mysql_num_rows($query);
$nameList = ''; // start with an empty string
for($i=0; $i<$numPicks; $i++) { // then use $num_rows here instead of numPicks
$nameList .= $row['name'].',';
}
$nameList = rtrim($nameList,','); // remove trailing comma from the string
$sql = "select * from table where name NOT IN ($nameList)";
I have a MySQL database on my domain, than I made a table called AccountInformation. In that table are things like AccountID, AccountName, FirstName, SecondName etc.
I made a PHP program which connects to that database and is able to fetch all rows and display them, but thats not what I want it to do. I want to fetch information from a certain row depending on its ID.
Pseudo-Example :
$UserName String
$SearchID = 1
$UserName = fetchrow where 'AccountID' = $SearchID and get 'AccountName' of that row
So that I now have the UserName of the account with the ID 1 in $UserName.
OK, this should be what you're looking for...
/* put the value of $number to the id number you want to get other info from*/
$number = "1";
$sql = mysql_query("select * from `AccountInformation` where `id`='$number';");
$result = mysql_fetch_assoc($sql);
echo $result["AccountName"];
A previous variable from a query gave me a value $name. I need to find the user id associated with that name, however in my users table I have two fields, firstName and lastName.
I cannot explode $name as I have both cases of double names (e.g. John Eric Smith) and last names (e.g. Jan van der Worde), so my attempt was to find a way to match firstName + lastName with $name.
My attempt was this:
$drid = "SELECT id FROM users WHERE CONCAT(firstName,' ',lastName)='$name'";
$rest = mysql_query($drid);
while ($row = mysql_fetch_row($rest)) {
$driver_id = $row[0];
}
Unfortunately, nothing comes out as a result for $driver_id (whereas $name returns a result).
Thank you for your help!
Are you looking for something like this:
<?php
$drid = "SELECT id FROM users WHERE CONCAT(firstName, ' ', lastName) LIKE '%".$name."%'";
$rest = mysql_query($drid);
while ($row = mysql_fetch_row($rest)) {
$driver_id = $row[0];
}
?>
I would suggest adding a new fullname field or using a temp table rather than using the concat, for performance reasons.
https://stackoverflow.com/a/29285246/3923450 should work though if you are looking for a temp solution
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.
Here is how my table "User" looks:
Name Password Favorites
Test Test 1 2 3 4
And in my table "Data" I have
Comment ID
"Test" 2
I want the the user to be able to save a comment to its favorites, hence the first table, where I save all favorites in one row, so the table doesn't get too big.
I try to get them all back by implode and IN clause.
Right now it does not seem to work and I hope that maybe someone here could give me some useful input on how to conquer this problem :)
$favoritenstring = ($GET_["favoritenstring"]);
$query = "SELECT * FROM $table_id WHERE ID in ('" . implode("','",$favoritenstring) . "')";
Right now I am getting this error on the above query line:
Invalid arguments passed
Your have to remove slashes from the string first:
$string = stripslashes($favoritenstring);
$query = "SELECT * FROM $table_id WHERE ID in " .$string;
Change This...
From
($GET_["favoritenstring"]);
TO
($_GET["favoritenstring"]);
Try this:
$query = "SELECT * FROM $table_id WHERE ID in ( ";
$myVars=explode($favoritenstring);
$numFavs=count(explode(' ', $favoritenstring));
for($i=0;$i<$numFavs;$i++)
{
$query.=$myVars[$i];
if($i<($numFavs-1))
{
$query.=", ";
}
}
$query.=");";