Simple PHP/mySQL query?
I have a database that stores the users name, number, a开发者_高级运维nd 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'] );
精彩评论