PHP query will not run

  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England

Post 3+ Months Ago

I have been working on a script for a login form but the sql will not run to the database, now its been years since I have worked with php and mysql so I am a little rusty but I can not see the bug, I am sure it is small.

Anyway what happens is that I try to run the query and my site gos to the "or die" part of the code and so says could not run query, so I know its "talking to the DB" so its something to do with the query, any ideas below is the code.

Code: [ Select ]
<form action="#" method="post" id="loginform">
<label>e-mail<input type="email" name="email" /></label><br />
<label>password<input type="password" name="pass" /></label><br />
<input type="submit" class="button" value="Log In" name="submit" />
</form>

<?php

if(isset($_POST['submit'])){
    $email = $_POST['email'];
    $pass = $_POST['pass'];
    
    $con = mysqli_connect('localhost','root','') or die ('Could not connect to databace');
    mysqli_select_db($con, 'mysitesdb');
    
    $query = mysqli_query($con, 'SELECT * FROM users WHERE email="$email" AND pass="$pass"') or die ('Could not run query');
    $result = mysql_result($query, 0);
    echo 'result = '.$result;
    
    mysqli_close($con);
    
    if($result != 1){
    echo "More than one record found";
    }else{
    echo "Log in done";
    $_SESSION['email'] = $email;
    }
}
?>
  1. <form action="#" method="post" id="loginform">
  2. <label>e-mail<input type="email" name="email" /></label><br />
  3. <label>password<input type="password" name="pass" /></label><br />
  4. <input type="submit" class="button" value="Log In" name="submit" />
  5. </form>
  6. <?php
  7. if(isset($_POST['submit'])){
  8.     $email = $_POST['email'];
  9.     $pass = $_POST['pass'];
  10.     
  11.     $con = mysqli_connect('localhost','root','') or die ('Could not connect to databace');
  12.     mysqli_select_db($con, 'mysitesdb');
  13.     
  14.     $query = mysqli_query($con, 'SELECT * FROM users WHERE email="$email" AND pass="$pass"') or die ('Could not run query');
  15.     $result = mysql_result($query, 0);
  16.     echo 'result = '.$result;
  17.     
  18.     mysqli_close($con);
  19.     
  20.     if($result != 1){
  21.     echo "More than one record found";
  22.     }else{
  23.     echo "Log in done";
  24.     $_SESSION['email'] = $email;
  25.     }
  26. }
  27. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

You need to swap your quotes around:

PHP Code: [ Select ]
"SELECT * FROM users WHERE email='$email' AND pass='$pass'"


Variables are only interpolated if the entire string itself is double-quoted.

You'll also need to fetch your results as mysqli_query simply returns a resource.

Finally, you can't mix and match mysql functions with mysqli functions. Pick one and stick with it.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 143 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.