Forum Moderators: coopster

Message Too Old, No Replies

is it possible? how?

please help me

         

abeerbd

7:28 am on Jun 12, 2011 (gmt 0)

10+ Year Member



Hi there i am very new here as well as in php

please help me regarding below issue

i use below code to display data from mysql row from specific id. here is id=1

<?php 
mysql_connect("localhost", "dhaka", "dhaka") or die("Connection Failed");
mysql_select_db("dhaka")or die("Connection Failed");
$result = mysql_query("SELECT *FROM page WHERE id='1'")
or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "content: ".$row['dtl'];

?>


now i included this code by using php include function in one page called " johns page.php"

so when i click the "john page" the page come with the pulling from id=1.

but if i have 30 page like " kate page" ," roberts page", "michale page"......

then i have to include the above code 30 time by editing it manusally have to change the id number.

for "kate page" i have to change manually the line
$result = mysql_query("SELECT *FROM page WHERE id='1'")


to

$result = mysql_query("SELECT *FROM page WHERE id='2'")

and include the code to " kate page.php"

which is horrible expreince for multiple page...

is it possible that the id number will automatically change when i click on different page?

please help me

aakk9999

2:57 pm on Jun 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change the following statement so that id is taken from a variable (for example, $userId)

$result = mysql_query("SELECT * FROM page WHERE id='$userId'")

Then before php include, set this variable to appropriate id, depending on which page you are, i.e. depending on whose ID you want:

So on John's page you would have:
<?php
// set ID for John
$userId = '1';
include 'getuser.php';
?>

And on Kate's page you would have:
<?php
// set ID for Kate
$userId = '2';
include 'getuser.php';
?>

[edited by: aakk9999 at 3:04 pm (utc) on Jun 12, 2011]

rainborick

2:57 pm on Jun 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There should be a space between "*" and "FROM" in your SQL query string. To have your code work for any page, you could use something like:

$result = mysql_query("SELECT * FROM page WHERE id='$idValue'")
or die(mysql_error());

Just replace $idValue with the appropriate variable in your code. Good luck!

g1smd

4:45 pm on Jun 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure that the code that reads from the database is placed BEFORE the DOCTYPE declaration so that it can instead return an error message if there is no content in the database for this query.

abeerbd

3:16 am on Jun 13, 2011 (gmt 0)

10+ Year Member



ow. thanks for the quick reply...

then for 100 page i have to copy the

<?php
// set ID for John
$userId = '1';
include 'getuser.php';
?>

the above code 100 times.

but what i want is... only the the i number will change when user click to the different button. id number can get by the url...

jspeed

2:31 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



$id=$_GET['id'];

This will GET the id from the URL, and store it in the $id variable.

In the link, you query the single php page with a question mark:

<a href="user.php?id=1">link</a>

And like aakk9999 or rainborick suggested, change your MySQL to accept the variable:

$result = mysql_query("SELECT * FROM page WHERE id=$id")

Make sure your $id variable only allows numeric characters:

if (!is_numeric($id)){return false;}

(the numeric bit I learned from httpwebwitch)

jspeed

3:12 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



As for generating the links, you could run a loop to generate them, instead of manually entering the id in the link:

$query="SELECT * FROM `page` WHERE `id` = $id" or die(mysql_error());
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id=$row["id"];
$name=$row["name"];
echo "<a href=\"user.php?id=$id\">$name</a><br />";
}

That is, assuming your table has a field "name". You get the idea.