Jump to content

Show table info on page


Dantesean

Recommended Posts

Hi, okee so I'm new to everything that has to do with scripting, especially when it comes to PHP and stuff,

but what is the best way to display information from a table on a page?

I'm creating this site where users can post their events, but I want the Location to be based on the Location they inserted during the registration process, so that the location will be displayed directly.

How am I able to do that? I've already got an login script, it looks like this:

<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="whats_happening_db"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:membersarea.php?user=$myusername");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

Sorry if my English is terrible haha.

 

Dante.

Link to comment
Share on other sites

Hi DanteseanTopic, welcome to the forms.

 

Some general comments:

I do not see any reason to use ob_start(), I wouldn't.

Since you have to open your database on every php page I find it easier to make a file to do this and place that file in a folder called inc.

So this goes into a file:

 

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="whats_happening_db"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

and you have this at the top of your php:

require ('./inc/DB_connect.php');

 

To see if the user is regestered on your site can be done a number of ways. I just select the password where user = myusername then compare the passwords. If good then he's in.

 

session_register() is deprecated. Better to use $_SESSIONS.

http://www.php.net/manual/en/function.session-start.php

 

And Lastly: I see no reason to have a users password so available I would just store the user name in sessions. $_SESSION["user"] = $myusername;

 

Now to answer your question.

 

This just echoes the location but  you can insert <?php echo $location; ?> in your html potion of the code.

 

<?php
session_start();  // must be first thing
require ('./inc/Book_connect.php');
$name = $_SESSION["user"];

$query = "SELECT location FROM members WHERE username='$name'";
$results = mysql_query($query);
if($results)
{
$row = mysql_fetch_row($results);
}
$location = $row[0];
echo $location;
?>

 

 

Link to comment
Share on other sites

well thanks, everything worked!

Also thanks for the simplified code for the connect to DB script haha!

 

Now another question, If I want to display the location in a HTML file, what is the best way to do this?

Right now I'm using iframe:

<Iframe src="display_location.php" width="200" height="20"></Iframe>

and it works, but it will limit the size and it displays a ugly block around the text.

 

Dante.

Link to comment
Share on other sites

never mind, got it!

other question,

I got this system where people can post info about an event in to the database,

but how can I display that info?

I know how to display 1 piece of info, but wait, I'll sketch my situation:

User comes to the site, want's to see which events are in "city" (For instance "Heerlen")

how can I display this, so it will display multiple names?

 

Dante.

(CODE:

<?php
$query = "SELECT uitgaansgelegenheid FROM events WHERE city='Heerlen'";
$results = mysql_query($query);
if($results)
{
$row = mysql_fetch_row($results);
}
$uitgaansgelegenheden = $row[0];
?>

)

Link to comment
Share on other sites

You might want to display your information in a table or some other way. this will just output lines:

<?php
$query = "SELECT uitgaansgelegenheid FROM events WHERE city='Heerlen'";
$results = mysql_query($query);
if($results)
{
while ($row = mysql_fetch_row($results))
{
	for($i = 0; $i < count($row); $i++)
	{
		echo $row[$i].'<br>';   //  You might want to display this in a table or some other way
	}
}
}
?>

 

To do a table:

<?php
$query = "SELECT uitgaansgelegenheid FROM events WHERE city='Heerlen'";
$result = mysql_query($query);
$colCount = mysql_num_fields($result);

echo "<br /><TABLE BORDER=1>uitgaansgelegenheid<br />";

while($row = mysql_fetch_row($result))
{
echo "<tr align=left VALIGN=TOP>";
for($colNumber = 0; $colNumber < $colCount; $colNumber++)
echo "<td>$row[$colNumber]</td>";
echo "</tr>";
}
echo "</table>";
?>

I do not know if this works 100% but it should give you a start

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.