-1

I am newbie in PHP+MySQL and learn it myself. I got some problem (i suppose it may be very simple and stupid) but i can't fix it. OK. I want to select some record from my database: I use the function to connect to database:

   function connect_bd() {
   $result = new mysqli('localhost', 'root', '*****', 'book_kz');
   if (!$result) {
      return false;
   } else{
    $result->autocommit(TRUE);
   return $result;
}
}

it works properly. Then i wrote verification function:

function log_in($user_name, $passwrd) {
//  verification of user's name and password in database
// if yes - return true
// otherwise - false

  // connect to database
  $connect = connect_bd();
  if (!$connect) {
     return 0;
  }

  // verification procedure
  $result = $connect->query("select * from admin
                         where user_name='".$user_name."'
                         and passwrd = sha1('".$passwrd."')");

  if (!$result) {
   echo "Incorrect password!".$connect->mysqli_errno;
// return to logging in menu
create_html_url("logo_in.php", "Return to logging menu");
return 0;
     }

  if ($result->num_rows>0) {

     return 1;
  } else {  

     return 0;
  }
$connect->close();
}

It doesn't work. Especially something is going wrong with $connect->query. I put query "select * from admin where user_name =...." directly into MYSQL environment - it works correctly. But $result = $connect->query("select... shows nothing. I inserted next commands echo "</br> print something</br>"; and echo "</br>print something".$result."</br>";. First command shows me string print something and next one shows nothing! It is looking like failure of $result blocks printing. I've checked a some samples about mysqli::query and didn't found anything wrong. I will be very appreciated anybody ready to help me. Thank you in advance....

  • `$result` isn't directly printable, it's not a simple value. Other than incorrectly trying to *print* it, is there any other indication that this is actually failing? – David Apr 14 '17 at 10:42
  • It is not problem with printing. I can't print because command $connect->query doesn't work properly. – George Ufimcev Apr 14 '17 at 11:49
  • What indication do you have that it doesn't work properly? According to your question, the indication is that you tried to print the query result. That test is invalid, so it doesn't indicate any actual problem. If you have *other* helpful information about the problem, including that information would be, well, helpful. – David Apr 14 '17 at 11:51
  • No. my general goal is to take some records from my database(fuction log_in). It is failed. I start checking and found the command $result = $connect->query("select * from admin.... is not working correctly.THIS IS THE MAIN PROBLEM. I am using printing(echo) to see intermediate values of $result and $connect variables (some kind of debugging)... ANYWAY! If i try to check source code, brouser shows me mistake (ERR_CACHE_MISS) and asks to reload my page. I reload my login page with login/password and nothing changed – George Ufimcev Apr 14 '17 at 12:14
  • Instead of vaguely describing what your debugging attempt looks like, maybe you could show it in the question? You keep insisting that there's a problem, but you aren't actually indicating what the problem is. As far as anybody here can tell you're just *assuming* that your query is failing. Additionally, your debugging should include what the actual query is that you're executing. Currently your code is wide open to SQL injection, so you could be executing anything. This community genuinely wants to help you solve the problem, but you have to provide information *about* the problem. – David Apr 14 '17 at 12:24
  • I am not reply in clear way.Sorry. Why the problem is with query? 1. Connection to database made correctly. 2. I put markers like this echo '1111'; inbetween every command. 3. I put marker echo " result = ".$result.""; after command $result = $connect->query("select...4. I see my function log_in was executed and stalled at the point echo " result = ".$result.""; 5. If I modified my last command into echo " result = ""; ALL markers were passed and function log_in was executed properly. I concluded that something is wrong with $result and with connect->querry... – George Ufimcev Apr 14 '17 at 12:48

1 Answers1

0

When you call a class constructor with new it will basically always return a class. So this piece of code is wrong:

$result = new mysqli('localhost', 'root', '*****', 'book_kz');
if (!$result) echo "Oh, there's an error!";

Instead do:

$mysqli = new mysqli('localhost', 'root', '*****', 'book_kz');

// a connect_errno exists so the connection attempt failed!
if ($mysqli->connect_errno) {
  echo "Error: Failed to make a MySQL connection, here is why: \n";
  echo "Errno: " . $mysqli->connect_errno . "\n";
  echo "Error: " . $mysqli->connect_error . "\n";
  exit;
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Never output error messages directly to the browser – Your Common Sense Apr 14 '17 at 11:07
  • I don't have neither problem with constructor new(msqli) and connection no with printing. I got problem with executing of command: $result = $connect->query("select * from admin... It doesn't return database records... – George Ufimcev Apr 14 '17 at 11:21
  • @George: I just wanted to point our a problem in your code. Clearly this is problem, even if you don't perceive it as such. "You Common Sense" is right, of course, you should process the real error internally and show the visitor a message like "Something went wrong. We will look into it.". – KIKO Software Apr 14 '17 at 11:44
  • I agree but i still don't know the source of the problem and how to fix it. ANYWAY! If i try to check source code, brouser shows me mistake (ERR_CACHE_MISS) and asks to reload my page. I reload my login page with login/password and nothing changed.... – George Ufimcev Apr 14 '17 at 11:47
  • Again only for debugging: 1. Try echo'ing the `$user_name` and `$passwrd` inside your `log_in()` function, to see you do call it correctly. 2. Put the query inside a variable, just before `$connect->query()`, and echo it before you use it. Again to check that the query is what you expect it to be. 3. Don't forget to `fetch_row();` the result, see http://php.net/manual/en/mysqli.autocommit.php AutoCommit does not 'AutoFetch'. Please check the manual, read it. Most methods, you use, return something else than you expect. – KIKO Software Apr 14 '17 at 12:17