Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>$q = "SELECT *"; $q.= " FROM yourtablename"; $q.= " WHERE `page-id` = ".(int)$page_id; $q.= " ORDER BY `link-id`"; </code></pre> <p>The backticks might not be necessary, but I'm throwing them in there since you have hyphens in your field names.</p> <p>Note that there is very basic validation going on here. By casting <code>$page_id</code> to an int before appending it, you ensure it won't be some sort of injection attack. This isn't a great way to do it, but it will work.</p> <p>Something like <code>mysqli_real_escape_string()</code> is an alternative that should be considered, especially for more general sanitization.</p> <p>Alternatively:</p> <pre><code>$q = sprintf("SELECT * FROM yourtablename WHERE `page-id` = %d ORDER BY `link-id", $page_id); </code></pre> <p>Which I like better.</p> <hr> <p><strong>Edit</strong> re: now what?</p> <p>First off, lets not use mysqli, lets use <a href="http://php.net/manual/en/book.pdo.php" rel="nofollow">PDO</a>.</p> <p>Second, we don't want to connect to the database on each call of the function, we want to do this once. So move this out of the function.</p> <pre><code>// Typically this line is in another file and included once, but for now lets just // get this out of the function $db = new PDO('mysql:host=localhost;dbname=my_db', 'root', 'root'); // Your function function get_page_links($page_id) { // Build query $q = sprintf("SELECT * FROM yourtablename WHERE `page-id` = %d ORDER BY `link-id`", $page_id); // Run Query foreach ($db-&gt;query($q) as $a) { printf('&lt;a href="%s"&gt;%s&lt;/a&gt;'."\n", $a['url'], $a['anchor-text']); } } </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