Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It would be easier if you just did a quick true/false check in the SQL and checked the flag that came back.</p> <pre><code>$sql = "SELECT " . "(SELECT 1 FROM `users` WHERE `username` = '" . mysql_real_escape_string($username) . "'), " . "(SELECT 1 FROM `users` WHERE `email` = '" . mysql_real_escape_string($email) . "')"; $query = mysql_query($sql); if (mysql_num_rows($query) &gt; 0) { $foundFlags = mysql_fetch_assoc($query); if ($foundFlags['username']) { $error[] = "username is existing"; } if ($foundFlags['email']) { $error[] = "email is existing"; } } else { // General error as the query should always return } </code></pre> <p>When it does not find an entry, it will return NULL in the flag, which evaluates to false, so the <code>if</code> condition is fine.</p> <p>Note that you could generalise it for a field list like this:</p> <pre><code>$fieldMatch = array('username' =&gt; $username, 'email' =&gt; $email); $sqlParts = array(); foreach ($fieldMatch as $cFieldName =&gt; $cFieldValue) { $sqlParts[] = "(SELECT 1 FROM `users` WHERE `" . $cFieldName . "` = '" . mysql_real_escape_string($cFieldValue) . "')"; } $sql = "SELECT " . implode(", ", $sqlParts); $query = mysql_query($sql); if (mysql_num_rows($query) &gt; 0) { $foundFlags = mysql_fetch_assoc($query); foreach ($foundFlags as $cFieldName =&gt; $cFlag) { if ($foundFlags[$cFieldName]) { $error[] = $cFieldName . " is existing"; } } } else { // General error as the query should always return } </code></pre> <p><em>NB.</em> Note that assumes all fields are strings, or other string-escaped types (eg. date/time).</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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