How to search matching charecter in mysql - php

$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE Sample_name LIKE '$userinput' OR Client_name LIKE '$userinput1%'") or die(mysqli_error($conn));
I am trying to execute above query, but its outputting full details from mysql database.
how to fix this issue?
thanks in advance!!

Missing % -
Sample_name LIKE '%$userinput%' OR Client_name LIKE '%$userinput1%'
Check the docs

You have to use '%' operator. A Like always expect a %. Try this -
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE Sample_name LIKE '%$userinput'") or die(mysqli_error($conn));

you missed % sign in $userinput
$result = mysqli_query($conn, "SELECT * FROM ProcessTrackingSystem.ProcessDetails WHERE Sample_name LIKE '%$userinput%'") or die(mysqli_error($conn));

Related

Tryning to get the right information from mysql

I'm trying to call the correct mysql information.
In my first query, I check if the order is available and take the post_id so I can make a SELECT that includes all post_id's for my while.
In the first query is there many post_id's so I need to connecte tto many id's
An example that does not work but explain what I mean:
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts`
where ID='order_id1' and ID='order_id2' and ID='order_id3'
") or die(mysqli_error($conn));
The $orderidtilsoog can be more IDs and not just 1
Hope you understand me
The is the completaly code:
$query = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_postmeta` where meta_value='$emailconvert' ORDER BY meta_id ") or die(mysqli_error($conn));
echo "Kunden er fundet og ligger på https://v1.com";
while($row55 = mysqli_fetch_array($query)) {
$orderidtilsoog = $row55['post_id']. ",";
echo $orderidtilsoog;
if(mysqli_num_rows($query) == '0') {
}else{
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts` where ID
IN ('$orderidtilsoog') ") or die(mysqli_error($conn));
}
Just use SELECT * FROM $v1.wpd2_posts where ID IN('$orderidtilsoog')

PHP Fetch Data From Database

I'm trying to fetch data from hashtag where is #a2 or #ar hashtag but not work with my code
$sql_query = mysqli_query($conn, "SELECT * FROM posts WHERE hashtag=#a2");
try this,
$data = "#a2";
$sql_query = mysqli_query($conn, "SELECT * FROM posts WHERE hashtag LIKE '%$data%'");
$sql_query = mysqli_query($conn, "SELECT * FROM posts WHERE hashtag LIKE '%#a2%'");
You can also do it using your method but you just have to enclose #a2 in quotes like
$sql_query = mysqli_query($conn, "SELECT * FROM posts WHERE hashtag='#a2'");
Hope it helps

How to fetch data?

This statement gives error
unable to fetch result from database
$res =$dbcon->exec("select *from tablename ");
$res->fetch();
Give an error.
change only one query
$res =$dbcon->exec("select *from tablename ");
to
$res =$dbcon->query("select *from tablename ");
You need a space in your query:
$res = $dbcon->exec("select * from tablename");
Depending on what $dbcon is, you may need to use the query method instead (part of alok.kumar's answer):
$res = $dbcon->query("select * from tablename");
Referenece: http://www.php.net/manual/en/pdo.query.php

Issue with mysql query when changing value to a variable

$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'

Using an array in a Where clause

So what I need to do is find in my tables where id = id and name = name. I have tried this with no success.
$sql="SELECT * FROM $mysqltable WHERE id='$id' && name='$name'"
EDIT I found that I was missing a } bracket below this and the above code does work :/ Thank you everyone.
This is how i would have done it: Also try entering the table name instead of a variable.
$sql = "SELECT * From $mysqltable WHERE id='$id' AND name='$name'";
$result = mysql_query($sql) or die(mysql_error());

Categories