{"id":440,"date":"2012-07-17T20:09:20","date_gmt":"2012-07-17T20:09:20","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=440"},"modified":"2026-06-17T09:22:27","modified_gmt":"2026-06-17T08:22:27","slug":"select","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/queries\/select\/select","title":{"rendered":".SELECT General"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Object Oriented Style<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Does Record Exist<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'\");\n  $stmt->bind_param(\"si\", $MyColumn1, $MyColumn2);\n  $MyColumn1 = \"abc\";\n  $MyColumn2 = 12;\n  $stmt->execute();\n  $stmt->store_result();\n  if ($stmt->num_rows &lt;= 0)\n  {\n\n  }\n  $stmt->close();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Simple Get A Result<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'\");\n  $stmt->bind_param(\"si\", $MyColumn1, $MyColumn2);\n  $MyColumn1 = \"abc\";\n  $MyColumn2 = 12;\n  $stmt->execute();\n  $Results = $stmt->get_result();\n  if ($Result = $Results->fetch_assoc())\n  {\n    $MyColumn4 = $Result&#91;'MyColumn4'];\n\n  }\n  $Results->free();\n  $stmt->close();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Alternative with check for no result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'\");\n  $stmt->bind_param(\"si\", $MyColumn1, $MyColumn2);\n  $MyColumn1 = \"abc\";\n  $MyColumn2 = 12;\n  $stmt->execute();\n  $Results = $stmt->get_result();\n  $Result = $Results->fetch_assoc();\n  if ($Result == Null)\n  {\n    \/\/No result\n    return;\n  }\n  $MyColumn4 = $Result&#91;'MyColumn4'];\n  $Results->free();\n  $stmt->close();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Each Row Returned<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"SELECT * FROM my_table WHERE MyColumn1 = ? AND MyColumn2 = ? AND MyColumn3 = 'yes'\");\n  $stmt->bind_param(\"si\", $MyColumn1, $MyColumn2);\n  $MyColumn1 = \"abc\";\n  $MyColumn2 = 12;\n  $stmt->execute();\n  $Results = $stmt->get_result();\n  while ($Result = $Results->fetch_assoc())\n  {\n    \/\/NEXT RESULT\n    $MyColumn4 = $Result&#91;'MyColumn4'];\n\n  }\n  $Results->free();\n  $stmt->close();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Procedural&nbsp;Style<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Does Record Exist<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $query = mysqli_query($dblink, \"SELECT * FROM my_table WHERE some_field = $some_field AND some_field2 = $some_field2 AND some_field3 = 'yes'\");\n  if (mysqli_num_rows($query) &lt; 1)\n  {<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Simple Get A Result<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $query = mysqli_query($dblink, \"SELECT user_name, password FROM member_profile WHERE email_address = '$user_name_login' AND password = '$password_login'\");\n  if ($Result = mysqli_fetch_array($query, MYSQLI_ASSOC))\n  {\t\n    $Result_display_username = $Result&#91;'user_name'];\n    $Result_username = strtolower($Result&#91;'user_name']);\n  }<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Each Row Returned<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $query = mysqli_query($dblink, \"SELECT * FROM my_table WHERE some_field = $some_field AND some_field2 = $some_field2 AND some_field3 = 'yes'\");\n  while ($Result = mysqli_fetch_array($query, MYSQLI_ASSOC))\n  {\n    \/\/Get next row\n    $MyVariable = $Result&#91;'some_field'];\n    \/\/Do something with it...\n  }<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Number Of Rows Returned<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ResultsCount = mysqli_num_rows($query);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Specific Columns<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $query = mysqli_query($dblink, \"SELECT first_name, last_name, image_file_name FROM member_profile WHERE member_id = $user_id\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get each field as a variable named as the field<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  foreach ( $Result as $key => $value )\n  {\n    $ResultString .= \",\\\"$key \\\": \\\"$value\\\"\";\n  }<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using OR Arguments<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT * FROM videos WHERE response_id = $vid AND (video_approved ='yes' || approved ='group' || approved ='school') AND public_private = 'public'\";<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Putting All Results Into A Comma Seperated List<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $QueryString = \"SELECT CONCAT_WS(',', MyColumnName1, MyColumnName2 ) FROM tblMyTable WHERE MyColumnName3 = 0\";\t\t\/\/\"CONCAT_WS(',',\" to cause results to be comma seperated for us<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Putting All Results Into An Array<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$all_results = array(); \t\t\/\/Empty array to hold returned rows\nwhile ($row = mysqli_fetch_array($query, MYSQLI_ASSOC))\n{\n  $all_results&#91;] = $row;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Style Does Record Exist Simple Get A Result Alternative with check for no result Get Each Row Returned Procedural&nbsp;Style Does Record Exist Simple Get A Result Get Each Row Returned Get Number Of Rows Returned Get Specific Columns Get each field as a variable named as the field Using OR Arguments Putting All [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","hentry","category-select"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":51,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":3124,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/440\/revisions\/3124"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}