Note that there are some explanatory texts on larger screens.

plurals
  1. POcustom function for mysqli queries
    text
    copied!<p>I'm trying my hand at custom functions in PHP in order to streamline a lot of stuff I'm otherwise doing manually. I'm damn new to custom functions so I'm not sure the limitations. Right now I'm trying to get data with MySQLi using custom functions Here's the code, and then I'll explain the issue:</p> <pre><code>function connect_db($db = 'db_username') { iconv_set_encoding("internal_encoding", "UTF-8"); mb_language('uni'); mb_internal_encoding('UTF-8'); @ $mysqli = new mysqli('host',$db,'password',$db); if(mysqli_connect_errno()) { die('connection error'); } } </code></pre> <p>This one seems to be working fine. It's the next function I'm having more trouble with.</p> <p><strong>edit:</strong> Updated thanks to Jeremy1026's response</p> <pre><code>function do_query($db = 'default_db', $query) { connect_db(); $result = $mysqli-&gt;query($query); if(!$result){ trigger_error("data selection error"); } while($row = $result-&gt;fetch_assoc()){ $result_array[] = $row; } return $result_array; } </code></pre> <p>My host forces database names and usernames as the same, so if the db name is 'bob' the username to access it will be 'bob' as well, so that's why <code>$db</code> shows up twice in the connection.</p> <p>The problem I'm having is that these two functions are in <code>functions.php</code> and being called from another page. I want to be able to pull the results from the query in that other page based on the column name. But I also need to be able to do this with formatting, so then maybe the <code>while()</code> loop has to happen on that page and not in a function? I want this to be as universal as possible, regardless of the page or the data, so that I can use these two functions for all connections and all queries of the three databases I'm running for the site. </p> <p>God I hope I'm being clear.</p> <p>Big thanks in advance for any suggestions. I've googled this a bit but it's tough to find anything that's not using obsolescent <code>mysql_</code> prefixes or anything that's actually returning the data in a way that I can use.</p> <p><strong>Update:</strong> I'm now getting the following error when accessing the page in question:</p> <p><strong>Fatal error:</strong> Call to a member function query() on a non-object in <strong>/functions.php</strong>`</p> <p>with the line in question being <code>$result = $mysqli-&gt;query($query);</code>. I assume that's because it thinks <code>$query</code> is undefined, but shouldn't it be getting the definition from being called in the page? This is that page's code:</p> <pre><code>$query = "SELECT * FROM `table`"; $myArray = do_query($db, $query); echo $myArray['column_name']; </code></pre>
 

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