Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Probably A Simple Answer

Codes here !

Moderators: macek, egami, gesf

Probably A Simple Answer

Postby kingk » Thu Oct 17, 2002 2:05 pm

I am a complete newbie to PHP and MySQL but I am trying to learn. I am trying to figure out how to keep all of the info in my table from displaying. Here is my example:
Here is the link I would give:

http://www.hightidemedia.net/v7/clientid.php?id=obh

But if i just type in
http://www.hightidemedia.net/v7/clientid.php
It lists all of the info in the table. How do I keep this from happening?

Here is the code I use to query the database:

<?php

mysql_connect (localhost, ******, ********);

mysql_select_db (******);

if ($id == "")
{$id = '%';}


$result = mysql_query ("SELECT * FROM client
WHERE id LIKE '%$id%'
");

if ($row = mysql_fetch_array($result)) {

do {
Print $row["company"];
print (" ");
print ("<br>");
print ("<p>");
print $row["pic"];
print ("<p>");
print $row["url"];
print ("<br>");

} while($row = mysql_fetch_array($result));
} else {print "Oops! Either I made a mistake, or you did!";}


?>
kingk
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Oct 17, 2002 1:55 pm

Re: Probably A Simple Answer

Postby DoppyNL » Thu Oct 17, 2002 10:37 pm

Code: Select all
if ($id == "")
{$id = '%';}

You find out here that id is not supplied to the script.
You give it a value so that any value in the database will be returned.
You could also give it some default value or give an error message and exit you're script.

Greetz Daan
DoppyNL
 

i still dont understand...

Postby kingk » Fri Oct 18, 2002 2:34 pm

ID is one of the colomns in my table. So what can I add to that statement so that if nothing is put in as the "ID" it will display nothing?

I want http://www.hightidemedia.net/v7/clientid.php to be blank.
kingk
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Oct 17, 2002 1:55 pm

Postby DoppyNL » Sat Oct 19, 2002 12:24 am

LIKE "%" will allow ANY value, so if ID is not present it will allow any value.
You could check if ID is supplied to you're script and if it is not remove the %%.
LIKE is used for string-comparison.

Also, if you're trying to select a specific item you could better use:
Code: Select all
where id = 12

, or when you want a range:
Code: Select all
where id > 12 and id <40

It will execute faster.
The speed will not really be noticed on small tables, but the moment they get much bigger you'll notice the difference.

Greetz Daan
DoppyNL
 


Return to mySQL & php coding

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.