Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to pass the mysqli as a resource. Use it as an object and invoke it's query method like this:</p> <pre><code>$mysqli = new mysqli("localhost", "user", "pass", "db"); $sql = "SELECT OrgNo, CompanyName FROM ematch WHERE CompanyName LIKE '%A%'"; $result = $mysqli-&gt;query($sql); while (($row=$result-&gt;fetch_assoc())!=false) { print $row['OrgNo'] .'&lt;br /&gt;'; print $row['CompanyName'] .'&lt;br /&gt;'; } </code></pre> <p>$result in this example is a mysqli_resultset object. There are many functions to give you these results in various formats like objects or numeric/associative arrays. check the PHP manual for all the choices. If you need to use this routine often, you can derive a class from the mysqli_result base and make a member function out of your code like:</p> <pre><code>class MyResult extends mysqli_result { public function company_info($name=null) { if ($name==null) { while (($row=$result-&gt;fetch_assoc())!=false) { print $row['OrgNo'] .'&lt;br /&gt;'; print $row['CompanyName'] .'&lt;br /&gt;'; } } else { while (($row=$result-&gt;fetch_assoc())!=false) { $result-&gt;field_seek(1); if($result-&gt;fetch_field==$name) { print $row['OrgNo'] .'&lt;br /&gt;'; print $row['CompanyName'] .'&lt;br /&gt;'; } } } } } </code></pre> <p>I threw in some extra code to give you a better feel for the mysqli functions. My little routine moves the cursor to the second field in the set and compares it to the function's name parameter.The mysqli class has many different functions just like the mysqli_resultset. Don't think of the result as a resource. It is member data in an object and is accessed through it's methods. Remember, load in your object data then manipulate it through it's member functions. If you need a function that isn't predefined, just derive and add it to the class. Just Dont use mysql at all. it is really obsolete and probably should be deprecated in favor of the true OO style in the "improved" version. The i in mysqli stands for improved. It makes use of the newer PHP 5 object model. When you get used to the OO syntax you'll not want to revert to the old procedural style. Access the derived class like this:</p> <pre><code> $i_am_an_object = new MyResult; //Make The result a MyResult instead of just a mysqli_result $i_am_an_object = $mysqli-&gt;query($sql); //populate the MyResult object with a base class function $i_am_an_object-&gt;company_info(); //call your function to print fields from each record in the result\ $i_am_an_object-&gt;company_info("xyz Company"); //checks the second field in each record for the $name parameter and prints fields for any matching records. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload