This is my search code,it works but when I got to print go even without entering any character. It still searches. Worst, it prints all the items in the database what would I do. Thanks.
if(isset($_GET['search'])) {
$search_value= $_GET['searchbox'];
//$sql="SELECT idemp,sn FROM employee
//WHERE idemp like '%$search_value%' OR
//sn like '%$search_value%'";
$sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "'$result->num_rows' result/s for '$search_value'";
print "<br><br>";
You might need to change your if condition as
if(isset($_GET['search']) && !empty($_GET['searchbox'])){
// do search
}
if(isset($_GET['search'])) will return true because it is set to empty string.
if(isset($_GET['search']) && $_GET['search'] )
is the condition you are looking for
First check that your getting any string or not,
Run your query only when you got something.
if(isset($_GET['searchbox']) && $_GET['searchbox'] != ''){
$search_value= $_GET['searchbox'];
$sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "'$result->num_rows' result/s for '$search_value'";
print "<br><br>";
}
Related
I have this php code:
$query = $database->query("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID')";
if($query == 0){
echo "not registered";
}elseif($query == 1){
echo "registered"
}
If I'm not wrong, the query is suppose to return 0 or 1 and it works in my SQLite manager. What is the correct way on getting that value in Php and use it in IF ELSE statement?
If you only need a single value, you can use querySingle:
$result = $database->querySingle("SELECT EXISTS(SELECT * FROM contacts WHERE contact_id = '$contactID'");
Otherwise, with normal queries, the result returned by ->query isn't actually the data itself, but an identifier you would use to get data from the database:
$results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
How to get value from select query without using while loop while we know that output is defiantly only one record
$sql = "SELECT id FROM MyGuests";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo $row["id"];
}
Here if i know that there is only one record comes as a output then how to avoid while loop and directly get our id
just delete the while loop!
$sql = "SELECT id FROM MyGuests";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row["id"];
are you using mysql_* functions by any chance? please switch to PDO as soon as possible.
You can use MYSQl bind result if its a single row output
if( isset($con) && !empty($con) && $con!="" ) {
$knownStmt=mysqli_prepare($con, "SELECT id FROM MyGuests");
if( $knownStmt ) {
mysqli_stmt_execute($knownStmt);
mysqli_stmt_bind_result($knownStmt,$id);
mysqli_stmt_fetch($knownStmt);
mysqli_stmt_close($knownStmt);
}
}
Please try this. This is one of the best way. You can also pass the where condition also and bind the value this query. Please see below is the example for the same.
if( isset($con) && !empty($con) && $con!="" ) {
$knownStmt=mysqli_prepare($con, "SELECT name FROM MyGuests WHERE id=?");
if( $knownStmt ) {
mysqli_stmt_bind_param($knownStmt,"d",$UID);
mysqli_stmt_execute($knownStmt);
mysqli_stmt_bind_result($knownStmt,$Name);
mysqli_stmt_fetch($knownStmt);
mysqli_stmt_close($knownStmt);
}
}
I'm sure this will definitely help you.
Please note this works only for single row result.
You can use this code for your purpose.
$result = mysql_query("SELECT id FROM MyGuests");
$row=mysql_fetch_array($result);
echo $row["id"];
I've found several tutorials which have similar code like the following:
$sql = "select * from users";
$result = $conn1->Execute($sql);
if ($result==false) {
print 'error' . $conn1->ErrorMsg() . '<br>';
} else {
print_r($result->GetRows());
}
But how can $result ever be false? If I add a where clause which can not be fulfilled the else-branch is still taken since $result contains the column titles. Examples:
"select * from users"; // Select the whole table data
echo "$result";
leads to
id,username,password 1,peter,geheim 2,sabine,secret 3,thorsten,qwertz
Whereas
"select * from users where username = 'does not exist'";
echo "$result";
leads to
id,username,password
Therefore result is never false. What is my mistake here?
The Execute method returns false if the query itself fails, and not if it has 0 results.
If you want to check if the query returned any results you can use the RecordCount method.
$rows = $conn1->Execute($sql);
if ($rows->RecordCount() > 0) {
// Do something with your rows
} else {
// Nothing returned
}
This is my code :
if(isset($_GET['search'])) {
$search_value= $_GET['searchbox'];
echo "Search results for $search_value";
print "<br><br>";
$query="
SELECT idemp,sn
FROM employee
WHERE idemp like '%search_value%'
OR sn like '%search_value%'";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc($query)) {
$idemp=$row->idemp;
$sn=$row->sn;
echo $row['$idemp'];
echo $row['$sn'];
}
}
};
it didn't return matching result even if there is an equivalent in the database... help tnx...
Your'e not including the variable in the query, you're including only its name (without the $?).
$query="SELECT idemp,sn FROM employee
WHERE idemp like '%$search_value%' OR
sn like '%$search_value%'";
Since you're using mysqli, it would be better to bind the values either way, since it will normalize the input and prevent injection. Here's an example:
$conn->prepare("SELECT idemp,sn FROM employee
WHERE idemp like ? OR
sn like ?");
$conn->bind_params('ss', $search_value, $search_value);
$conn->execute(); // <-- returns the query results
I want to show user if he liked a image or not..
for that I am creating php code
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if($row){
echo "unlike";
}
else{
echo "like";
}
I can not do this for everything like 'tags', 'shares', 'comments', 'favourites' ...many
Isn't there anything simpler than this...?
Like say $row_check=mysqli_check_exist($table,$column_name,$userid);
use mysql fetch row method
$num_row = mysqli_num_rows($query);
if($num_row>0)
{
//add your code
}
else
{
//add your code
}
There are a lot of ways of doing this really but if you arnt going to use any more information then weither or not the user has liked it doing select * is a bad idea. The reason why is that you are asking the database to return the value of every column in that table.
Assuming its a small database its probably not a problem no but as your database gets bigger you are puting more load on it then you need you should try and only select the columns you need and intend to use. Ok in this case the userid is probably indexed and its only one row, but if you get in the habit of doing it here you may do it else where as well.
try this instead.
$userid=$_COOKIE['userid'];
$sql = "SELECT count(user_id) as total FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if( $row ['total'] > 0){
echo "unlike";
}
else{
echo "like";
}
This way we are just getting the total. simple and elegant
Use mysqli_num_rows($query) if > 0 exist
You simply need to count the available records using
mysqli_num_rows($query);
This will return a number (count) of available records
So simple put a check like this :
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if($count>0){
echo "unlike";
}
else{
echo "like";
}