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;
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.