Re: Displaying a single cell
am 17.08.2007 01:28:43 von luiheidsgoeroe
On Thu, 16 Aug 2007 23:56:01 +0200, Allan Drake
> =
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 name=
> (grabbed from URL)
>
>
>
> //Connection Data is in the header I think
> include_once("head.php");
>
> //Grabs the name from the URL and I know it is working
> $names =3D $_REQUEST["name"];
>
> // set up query?
> $sql =3D "select * from db_members where idmembers =3D $names";
>
> //performs query I think?
> $result =3D MYSQL_QUERY($sql);
>
> //trying to grab his email from the email column
> $array =3D mysql_fetch_array($result);
> $mymail =3D $array[email];
> ?>
>
> My e-mail is: =3D$mymail?> <-----------This comes up blank
>
> Result is working as =3D$result?> <------This comes up=
> as blank
>
> If you could point me in the right direction as to where my code is
> going wrong, and how to correct it that would be great.
As long as the table db_members exists and has a field 'email', every SQ=
L =
question is anwered, and this is a PHP issue (funny, usually the other w=
ay =
around...)
Crossposted to comp.lang.php & f'upped there, care to follow me there? =
Lots & lots of comments to follow :-)
-- =
Rik Wasmus
Re: Displaying a single cell
am 17.08.2007 01:42:26 von luiheidsgoeroe
On Fri, 17 Aug 2007 01:28:43 +0200, Rik wro=
te:
> On Thu, 16 Aug 2007 23:56:01 +0200, Allan Drake =
> 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.
>>
>> //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()==0) 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
ld =
be trouble later on, or maybe even now. It'better to use the full syntax=
:
My e-mail is:
>> 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
Re: Displaying a single cell
am 17.08.2007 01:43:57 von luiheidsgoeroe
On Fri, 17 Aug 2007 01:42:26 +0200, Rik wrote:
> Could you at least give us a snipper?
snippet....
--
Rik Wasmus