Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:14:26+00:00 2026-06-08T05:14:26+00:00

I have this code : PHP : [CONNECT TO DATABASE OR DIE ( THIS

  • 0

I have this code :

PHP :

[CONNECT TO DATABASE OR DIE ( THIS WORKS )]   
if(isset($_GET['search'])) // If it's submitted 
    { 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT name FROM user WHERE (id LIKE '%$inp%')"; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); 
    if(mysql_affected_rows()===0) // If no match found 
        echo "{$inp} is not in our database."; 
    else 
        { 
        echo "<p>{$inp} was successfully searched.</p>"; // Yes, the query worked 
        while($row=mysql_fetch_array($r)) // Loop through the query results 
            echo "{$row[0]}<br>"; // Show the results 
        } // End of the else statement 
    } // End of the if statement 

function Clean($str) // Clean my input 
{ 
    return mysql_real_escape_string(strip_tags(trim($sStr))); // Remove traces of injection 
}

HTML :

<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> 
<input name="inpname" type="text"> 
<input type="submit" name="search" value="Search"> 
</form>

I wanna search members in my database by ID but when I search for example 1 I have no results : is not in our database. (/index.php?inpname=1&search=Search)

If I use ‘%”.$inp.”%’ instead of ‘%$inp%’ it’s works but It’s showing me all users from my database .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:14:28+00:00Added an answer on June 8, 2026 at 5:14 am

    If you check the php manual says:

    int mysql_affected_rows ([ resource $link_identifier = NULL ] )
    

    Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

    http://php.net/manual/en/function.mysql-affected-rows.php

    So, no return values if you do a query with SELECT.

    Change your code for something like this

    if(isset($_GET['search'])) // If it's submitted 
    { 
        $inp = Clean($_GET['inpname']); // Clean my input 
        $sQuery="SELECT name FROM user WHERE id LIKE '%$inp%'"; // mySql query 
        $r = mysql_query($sQuery) or die(mysql_error());
    
        if (mysql_num_rows($r))
        {
           while ($row=mysql_fetch_array($r))
           {
               echo $row['name']."<br>"; // Show the results 
           }
    
        }
        else
        {
           echo "{$inp} is not in our database.";  
        }   
    
    } // End of the if statement 
    

    if you want only one row

    if(isset($_GET['search'])) // If it's submitted 
    { 
        $inp = Clean($_GET['inpname']); // Clean my input 
        $sQuery="SELECT name FROM user WHERE id LIKE '%$inp%' LIMIT 1"; // mySql query 
        $r = mysql_query($sQuery) or die(mysql_error());
    
        if (mysql_num_rows($r))
        {
           $row=mysql_fetch_array($r)
           echo $row['name']."<br>"; // Show the results 
        }
        else
        {
           echo "{$inp} is not in our database.";  
        }   
    
    } // End of the if statement 
    

    and if you want search by exact id

    if(isset($_GET['search'])) // If it's submitted 
    { 
        $inp = Clean($_GET['inpname']); // Clean my input 
        $sQuery="SELECT name FROM user WHERE id = $inp LIMIT 1"; // mySql query 
        $r = mysql_query($sQuery) or die(mysql_error());
    
        if (mysql_num_rows($r))
        {
           $row=mysql_fetch_array($r)
           echo $row['name']."<br>"; // Show the results 
        }
        else
        {
           echo "{$inp} is not in our database.";  
        }   
    
    } // End of the if statement 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this PHP code to highlight the Query on search results. if (isset($_REQUEST['k'])){
i have this PHP code, and a DataBase with Question, answer1, answer2, Question_id well,
I have this code in PHP. It connects to the DB fine, but pops
I have this code in PHP writing views to a text-file and just increasing
i have this code in php and it's supposed to print me the image
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
I have this PHP code in a loop for every post on the admin
i have this php code and keep on getting errors upon fixing each error.
I have this php code $filename = verbs.php; // http://alylores.x10.mx/vega/verbs2.php $handle = fopen($filename, r);
I have this code in userpage.php: <script langauge=JavaScript><!-- function newWindow(fileName,windowName) { msgWindow=window.open(fileName,windowName); } //--></script>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.