Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP best approach where to store MySQL queries when using MySQLi
    primarykey
    data
    text
    <p>I am new to <strong>MySQLi</strong> and I am not an expert at PHP. I am now moving over to use <strong>MySQLi</strong> instead of the <strong>MySQL</strong> function in PHP.</p> <p>I would appreciate a little help in understanding what is the <strong>best approach</strong>, yet easy to handle, when I code my PHP scripts and need to query my MySQL database when using <strong>MySQLi</strong>.</p> <p>When using <strong>MySQL</strong> function in PHP I often did it this way:</p> <p><strong>PHP Script</strong></p> <pre><code>// My PHP Script include("includes/dbc.php"); // Include script with the database connection string </code></pre> <p>The <strong>dbc.php</strong> script would look like:</p> <pre><code>&lt;?php $mySqlServer = "localhost"; $mySqlUser = "username"; $mySqlpwd = "password"; $mySqlDB = "database"; if ( !mysql_connect($mySqlServer, $mySqlUser, $mySqlpwd) ) { $_SESSION["dberror"] = "Not able to connect to database SERVER."; } if ( !mysql_select_db( $mySqlDB ) ) { $_SESSION["dberror"] = "Not able to select database."; } </code></pre> <p>Then in the scripts where I would <strong>query</strong> and <strong>echo</strong> results I actually called a <strong>function</strong> which I stored in a <strong>func.php</strong> script.</p> <p><strong>func.php</strong> script:</p> <pre><code>function getPeople($id) { $sql = "SELECT * FROM table WHERE hid = '$id'"; $sqlResult = mysql_query($sql) or die(mysql_error()); return $sqlResult; } </code></pre> <p>In the script that echoed the data it could look like:</p> <pre><code>$data = getPeople($id); while ($row = mysql_fetch_assoc($data)) { echo "Firstname: " . $row['firstname']; } </code></pre> <p><strong>Would the above be the same structure although I would use <em>MySQLi</em> now?</strong></p> <p><strong>This is how I now have edited my PHP script files with <em>MySQLi</em> instead.</strong></p> <p>The <strong>dbc.php</strong> now contains</p> <pre><code>// Create the object $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName); // Check if able to connect to database if ($conn-&gt;connect_error) { trigger_error('Database connection failed: ' . $conn-&gt;connect_error, E_USER_ERROR); } </code></pre> <p>The *<em>function.php</em> file contains</p> <pre><code>function getFewEventDetails() { $sql = "SELECT * FROM iboevents WHERE visible = '1' LIMIT 1"; $rs = $conn-&gt;query($sql); if($rs === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn-&gt;error, E_USER_ERROR); } else { $returned_rows = $rs-&gt;num_rows; return $returned_rows; } } </code></pre> <p>But when I call this function to produce the results in another script file, nothing happens.</p>
    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.
 

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