-1

I am pretty new to PHP so I apologise if this seems like a dumb question. I am using PHP code with a MYSQL database.

I basically have the code below which has an HTML menu with its link being generated by a PHP variable. This is for the home page(this file ends in "pg1"). I want the file to check in my MYSQL database if there is a document with the same name but ending in "pg2". I am creating two variables one with pg1($fpname1) at the end and another with pg2($fpname2). How do I then have it look in my database for a table with a name = $fpname2? I know that then if it finds the table I can put a "else" and then the html which I want to include in the menu link.

This is the relevant code so far:

$fpname1 = $web_user."ID".$web_number."webID_"."pg1".".php";
$fpname2 = $web_user."ID".$web_number."webID_"."pg2".".php";

?>
<div class="menu">
<ul>
<li><a href="<?php echo $fpname1; ?>">Home</a></li>
</ul>
</div>

thank you for any help

0

5 Answers 5

0

SELECT * FROM table WHERE page LIKE '%variable%'

Then you would check the amount of rows the query returned, if that number is greater than 0 at least 1 page was returned, else it was never found.

0
SELECT * FROM table WHERE page LIKE '%pg_.php'

this will select all pages ranging between pg0 to pg9

0

you have a information_schema database I presume. In that there is a table called TABLE. in that all the table_names are kept. so if u give a query in database information_schema: SELECT table_schema FROM TABLES where table_name = example if the table_schema is null then there is no table otherwise there is the database the table is in.

0

Looks like your database setup is wrong.
You have to store all your pages in one table, not many.

You are suffering from the SQL injection as a direct consequence. As well as overcomplicated and unmaintainable code.

-1

You have to use the mysql_num_rows function.

$fetch = mysql_query("SELECT * FROM example") or die(mysql_error());
$count = mysql_num_rows($fetch);

http://www.tizag.com/mysqlTutorial/index.php

2
  • 1
    Just a heads up, you don't pass $count to the mysql_num_rows function, you pass the query result, so $fetch in this case. See (the doc)[php.net/manual/en/function.mysql-num-rows.php]
    – jedwards
    Apr 28, 2012 at 21:09
  • I want the file to check in my MYSQL database if there is a document with the same name but ending in "pg2". He doesn't want to check a file. He wants to check whether a database row exists. He's new to php so he's mixing up the terminology. So therefore, I gave him code to check how much rows a query matches as a count, so he can check whether the "file" exists, by reading the variable of $count. So no, I am actually right, giving a right answer.
    – desbest
    Apr 29, 2012 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.