Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck if username exists in mysql database?
    primarykey
    data
    text
    <p>Ok so have this code, its for a registration form but i can't get it to check if there is already the username taken? all it does is creates the same username in the database again, instead of returning "Username already taken" if anyone could help would be very appreciated.</p> <pre><code>$checkusernametaken = mysql_query("SELECT FROM users (username) WHERE username=username"); if(mysql_num_rows($checkusernametaken)&gt;=1) { echo"Username already exists"; } else { $sql = "INSERT INTO users(username, password) VALUES(:username, :password)"; $stmt = $con-&gt;prepare( $sql ); $stmt-&gt;bindValue( "username", $this-&gt;username, PDO::PARAM_STR ); $stmt-&gt;bindValue( "password", hash("sha256", $this-&gt;password . $this-&gt;salt), PDO::PARAM_STR ); $stmt-&gt;execute(); return "Registration Successful Welcome " + $checkusernametaken + "&lt;br/&gt; &lt;a href='index.php'&gt;Login Now&lt;/a&gt;"; } </code></pre> <p>This is the code i used and i got it working with the help of everyone in the comments, thanks heaps guys!</p> <pre><code>try { $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $con-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql = "INSERT INTO users(username, password) VALUES(:username, :password)"; $stmt = $con-&gt;prepare( $sql ); $stmt-&gt;bindValue( "username", $this-&gt;username, PDO::PARAM_STR ); $stmt-&gt;bindValue( "password", hash("sha256", $this-&gt;password . $this-&gt;salt), PDO::PARAM_STR ); $stmt-&gt;execute(); return "Registration Successful &lt;br/&gt; &lt;a href='index.php'&gt;Login Now&lt;/a&gt;"; } catch( PDOException $e ) { return "Username Taken &lt;br/&gt; &lt;a href='register.php'&gt;Try Again?&lt;/a&gt;"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload