Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks like a rather simple query for me</p> <pre><code>SELECT * FROM `links_table` WHERE `page_id` = $page_id ORDER BY `link_id` ASC; </code></pre> <p>This of course is assuming <code>$page_id</code> does not come from the user input, and is safely sanitzied. If not, you should be using prepared statements.</p> <p>Read about <strong><a href="http://il.php.net/manual/en/mysqli.quickstart.statements.php" rel="nofollow">Executing Statements on MySQLi</a></strong> to learn how to deal with the results this query generates.</p> <hr> <p>I'm not proficient at MySQLi, as I prefer PDO. In PDO, I would have done something like so:</p> <pre><code>&lt;?php /** * @param integer $page_id * @param PDO $db * * @return array */ /* * First, we give the function the database connection object as an argument. * A function to get the page links shouldn't care where you got the connection from! */ function get_page_links($page_id, PDO $db) { $query = &lt;&lt;&lt;MySQL SELECT * FROM `links_table` WHERE `page_id` = :page_id ORDER BY `link_id` ASC; MySQL; /* * Prepare the query and bind all values to placeholders */ $stmt = $db-&gt;prepare($query); $stmt-&gt;bindValue(":page_id", $page_id, PDO::PARAM_INT); $stmt-&gt;execute(); /* * Fetch all results to an array, and return it */ $result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); return $result; } try { /* * Start new PDO connection for the function */ $db = new PDO("mysql:host=localhost;dbname=database_name", "user", "password"); //This line tells PDO to throw PDOExceptions in case of errors, //which are much easier to handle $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //This lines disables PDO's default emulation for prepared statements. Adds security. $db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $links = get_page_links(1, $db); } catch (PDOException $e) { die("There has been an error with the database: " . $e-&gt;getMessage()); } foreach ($links as $link) { echo "&lt;a href='{$link["url"]}'&gt;{$link["anchor_text"]}&lt;/a&gt;"; } </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