Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a row exists using old mysql_* API

Tags:

I just want to check and see if a row exists where the $lectureName shows. If a row does exist with the $lectureName somewhere in it, I want the function to return "assigned" if not then it should return "available". Here's what I have. I'm fairly sure its a mess. Please help.

function checkLectureStatus($lectureName) {  $con = connectvar();  mysql_select_db("mydatabase", $con);  $result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName'");   while($row = mysql_fetch_array($result));   {      if (!$row[$lectureName] == $lectureName)      {          mysql_close($con);          return "Available";      }       else      {         mysql_close($con);         return "Assigned";     } } 

When I do this everything return available, even when it should return assigned.

like image 460
Jeff Avatar asked Mar 31 '12 05:03

Jeff


1 Answers

Easiest way to check if a row exists:

$lectureName = mysql_real_escape_string($lectureName);  // SECURITY! $result = mysql_query("SELECT 1 FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1"); if (mysql_fetch_row($result)) {     return 'Assigned'; } else {     return 'Available'; } 

No need to mess with arrays and field names.

like image 149
kijin Avatar answered Oct 18 '22 01:10

kijin