0

I'm trying to find if a user exists using this code:

$usercheck = "SELECT * FROM profiles WHERE username = '$user'";
$rs = mysqli_query($con,$usercheck);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);

echo "<script> console.log('data=' + " . $data[0] . " + ''); </script>";

Data always returns 0 despite $user being equal to a name which already exists.

Any ideas?

1
  • Your query is open to SQL injection. What's the actual query being executed at runtime? If you do a var_dump() on $data, what is being returned by that query? What does $data[0] actually contain in the results? Commented Mar 5, 2017 at 15:59

2 Answers 2

1

try iterate on your result and show more that the first column (should be the first column of the firts row is 0

while ($row = mysqli_fetch_array($rs, MYSQL_NUM)) {
   printf("col0: %s  col1: %s", $row[0], $row[1]);  
}

or try refer to column name istead of column position

while ($row = mysqli_fetch_array($rs, MYSQL_NUM)) {
   printf("colUsername: %s  col1: %s", $row['userane']);  
}
Sign up to request clarification or add additional context in comments.

1 Comment

The OP is using MySQLi not MySQL.
0

Try to replace

$data[0]

database field name, like

$data['username']
echo "<script> console.log('data=' + " . $data['username'] . " + ''); </script>";

Hope it will works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.