0

I am not sure how to go about this in PHP & MySQL;

But basically, I just want to check to see if a row exists in a table, and if it does, return a error, example:

$exists = MYSQL CODE TO CHECK HOW MANY ROWS INCLUDE BADGE_ID
if($exists >= 1)
{
$errors[] = "Exists.";
}

Something like that, because I'm coding a small shop script and I want it to check to make sure that they don't already have the badge_id. Structure of the db is user_id and badge_id (user_id = 1, badge_id = 1; for an example)

1
  • try mysql_num_rows Commented May 3, 2014 at 9:12

6 Answers 6

1
//Mysql
$res = mysql_query("YOUR QUERY");
if(mysql_num_rows($res) > 0) {
   $errors[] = "Exists.";
}

//PDO
$query = $db->prepare("YOUR QUERY");
$ret = $query->execute();
if($ret && $query->rowCount() > 0) {
   $errors[] = "Exists.";
}
Sign up to request clarification or add additional context in comments.

Comments

0

for this query as

$query=mysql_query("SELECT *  from table WHERE userid = 1 AND badgeid = 1");

and in php

if(mysql_num_rows($query)>0){

// whatever you want if match found 

}else{

echo "No match Found".

}

Comments

0
$sql = "SELECT COUNT(*) FROM `table_name` WHERE `user_id` = '1' AND `badge_id` = '1' "
$exists = mysql_query($sql) ; 
if(mysql_num_rows($exists) >= 1) {
      $errors[] = "Exists.";
}else {
   // doesn't exists.
}

Comments

0

Try using PDO instead of the old mysql_query() functions for they are deprecated since PHP 5.5.

For a simple query:

$slcBadge = $con->query('SELECT badge_id FROM badges');
if ($slcBadge->rowCount() > 0) {
  $errors[] = 'Exists.';
}

Or if you want to fetch all rows from a single user:

$sqlBadge = 'SELECT id_badge FROM badges WHERE id_user = :idUser';
$slcBadge = $con->prepare($sqlBadge);
$slcBadge->bindParam(':idUser', $idUser, PDO::PARAM_INT);
$slcBadge->execute();
if ($slcBadge->rowCount() > 0) {
  $errors[] = 'Exists.';
}

Notice that in the second piece of code I used prepare() and execute() rather than query(). This is to protect you query from SQL injection. By using prepare() you fix the construct of your query so if a user enters a query string as a value, it will not be executed. You only need to prepare a query if a user can enter a value which will be used in your query, otherwise query() will do just fine. Check out the injection link for more detailed info.

Comments

0

Your version of PHP is important:

  • mysql_* functions are deprecated as of 5.4, and
  • Removed as of 5.5

It is advised to either implement PDO or Mysqli

mysql: This extension is now deprecated, and deprecation warnings will be generated when connections are established to databases via mysql_connect(), mysql_pconnect(), or through implicit connection: use MySQLi or PDO_MySQL instead Dropped support for LOAD DATA LOCAL INFILE handlers when using libmysql. Known for stability problems Added support for SHA256 authentication available with MySQL 5.6.6+

For reference please see the changelog

Structuring your Query

First of all I'm assuming you are indexing your fields correctly refer to this article I posted on Stack Exchange.

Second of all you need to consider efficiency depending on the volume of this table: doing a SELECT * is bad practice when you only need to count the records - mysql will cache row counts and make your SELECT Count(*) much faster. with indexes this is furthermore efficient.

I would simply consider something along the line of this:

$dsn = 'mysql:host=127.0.0.1;dbname=DATABASE';
$db = new PDO($dsn, 'username', 'password', array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
));

NOTE: where host=127.0.0.1 if your user has been granted access via localhost then you need this to state localhost - or grant the user privileges to 127.0.0.1

NOTE: with SET NAMES there is also a bug with the PDO driver from 5.3 (I believe) whereby an attacker can inject nullbytes and backspace bytes to remove slashing to then inject the query.

##Quick example:

// WARNING: you still need to correctly sanitize your user input!
$query = $db->prepare('SELECT COUNT(*) FROM table WHERE user_id = ? AND badge_id = ?');
$query->execute(array((int) $userId, (int) $badgeId));

$total = $query->fetchAll();

Comments

0
$result = mysql_query("SELECT COUNT(*) FROM table WHERE userid = 1 AND badgeid = 1 LIMIT 1") or die(mysql_error());
if (mysql_result($result, 0) > 0)
{
   echo 'exist';
}
else
{
   echo 'no';
}

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.