1

I want to print the name and last name of an ID entered in the text box. Here is the PHP and HTML code:

    <head>
        <title>
            Search your name by ID
        </title>
    </head>
<?php
if(isset($_POST["searchname"]))
{
    $id = $_POST["searchname"]; 
    $connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
    $a = mysql_query("Select * from users where id='$id'",$connect);
    $row = mysql_fetch_assoc($a);
    echo "$row[0] , $row[1] , $row[2]";
}
else
{
    echo "error";
}
?>
    <body>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <input type="text" maxlength="6" name="searchname">
            <input type="Submit" name="Submit">
        </form>
    </body>

Output when I enter ID:

 , , 

There are entries in the MySQL table but I am unable to fetch them. What is wrong with my code?

UPDATE: I have also tried mysql_fetch_array but it is not working.

2
  • 2
    mixing mysql and mysqli !!! Commented Sep 21, 2015 at 11:22
  • It looks like you are trying to echo the result between the head and the body? Are you sure that is what you want to do? Commented Sep 21, 2015 at 11:30

2 Answers 2

4

Main problem is that you're miximg mysqli and mysql. These are absolutely different APIs. Assuming you have

$id = $_POST["searchname"]; 
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");

Next you should:

$result = $connect->query("Select * from users where id='$id'");

Then get results:

while ($row = $result->fetch_assoc()) {
    var_dump($row);
}

And of course, instead of directly putting values into your query use prepared statements.

Update: about mistakes:

  • Your main mistake is mixing apis. When you use mysql (which is deprecated and you mustn't use it anymore) you can't use any of mysqli functions and vice versa.

  • Next - as you create mysqli object with new, you should work in object-oriented style, i.e. calling methods from your mysqli object.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<html>
    <head>
        <title>
            Search your name by ID
        </title>
    </head>
    <body>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            <input type="text" maxlength="6" name="searchname">
            <input type="Submit" name="Submit">
        </form>
    </body>
</html>

<?php
if(isset($_POST["searchname"])){

    $id = $_POST["searchname"]; 
    $connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
    $result = mysql_query("Select * from users where id='$id'",$connect);
    $row = mysql_fetch_assoc($result);
    print_R($row);
}else{

    echo "there is something wrong";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.