Object Oriented Style

Does Record Exist
  $stmt = $maindb->prepare("SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'");
  $stmt->bind_param("si", $MyColumn1, $MyColumn2);
  $MyColumn1 = "abc";
  $MyColumn2 = 12;
  $stmt->execute();
  $stmt->store_result();
  if ($stmt->num_rows <= 0)
  {

  }
  $stmt->close();
Simple Get A Result
  $stmt = $maindb->prepare("SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'");
  $stmt->bind_param("si", $MyColumn1, $MyColumn2);
  $MyColumn1 = "abc";
  $MyColumn2 = 12;
  $stmt->execute();
  $Results = $stmt->get_result();
  if ($Result = $Results->fetch_assoc())
  {
    $MyColumn4 = $Result['MyColumn4'];

  }
  $Results->free();
  $stmt->close();

Alternative with check for no result

  $stmt = $maindb->prepare("SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'");
  $stmt->bind_param("si", $MyColumn1, $MyColumn2);
  $MyColumn1 = "abc";
  $MyColumn2 = 12;
  $stmt->execute();
  $Results = $stmt->get_result();
  $Result = $Results->fetch_assoc();
  if ($Result == Null)
  {
    //No result
    return;
  }
  $MyColumn4 = $Result['MyColumn4'];
  $Results->free();
  $stmt->close();
Get Each Row Returned
  $stmt = $maindb->prepare("SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'");
  $stmt->bind_param("si", $MyColumn1, $MyColumn2);
  $MyColumn1 = "abc";
  $MyColumn2 = 12;
  $stmt->execute();
  $Results = $stmt->get_result();
  while ($Result = $Results->fetch_assoc())
  {
    //NEXT RESULT
    $MyColumn4 = $Result['MyColumn4'];

  }
  $Results->free();
  $stmt->close();

Procedural Style

Does Record Exist
  $query = mysqli_query($dblink, "SELECT * FROM my_table WHERE some_field = $some_field AND some_field2 = $some_field2 AND some_field3 = 'yes'");
  if (mysqli_num_rows($query) < 1)
  {
Simple Get A Result
  $query = mysqli_query($dblink, "SELECT user_name, password FROM member_profile WHERE email_address = '$user_name_login' AND password = '$password_login'");
  if ($Result = mysqli_fetch_array($query, MYSQLI_ASSOC))
  {	
    $Result_display_username = $Result['user_name'];
    $Result_username = strtolower($Result['user_name']);
  }
Get Each Row Returned
  $query = mysqli_query($dblink, "SELECT * FROM my_table WHERE some_field = $some_field AND some_field2 = $some_field2 AND some_field3 = 'yes'");
  while ($Result = mysqli_fetch_array($query, MYSQLI_ASSOC))
  {
    //Get next row
    $MyVariable = $Result['some_field'];
    //Do something with it...
  }
Get Number Of Rows Returned
  $ResultsCount = mysqli_num_rows($query);
Get Specific Columns
  $query = mysqli_query($dblink, "SELECT first_name, last_name, image_file_name FROM member_profile WHERE member_id = $user_id");
Get each field as a variable named as the field
  foreach ( $Result as $key => $value )
  {
    $ResultString .= ",\"$key \": \"$value\"";
  }
Using OR Arguments
$sql = "SELECT * FROM videos WHERE response_id = $vid AND (video_approved ='yes' || approved ='group' || approved ='school') AND public_private = 'public'";
Putting All Results Into A Comma Seperated List
  $QueryString = "SELECT CONCAT_WS(',', MyColumnName1, MyColumnName2 ) FROM tblMyTable WHERE MyColumnName3 = 0";		//"CONCAT_WS(','," to cause results to be comma seperated for us
Putting All Results Into An Array
$all_results = array(); 		//Empty array to hold returned rows
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
  $all_results[] = $row;
}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.