0

So I have this rustic and basic php that will check a value in my database and if its corrects it will pull out the rest of that row I guess but my problem is how to echo certaine html if the result is okay and how to echo some other html if else.

btw atm else is not working, in other words if you input a code that is not on my db it will not show anything

<?php
$db_hostname = 'localhost';
$db_database = 'codedb';
$db_username = 'root';
$db_password = '';
$table = 'users';
$field = 'code';
$test = 'first_name';


// Connect to server.
$connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to 
connect to database! Please try again later.");

// Select the database.
mysql_select_db($db_database)
    or die("Unable to select database: " . mysql_error());

$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
    $result = mysql_query($query);
    if ($result) {
        while($row = mysql_fetch_array($result)) {
                $name = $row["$field"];
                $test = $row["$test"];
                echo "Hello: $name $test";
            }
    }
    else {
        echo "Im sorry you buddy, you are not a winner this time! $test";
    }
   mysql_close($connection);
?>  
4
  • 1
    just so you're aware, the script is open to sql injection, and mysql_* functions are obsolete in newer version of php :) Commented Apr 28, 2014 at 3:55
  • 1
    You REALLY want to move to PDO and prepared statements...But that is beyond the scope of this question. Commented Apr 28, 2014 at 3:55
  • thanks I wasnt aware of the injection but I was hoping for something like that since im just starting to code and yup I know that thanks tho Commented Apr 28, 2014 at 3:56
  • I personally like Zend Frameworks DB engine or ADODB (though it's really out dated now). But there are dozens of different frameworks out there. Commented Apr 28, 2014 at 4:03

5 Answers 5

1

You can use mysql_num_rows(), it's the best way.

if( mysql_num_rows($result) > 0 ){
/* Anything you want here on success */
} else {
/* Anything you want here on failure */
}
Sign up to request clarification or add additional context in comments.

Comments

0
if(mysql_num_rows($result) > 0) { echo "output"; } else { echo "error msg"; }

2 Comments

this worked, but I still dont know how to echo a full page for the different results
I get it but I tried to input a whole body and it did not work
0

Use mysql_num_rows against the query.

if(mysql_num_rows($result)){ } else { }

Comments

0

Instead of echoing every html value you can simply enclose html in if statements

like:

<?php
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if ($result) {
    while($row = mysql_fetch_array($result)) {
            $name = $row["$field"];
            $test = $row["$test"];
?>
<b>Hello: <?php echo $name $test"; ?></b>
<?php
        }
}
else {
?>
    <b> Im sorry you buddy, you are not a winner this time! </b> <?php echo $test"; 
}
mysql_close($connection);
?>

1 Comment

Yep, you could because mysql_result returns false on failure.
0

First you have to correct syntax of database selection like this

mysql_select_db($db_database,$connection);

Insted of

mysql_select_db($db_database);

Check Manual

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.