|
Posted by Rik on 08/16/07 23:42
On Fri, 17 Aug 2007 01:28:43 +0200, Rik <luiheidsgoeroe@hotmail.com> wro=
te:
> On Thu, 16 Aug 2007 23:56:01 +0200, Allan Drake =
> <supercool@chartermi.net> wrote:
>
>> I am a beginning programmer, and I am trying to display a single cell=
>> of data from my database. This php script is for displaying profile
>> information, specifically an e-mail in the same row as the member nam=
e
>> (grabbed from URL)
Ah, welcome to the right ng, let's start.
>> <?php
>> //Connection Data is in the header I think
>> include_once("head.php");
You think? Could you at least give us a snipper? It should contain a =
mysql_connect() and optionally a mysql_select_db() statement.
>> //Grabs the name from the URL and I know it is working
>> $names =3D $_REQUEST["name"];
If it's from the url, it's better to use $_GET['name']. Defining where =
your data comes from will save you headaches later on.
>> // set up query?
>> $sql =3D "select * from db_members where idmembers =3D $names";
Sounds like a basic OK query, only forgot the quotes, and it's vulnerabl=
e =
to SQL injection (google it). Also, peeping ahead you don't need =
'everything' you need 'email', from just one row, let's do this:
//prevent SQL injection
$names =3D mysql_real_escape_string($names);
//create query
$sql =3D "SELECT email FROM db_members WHERE idmembers =3D '$names' LIMI=
T 1";
>> //performs query I think?
>> $result =3D MYSQL_QUERY($sql);
Yup. There might be an error though. In developing you can use =
mysql_error() to check it, change it to somewhat more userfriendly on a =
=
live server:
//check wether everything went OK:
$result =3D mysql_query($sql) or die('MySQL error in query:'.mysql_error=
());
//lets check wether we have a match:
if(mysql_num_rows()=3D=3D0) die('No result found in database.');
>> //trying to grab his email from the email column
>> $array =3D mysql_fetch_array($result);
>> $mymail =3D $array[email];
Will work, however, if you only plan to use the associative array (resul=
ts =
are grabbed both associative & numerical in this one), this is slightly =
=
better:
$array =3D mysql_fetch_assoc($result);
//don't forget the quotes around 'email'!!
$mymail =3D $array['email'];
>> ?>
>>
>> My e-mail is: <?=3D$mymail?> <-----------This comes up blank
This relies on short_open_tags (not opened by the full <?php), which cou=
ld =
be trouble later on, or maybe even now. It'better to use the full syntax=
:
My e-mail is: <?php echo $mymail; ?>
>> Result is working as <?=3D$result?> <------This comes u=
p
>> as blank
Aside from the short_open_tags here, you shouldn't try to echo a resourc=
e =
($result), which will give you no further information about what data it=
=
holds.
I've rushed my comments a bit as I directed you to this ng, so if =
something is unclear just ask.
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|