1

I've written a script that in short is supposed to query data from the database and echo a result into a HTML form field. However, I have been unsuccessful. Please see code below:

<?php


include("dbconfig.php");

$val = '6';

$result = mysqli_query("Select * from test where testid= '$val'");
$name = (mysqli_num_rows($result)==1) ? mysqli_fetch_assoc($result) : null;

if(is_array($name)){


?>

<html>

<body>

<form>
Name: <input type="text" id="firstname" value="<?php echo $name['firstname']; ?>"/>
</form>

<?php
} else {

echo "No such name exists";


}

?>



</body>
</html>

Can someone please tell me what I'm doing wrong. Because it won't echo anything into the field and I find it rather annoying because majority of the scripts I've come across are quite similar to this one.

Help will be much appreciated.

Thank You,

Sohail.

5
  • To be clear, the intended result here (if it was successful) would be: Name: <input type="text" id="firstname" value="6"/> Commented Nov 4, 2015 at 15:43
  • There could be a number of problems with your code. You might want to explain what happens when you run it for easier debugging. You could also dump some variables with var_dump($var). I can see that you are testing if mysqli_num_rows() equals one. This should be a "greater or equal" comparison, I think. Commented Nov 4, 2015 at 15:44
  • Sorry, that was my mistake... that was supposed to say "where testid = '$val'' Commented Nov 4, 2015 at 15:44
  • If you change that in your test code, does it work? Commented Nov 4, 2015 at 15:45
  • When I run the query, it echoes 'No such name exists' Commented Nov 4, 2015 at 15:46

2 Answers 2

1

I have tested the below and it works OK. @Fred-ii- gave you loads of good info, especially using error debugging - but you do need to supply the connection object which you were missing.

<?php
    error_reporting( E_ALL );

    include("conn.php");

    $val = 6;

    /* What is the name of the $connection object ? */
    $result = mysqli_query( $conn, "Select * from `test` where `testid`='$val'" );
    $name=( $result ) ? mysqli_fetch_assoc( $result ) : false;

?>  
<html>
    <head>
        <title>Ya gotta have a title...</title>
    </head>
    <body>
        <?php
            if( !empty( $name ) ){
                echo "
                    <form>
                        Name: <input type='text' id='firstname' value='{$name['firstname']}'/>
                    </form>";
            } else {
                echo "No such name exists";
            }
        ?>
    </
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your solution worked nicely. I did have to make some alterations to my database connection file as that was written as a php data object connection and not in mysqli format. But we're all good!
0

You did not pass your db connection to your query, so it never gets executed.

  • Assuming a successful connection using mysqli_

This line of code:

$result = mysqli_query("Select * from test where testid= '$val'");

needs to have a connection parameter:

$result = mysqli_query($connection, "Select * from test where testid= '$val'");

and is unknown to us as to which MySQL API you're using to connect with.

Your query may have failed, so check for errors.

$result = mysqli_query("Select * from test where testid= '$val'")
    or die(mysqli_error($connection));

and replacing the $connection variable with the one that you have assigned in your dbconfig.php which is unknown to us.

Different MySQL APIs/functions do not intermix.

Consult these following links http://php.net/manual/en/mysqli.error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code.

You're also open to an SQL injection. Use a prepared statement.

References:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


If you want to check if a row exists, see my other answer on Stack:

1 Comment

@MuhammadSohailArif "does not work" isn't much to go on really. check for errors, your query may have failed. I also included a link to one of my answers I gave for another question, which is at the bottom of my answer (in an edit). stackoverflow.com/a/22253579/1415724 you can't go wrong with that. Anyway, check for errors. Use error reporting also. php.net/manual/en/function.error-reporting.php

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.