0

I have read a lot of these and they are for the most part and error in the database connection or the query. I am getting this error and I have a good connection and a good query. I know this because I print_r or echo the results of the $row and I get matching results.

<?php 
if (!isset($_POST['submit'])) {
    header("Location: http://tsec.tleeaa.org"); 
    exit;   
}
?>
<?php 
session_start();
require_once('Connections/db_tsectleeaa_connection.php');
require_once("_includes/functions.php"); 
?>
<?php $_SESSION['leadadv_email'] = $_POST['leadadv_email']; $email_compare =    $_SESSION['leadadv_email']; ?>
<?php
$dbquery  = "SELECT * ";
$dbquery .= "FROM `tsec_team_registration` "; 
$dbquery .= "WHERE `leadadv_email` = ";
$dbquery .= " '$email_compare' " ;

$result = mysqli_query($db_tsectleeaa_connection, $dbquery);
    if(!isset($result)) {
        die("Database query failed");
    }

while ($row = mysqli_fetch_row($result)){
print_r($row[10]);                  //should match next line
echo "<br />".$email_compare."<br />";          //should match line above
    if ($row[10] === $email_compare){
        echo "match!";

        } else echo "no match!";
} 
?>

the big problem is that the IF statement doesn't work. It will work when the statement is true but will not to to 'else' if it is false.

What this page does is verify against an email put in the previous page. Once they hit 'submit' the action for the form is this page. When the database has bacon@burger in the correct place and the user uses bacon@burger for the email address, the following result happens: bacon@burger bacon@burger match!

I am still getting this error:

( ! ) Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\index_action.php on line 24
Call Stack
#   Time    Memory  Function    Location
1   0.0005  256032  {main}( )   ..\index_action.php:0
2   0.0052  273264  mysqli_fetch_row ( )    ..\index_action.php:24

can anyone point me in the direction of what might be going on, please?

scratz
  • 17
  • 1
  • 1
  • 3
  • 1
    Check that you are connected to the database. The connection will return a resource if valid or false if not. – Jonathan Kuhn Feb 12 '14 at 22:12
  • $result should be coming from mysqli_result function and not mysqli_query function. – Maximus2012 Feb 12 '14 at 22:13
  • see if this helps: http://stackoverflow.com/questions/13053660/mysqli-query-mysqli-fetch-array-and-while-loop – Maximus2012 Feb 12 '14 at 22:15
  • Read this and stop injecting values directly into your queries - http://php.net/manual/mysqli.quickstart.prepared-statements.php – Phil Feb 13 '14 at 00:01

2 Answers2

-1

change that if statment to

  if(!$result) {
echo_Me
  • 37,078
  • 5
  • 58
  • 78
-1

OK THANKS EVERYONE FOR ALL THE FEEDBACK!!

I found the answer and, as I thought, it was a problem with my code, but it was inside the connections. Here's what happens.

There is a difference between CONNECTING to the database and making a QUERY to the db. In the connection, using mysqli, you only need the $hostname, $username and $password:

$hostname_db_tsectleeaa_connection = "localhost";
$database_db_tsectleeaa_connection = "tsectleeaa2014competition";
$username_db_tsectleeaa_connection = "dbtleeaauser";
$password_db_tsectleeaa_connection = "xxxxxxxxxxx";
$db_tsectleeaa_connection = mysqli_connect($hostname_db_tsectleeaa_connection, $username_db_tsectleeaa_connection, $password_db_tsectleeaa_connection, ***$database_db_tsectleeaa_connection***) or trigger_error(mysqli_error(),E_USER_ERROR); 
if(mysqli_connect_errno()) {
            die ("Database Connection Failed, ".$mysql_connect_error()." (". $mysql_connect_errno()." )");
        }
?>

What I didn't have in my db connection ($db_tsectleeaa_connection) was the actual variable for the database ($database_db_tsectleeaa_connection). Whereas that will connect the database itself, it can't produce a query. Once I loaded that into the connect string, it was fine.

scratz
  • 17
  • 1
  • 1
  • 3