amg182 Posted June 19, 2011 Share Posted June 19, 2011 Hi guys. Is it possible to use a WHERE clause that uses the page name or file name? I presume its a tag of some sort...? For example Select * FROM db_name WHERE Field_name='Page_title' I want to query my results using the page name or title, this would say me creating multiple queries. Any help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/ Share on other sites More sharing options...
redixx Posted June 19, 2011 Share Posted June 19, 2011 Sure, but it will only give you what you expect if all the title names are sure to be unique. Your example is the way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231745 Share on other sites More sharing options...
amg182 Posted June 19, 2011 Author Share Posted June 19, 2011 I thought so, but its not working for me.... $sql = "SELECT COUNT(*) FROM (SELECT * FROM `People` WHERE Name='page_title') subq"; I have tried this but not luck. any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231748 Share on other sites More sharing options...
redixx Posted June 19, 2011 Share Posted June 19, 2011 Is "page_title" supposed to be a variable? If so, you need to do it this way: $sql = "SELECT COUNT(*) FROM (SELECT * FROM `People` WHERE Name='" . $page_title . "') subq"; Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231755 Share on other sites More sharing options...
amg182 Posted June 19, 2011 Author Share Posted June 19, 2011 The page name is within my page <title></title> tags only-not a variable. Lets say I have a web page called John.php and I want to query all the people show names are John. I would use $sql = "SELECT COUNT(*) FROM (SELECT * FROM `People` WHERE Name='john') subq"; Which works just fine. But I would like to have the WHERE clause to change dynamically as the page name changes. Is this possible? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231766 Share on other sites More sharing options...
redixx Posted June 19, 2011 Share Posted June 19, 2011 Ah, I see. The easiest way would be just define a page_title variable and stick it in both the <title></title> and the query. The hardest way would be to use file_get_contents and scrape the HTML till you get the <title></title> text. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231770 Share on other sites More sharing options...
amg182 Posted June 19, 2011 Author Share Posted June 19, 2011 Hi, I'm so sorry but I have very limited experience with php.... and have no idea how to create a variable... How would i implement this to my code? <Title>John</title> <?php //connect to db $result = mysql_query("SELECT * FROM `students` WHERE Name='pagetitle'); <table> <tr> while($row = mysql_fetch_array($result)) { <td>' . $row['Name'] . '</td> <td> . $row['Surname] . '</td> <td>' . $row['Age'] . '</td> <td>' . $row[ASP'] . '</td> </tr> } </table> mysql_close($con); ?> Thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231783 Share on other sites More sharing options...
Xtremer360 Posted June 19, 2011 Share Posted June 19, 2011 I recommend a few things. You should either open up a good php book or looking at php.net lots of documentation. I recommend opening a general programming book as knowing how define a varible is programming 101. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231856 Share on other sites More sharing options...
Xtremer360 Posted June 19, 2011 Share Posted June 19, 2011 Here's a starting spot. <?php define("CONSTANT", "John."); ?> <Title><?php echo CONSTANT; ?></title> <?php //connect to db $result = mysql_query("SELECT * FROM `students` WHERE Name='CONSTANT'); echo '<table><tr>'; while($row = mysql_fetch_array($result)) { echo '<td>' . $row['Name'] . '</td>'; echo '<td>' . $row['Surname] . '</td>'; echo '<td>' . $row['Age'] . '</td>'; echo '<td>' . $row['ASP'] . '</td>'; } echo '</tr></table>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231859 Share on other sites More sharing options...
Xtremer360 Posted June 19, 2011 Share Posted June 19, 2011 I rushed though that use this instead. <?php define("CONSTANT", "John."); ?> <Title><?php echo CONSTANT; ?></title> <?php //connect to db $result = mysql_query("SELECT * FROM `students` WHERE Name=CONSTANT"); echo "<table><tr>"; while ($row = mysql_fetch_array($result)) { echo "<td>'" . $row['Name'] . "'</td>"; echo "<td>'" . $row['Surname'] . "'</td>"; echo "<td>'" . $row['Age'] . "'</td>"; echo "<td>'" . $row['ASP'] . "'</td>"; } echo '</tr></table>'; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231865 Share on other sites More sharing options...
amg182 Posted June 19, 2011 Author Share Posted June 19, 2011 Hi Coolascarlito. Thanks for reply, just had a go at the variable, and works perfectly. Yeah, I have purchased some O'Reilly books to learn more about PHP- hopefully get them this week! I have literally just started using php... Its quite daunting. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231905 Share on other sites More sharing options...
Xtremer360 Posted June 19, 2011 Share Posted June 19, 2011 Your welcome and I said all of that because when I first started out I just went in with it and didn't really know the basics of php or some stuff of programming in general and just tried messing with trial and error which is how a lot of noobs operate. And I've gained a some good friends off this web site that when I get in a crunch they are always there to explain to me a syntax issue or what not but books are there for a reason. But like I said glad to help you. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231923 Share on other sites More sharing options...
monkeytooth Posted June 19, 2011 Share Posted June 19, 2011 You want the core basics in a semi direct manor.. i picked up this site many years ago.. tizag.com they are basically a core basics sites. Which will give you the stepping stones to learn quickly. And they do it with visible examples. Over all not a bad site, they also have various references there from static HTML to mySQL, PHP, JavaScript.. etc.. I would say before continuing on you at the least study that site and its references. I mean this with no disrespect. But I also say it with a firm guiding tone :-) Trust me, knowing at the least the basics before you come on to a forum like this will save you a lot of frustration. As some people can be brutal savages more so cause they don't remember the years they struggled through. Also being as descript as you can without dropping 200+ lines of code with only "This is broken how do I fix it" as the description (*cough* carlito... *cough, cough*) is helpful too. One thing I also want to mention is the books you ordered, and sites you are going to come up with in general til you start getting more familiar with the code your intent on using aren't likely to mention the concept of code injection tizag I know covers it lightly but. If your developing a site where a user can change any variable through the browser its worth reading up on "XSS Attacks" and "Injection Prevention" it will save you a lot of headache in the future to implement good security now Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1231929 Share on other sites More sharing options...
amg182 Posted June 20, 2011 Author Share Posted June 20, 2011 Thanks monkeytooth, noted! Appreciate your time guys. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1232417 Share on other sites More sharing options...
Xtremer360 Posted June 20, 2011 Share Posted June 20, 2011 On a side note I would here and there just look around for other active threads and try and pick up on a some other things that others do but don't copy their code. The thought is to read the code and UNDERSTAND it and by understand it I mean understand why that line(s) of code is written that way and what it does exactly. Plus you can pick up on others mistakes in their code and understand why its wrong so you don't make the same mistakes. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1232421 Share on other sites More sharing options...
monkeytooth Posted June 20, 2011 Share Posted June 20, 2011 Valid point carlito.. But its also worth mentioning sometimes the way other code is done is the best possible way to achieve a goal. Hundreds of thousands of coders in the world, and only so many ways to write code to work. Thats why you can't copyright code for the most part, you can copy right the concept of what the code does and what it was built for but the code itself not really cause if someone hasn't yet come up with a concept similar to anything anyone has done, someday sometime in the future someone will wanna do something entirely different that will look act and feel like what youve done :-) but reading through and trying to understand what your pillaging is always a good thing to do.. Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1232425 Share on other sites More sharing options...
amg182 Posted June 20, 2011 Author Share Posted June 20, 2011 Will do! Quote Link to comment https://forums.phpfreaks.com/topic/239791-query-by-page-title/#findComment-1232490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.