Jump to content

php not running mysql query


brem13

Recommended Posts

i have a code made to check a database if a username already exists and its not running the mysql code to get the field and when i echo the result, it just shows the code 'select *.....'

$user = $values['username'];
mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error());

mysql_select_db($database) or die ("Could not select database because ".mysql_error());

$result = "select * from $table where username = '$user'";

//if username is taken
if ($values['username'] == $result)
	$errors['username'] = 'Username already taken';

and here is the html code that displays it

<td width="250px"><font face="tahoma" size=2>Username</font></td>
	<td width="250px"><input type="text" name="username" width="200px" value="<?= htmlentities($values['username']) ?>" style="font: 14px Tahoma; height=22px;"/></td>
	<?= $errors['username'] ?></td>

any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/198651-php-not-running-mysql-query/
Share on other sites

As a fact, the code is quite a mess.

   mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error());
   mysql_select_db($database) or die ("Could not select database because ".mysql_error());

   $user = $values['username'];
   $result = mysql_query("select * from $table where username = '$user'");
   $row = mysql_fetch_array($result);
   //if username is taken
   if ($values['username'] == $row['username']) {
      $errors['username'] = 'Username already taken';
   }

$row = mysql_fetch_array($result);

What this does, is gets the data from the query, into an array.

 

$row['username'];

As $row is now an array, you can now specifically get a certain collum from the database.

In this instance "username" is the collum name. So $row['username'] is the users username.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.