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
Related
First time asking on SO. I currently am trying to search a database where the first and last names are seperate. Example:
player_id | first_name | last_name
191 John Smith
192 Larry Citizen
193 Benjamin Example
I am trying to allow users to search this list using a full name only. I currently have the following code once the user hits submit, it calls usersearch.php.
session_start();
include '../con.php';
$player = $_POST['name'];
$sql = "SELECT * FROM characters WHERE (concat(first_name,' ',last_name)) = ($player)";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result)) {
echo "Found no-one with the name $player. <a href='../search.php'>Try Again?</a>";
} else {
$_SESSION['selplate'] = $row['plate'];
$_SESSION['selname'] = $row['first_name, last_name'];
header("Location: ../profile.php?player=$player");
}
No matter the query it will not find users and always returns "Found no-one with the name $player. Try again?"
This was supposed to be the easy part of this project and I am pulling my hair out.
I have spent over an hour searching SO and Google to no avail so it must be my code? afaik it should work.
you need qoutes ' ' around player because its a text and also concatinated.
if you search Johnsmith it will return nothing
if you search John Smith it will give you result, because in your concat you are adding a space between words
"SELECT * FROM characters WHERE concat(first_name,' ',last_name) =('".$player."')"
I would change it to a fulltext index on both first and last name, change the word min on full text indexes. then I would search using ( some guy as the name )
MATCH( first, last )AGAINST('+"some" +"guy"' IN BOOLEAN MODE )
delete the white-space from concat function in query and other thing is use $player = str_replace(' ','',trim($_POST['name'])) instead of $player = $_POST['name'].
$SQL = "SELECT * FROM TABLE_NAME WHERE concat(first_name,last_name) = '".$player."'"
i suggest PDO with prepared statements...
assume you are searching 'benjamin example' and query will check for 'benjamin example' so str_replace will output as benjaminexample.and the concated first_name and last_name will match it.
hope it help.
ignore if it sound silly.
I have no clue how to do this. I've looked up examples but it is all kind of vague for me using the #variable, I have no clue on what that is and how I can look up my answer.
I've got this variable $slotname which can be weapon or amulet or anything.
In my database it is weapon_name or amulet_name thats why the ._name. after the post
$slotname = $_POST['slot'].'_name';
$naam = 'Player1';
$sql = $db->prepare("SELECT #slotname FROM player WHERE naam = :naam");
$sql->execute(array(":naam" => $naam));
$fetch = $sql->fetch();
So in this case $slotname = weapon_name and I want to select the column weapon_name from the user Player1 but I have no clue how to do this.
Any tips?
Your database structure is essentially wrong.
There should be no such thing like slot_name field.
there should be a distinct table slots where all the slots have to be stored, and selected by values, not field names.
sql = $db->prepare("SELECT ".$slotname." FROM player WHERE naam = :naam");
I have the following code when I search for Juan Pedro Cruz nothing appears. But if I search only for juan, it does appear.
<?php
if(isset($_POST["btnssrch1"])){
$cno1 = $_POST["ssrch1"];
$p = mysqli_query($link,"select * from patient
where PatientId like '%$cno1%'
or FirstName like '%$cno1%'
or MiddleName like '%$cno1%'
or LastName like '%$cno1%'");
}
A few pointers for you:
Try to sanitize the inputs.
$cno1 = mysqli_real_escape_string($link, $_POST["ssrch1"]);
Since you are using multiple columns, your SQL should be like:
WHERE CONCAT(`FirstName`, ' ', `MiddleName`, ' ', `LastName`) LIKE '%{$cno1}%'
Change your query to something like below, which concentrates the full name and then compare the same with like, assuming PatientId is numerical,
$p = mysqli_query($link,"select * from patient where CONCAT(FirstName,' ',MiddleName,' ',LastName) like '%{$cno1}%'");
This won't create any problem if you are just learning in local machine, but in production, always sanitize user inputted data and never trust user data.
I have looked all over and at tons of code and examples.. This is such a small bit of code but I just can't seem to get it to work.
I have dbo.accounts which contains the id, username, password, createtime..
I have a simple form, you type in the username, and I need the select query to return the ID based on the username.
$result = mssql_query('SELECT id FROM dbo.account WHERE name = $username');
The dbo.gamemoney table will just insert some hardcoded info such as an amount of coins for the game..
My problem is that if I use a query as ID = 123, it works, but when I try to grab the id of dbo.accounts by using the username, I get nothing back.
I know it has to be something small, But I have tried to figure it out for so many hours now that I'm honestly lost..
Thanks for your time,
Chris
Since, $username is string type, you have to enclose it in quotes.
$result = mssql_query("SELECT id FROM dbo.account WHERE name = '$username'");
As a better practice would suggest use a try-catch scenario so that you get the exact error log. Try -
$result = mssql_query('SELECT id FROM dbo.account WHERE name = "'.$username.'"') or die('MSSQL error: ' . mssql_get_last_message());
Thanks everyone for the help!
I was able to get it working. Now I'll make sure it's the right way. I had forgot to add,
while($row = mssql_fetch_array($result)) {
$id = $row['id'];
$ip = $row['ip'];
}
Thats why the id was blank. I was missing some code.
Chris
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'] );