Bradley Cousins Bradley Cousins - 1 year ago 67
PHP Question

PHP URL Encoding name and storing it in URL, retrieving the name and searching in MYSQLi

I have a php page which outputs all of the pages created (located in the database) each page links to a new page which will contain all of the content for that page.

I want to parse the name of the page into the URL so i can retrieve it and search through the database to find all of the records using that name within the database, I have been able to do this by parsing the rows ID however i want to do it with the pages name.

This is my code outputting all of the page links and storing the value of 'name' in the URL :

$res = $conn->query("SELECT * FROM pages ORDER BY id ASC");

echo '<a href="page.php?name=' . urlencode($row['name']) . '">' . ucfirst($row['name']) . '</a>' . "<br>";


Here is the page where i want to retreive the value of 'name' and search through the database for the record containing 'name' which is now stored in the $page variable

include_once 'db.php';

$page = urldecode($_GET['name']);

echo $page;


$res = $conn->query("SELECT * FROM pages WHERE name = $page");

When i echo the $page variable, it does show the correct name, so i dont know why i am receiving this error :

Fatal error: Uncaught Error: Call to a member function fetch_array() on boolean in /Applications/XAMPP/xamppfiles/htdocs/newpage/page.php:12 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/newpage/page.php on line 12

Thankyou for any help

Answer Source

Error means that you have error in query. Probably because of lack ' Strings in sql must be QUOTED like this:

$conn->query("SELECT * FROM pages WHERE name = '$page'");

ps: you should also escape values which you put into query. Read about sql injections

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download