Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9232 > unrolled thread

fetch items from a row

Started byrichard <noreply@example.com>
First post2013-05-03 15:55 -0400
Last post2013-08-05 00:19 +0000
Articles 18 — 11 participants

Back to article view | Back to comp.lang.php


Contents

  fetch items from a row richard <noreply@example.com> - 2013-05-03 15:55 -0400
    Re: fetch items from a row Salvatore <sal@yojimbo.hack.invalid> - 2013-05-03 20:18 +0000
      Re: fetch items from a row Tim Streater <timstreater@greenbee.net> - 2013-05-03 21:49 +0100
        Re: fetch items from a row Mladen Gogala <gogala.mladen@gmail.com> - 2013-08-04 17:06 +0000
          Re: fetch items from a row Tim Streater <timstreater@greenbee.net> - 2013-08-04 18:41 +0100
          Re: fetch items from a row The Natural Philosopher <tnp@invalid.invalid> - 2013-08-04 22:27 +0100
            Re: fetch items from a row Tim Streater <timstreater@greenbee.net> - 2013-08-04 23:15 +0100
            Re: fetch items from a row Jerry Stuckle <jstucklex@attglobal.net> - 2013-08-04 22:09 -0400
    Re: fetch items from a row Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-03 20:47 +0000
    Re: fetch items from a row Richard Yates <richard@yatesguitar.com> - 2013-05-03 14:00 -0700
      Re: fetch items from a row Scott Johnson <noonehome@chalupasworld.com> - 2013-05-03 14:21 -0700
      Re: fetch items from a row Lew Pitcher <lpitcher@teksavvy.com> - 2013-05-03 17:38 -0400
        Re: fetch items from a row Richard Yates <richard@yatesguitar.com> - 2013-05-03 15:15 -0700
          Re: fetch items from a row SwissCheese <swisscheese@cfl.rr.com> - 2013-05-04 05:56 -0400
            Re: fetch items from a row The Natural Philosopher <tnp@invalid.invalid> - 2013-05-04 11:08 +0100
      Re: fetch items from a row The Natural Philosopher <tnp@invalid.invalid> - 2013-05-04 11:06 +0100
    Re: fetch items from a row The Natural Philosopher <tnp@invalid.invalid> - 2013-05-04 11:05 +0100
    Re: fetch items from a row Denis McMahon <denismfmcmahon@gmail.com> - 2013-08-05 00:19 +0000

#9232 — fetch items from a row

Fromrichard <noreply@example.com>
Date2013-05-03 15:55 -0400
Subjectfetch items from a row
Message-ID<8w0ngcjvc3xo$.7xh0lk9pqzs0.dlg@40tude.net>
How would I change the '42' value to a string value based upon the value
retrieved from using $_GET('number')?


<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?>

[toc] | [next] | [standalone]


#9233

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-05-03 20:18 +0000
Message-ID<km162d$j1l$1@yojimbo.hack>
In reply to#9232
On 2013-05-03, richard <noreply@example.com> wrote:
>
> How would I change the '42' value to a string value based upon the value
> retrieved from using $_GET('number')?
> [snip]

What do you mean by this? Do you want the value "42" cast as a string?

-- 
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++

[toc] | [prev] | [next] | [standalone]


#9234

FromTim Streater <timstreater@greenbee.net>
Date2013-05-03 21:49 +0100
Message-ID<timstreater-0F52B7.21495203052013@news.individual.net>
In reply to#9233
In article <km162d$j1l$1@yojimbo.hack>,
 Salvatore <sal@yojimbo.hack.invalid> wrote:

> On 2013-05-03, richard <noreply@example.com> wrote:
> >
> > How would I change the '42' value to a string value based upon the value
> > retrieved from using $_GET('number')?
> > [snip]
> 
> What do you mean by this? Do you want the value "42" cast as a string?

More likely:

$result = mysql_query("SELECT id,email FROM people WHERE id = '" . 
$_GET['number'] . "'");

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#10389

FromMladen Gogala <gogala.mladen@gmail.com>
Date2013-08-04 17:06 +0000
Message-ID<pan.2013.08.04.17.06.01@gmail.com>
In reply to#9234
On Fri, 03 May 2013 21:49:52 +0100, Tim Streater wrote:

> More likely:
> 
> $result = mysql_query("SELECT id,email FROM people WHERE id = '" .
> $_GET['number'] . "'");

And the code like that is the basis for all SQL injection attacks. It's 
so frequent that even comic strips have been written about it:

http://xkcd.com/327/

If you have such code in the client facing application, prepare to meet 
little Bobby Tables.


-- 
Mladen Gogala
The Oracle Whisperer
http://mgogala.byethost5.com

[toc] | [prev] | [next] | [standalone]


#10393

FromTim Streater <timstreater@greenbee.net>
Date2013-08-04 18:41 +0100
Message-ID<timstreater-310086.18411804082013@news.individual.net>
In reply to#10389
In article <pan.2013.08.04.17.06.01@gmail.com>,
 Mladen Gogala <gogala.mladen@gmail.com> wrote:

> On Fri, 03 May 2013 21:49:52 +0100, Tim Streater wrote:
> 
> > More likely:
> > 
> > $result = mysql_query("SELECT id,email FROM people WHERE id = '" .
> > $_GET['number'] . "'");
> 
> And the code like that is the basis for all SQL injection attacks. It's 
> so frequent that even comic strips have been written about it:
> 
> http://xkcd.com/327/
> 
> If you have such code in the client facing application, prepare to meet 
> little Bobby Tables.

I didn't write the above.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#10401

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-08-04 22:27 +0100
Message-ID<ktmgv5$bra$1@news.albasani.net>
In reply to#10389
On 04/08/13 18:06, Mladen Gogala wrote:
> On Fri, 03 May 2013 21:49:52 +0100, Tim Streater wrote:
>
>> More likely:
>>
>> $result = mysql_query("SELECT id,email FROM people WHERE id = '" .
>> $_GET['number'] . "'");
> And the code like that is the basis for all SQL injection attacks. It's
> so frequent that even comic strips have been written about it:
>
> http://xkcd.com/327/
>
> If you have such code in the client facing application, prepare to meet
> little Bobby Tables.
>
>
avoided simply by :

$result = mysql_query(sprintf("SELECT id,email FROM people WHERE id = '%d'",
$_GET['number'] ));

Using sprintf not only makes everything to look reasonable at code 
inspection level  it self validates stuff that should be a number and 
gurantees only a number.

Likewise either escape strings or hexify them.

It isn't rocket science.

-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#10402

FromTim Streater <timstreater@greenbee.net>
Date2013-08-04 23:15 +0100
Message-ID<timstreater-24A496.23151504082013@news.individual.net>
In reply to#10401
In article <ktmgv5$bra$1@news.albasani.net>,
 The Natural Philosopher <tnp@invalid.invalid> wrote:

> On 04/08/13 18:06, Mladen Gogala wrote:
> > On Fri, 03 May 2013 21:49:52 +0100, Tim Streater wrote:
> >
> >> More likely:
> >>
> >> $result = mysql_query("SELECT id,email FROM people WHERE id = '" .
> >> $_GET['number'] . "'");
> > And the code like that is the basis for all SQL injection attacks. It's
> > so frequent that even comic strips have been written about it:
> >
> > http://xkcd.com/327/
> >
> > If you have such code in the client facing application, prepare to meet
> > little Bobby Tables.
> >
> >
> avoided simply by :
> 
> $result = mysql_query(sprintf("SELECT id,email FROM people WHERE id = '%d'",
> $_GET['number'] ));
> 
> Using sprintf not only makes everything to look reasonable at code 
> inspection level  it self validates stuff that should be a number and 
> gurantees only a number.
> 
> Likewise either escape strings or hexify them.
> 
> It isn't rocket science.

Oh yeah, it turns out I did write that. Well duh. Mr Gogala can't have 
read the thread, otherwise he would have seen that the point was to find 
out WTF richard was babbling about wrt strings. Problems resulting from 
lack of code sanitisation are second order at most where he's concerned.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#10407

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-08-04 22:09 -0400
Message-ID<ktn14i$srp$3@dont-email.me>
In reply to#10401
On 8/4/2013 5:27 PM, The Natural Philosopher wrote:
> On 04/08/13 18:06, Mladen Gogala wrote:
>> On Fri, 03 May 2013 21:49:52 +0100, Tim Streater wrote:
>>
>>> More likely:
>>>
>>> $result = mysql_query("SELECT id,email FROM people WHERE id = '" .
>>> $_GET['number'] . "'");
>> And the code like that is the basis for all SQL injection attacks. It's
>> so frequent that even comic strips have been written about it:
>>
>> http://xkcd.com/327/
>>
>> If you have such code in the client facing application, prepare to meet
>> little Bobby Tables.
>>
>>
> avoided simply by :
>
> $result = mysql_query(sprintf("SELECT id,email FROM people WHERE id =
> '%d'",
> $_GET['number'] ));
>
> Using sprintf not only makes everything to look reasonable at code
> inspection level  it self validates stuff that should be a number and
> gurantees only a number.
>
> Likewise either escape strings or hexify them.
>
> It isn't rocket science.
>

Which gives incorrect results if the value isn't an integer.  It is much 
better to validate the data and, if it is incorrect (i.e. in this case, 
not an integer), provide an error message instead of incorrect results.

But then only a programmer would know that.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#9235

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-05-03 20:47 +0000
Message-ID<km17ou$da$1@dont-email.me>
In reply to#9232
On Fri, 03 May 2013 15:55:58 -0400, richard wrote:

> How would I change the '42' value to a string value based upon the value
> retrieved from using $_GET('number')?
> 
> <?php $result = mysql_query("SELECT id,email FROM people WHERE id =
> '42'"); if (!$result) {
>     echo 'Could not run query: ' . mysql_error();
>     exit;
> }
> $row = mysql_fetch_row($result);
> 
> echo $row[0]; // 42 echo $row[1]; // the email value ?>

Rephrase the question so it makes some sort of sense.

$_GET is not a function, $_GET('number') is invalid syntax.

$_GET['number'] will return the string from the get request that was 
prefixed with "(?|&)number= up to but not including the end of the 
request or the next "&" whichever comes first.

If you're trying to use the string value as a numeric value in a query, 
then you may not want to put quotes round it in the query string:

select * from table where column = '42'; // looks for a string
select * from table where column = 42; // looks for a number

As the value you get from the request is a string, and the sql command is 
a string, you could just copy the string value across, or you could force 
it to a number and back in the process, which might be slightly slower, 
but would be a lot safer:

num = -1;
if (isset($_GET['number'])) num = intval($_GET['number'])
if ( num > 0 ) {
sql = "select * from table where column = {$num}";
}
else {
// handle invalid number here
}

But all of this presumes that your issue is that you are comparing a 
string value with an integer field, and that's why you're not getting the 
result you expect - however you really haven't presented enough 
information about the problem - and if your issue is a mysql one, perhaps 
you need to ask in a mysql group.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#9236

FromRichard Yates <richard@yatesguitar.com>
Date2013-05-03 14:00 -0700
Message-ID<k198o8lkqakihel5gu8venk2in088k7i5l@4ax.com>
In reply to#9232
On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
wrote:

>
>How would I change the '42' value to a string value based upon the value
>retrieved from using $_GET('number')?
>
>
><?php
>$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>if (!$result) {
>    echo 'Could not run query: ' . mysql_error();
>    exit;
>}
>$row = mysql_fetch_row($result);
>
>echo $row[0]; // 42
>echo $row[1]; // the email value
>?>

Your question is a little confusing. It sounds like you are GETting an
integer and want to use it in the mysql query there the id is stored
as a string in the database. 

$number = $_GET['number'];
$result = mysql_query("select id, email from people where id='$number'
";

But I also wonder why your id numbers in the database are stored as
strings and not as integer primary keys (which they seem to be).

[toc] | [prev] | [next] | [standalone]


#9237

FromScott Johnson <noonehome@chalupasworld.com>
Date2013-05-03 14:21 -0700
Message-ID<km19ip$o9p$1@dont-email.me>
In reply to#9236
On 5/3/2013 2:00 PM, Richard Yates wrote:
> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
> wrote:
>
>>
>> How would I change the '42' value to a string value based upon the value
>> retrieved from using $_GET('number')?
>>
>>
>> <?php
>> $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>> if (!$result) {
>>     echo 'Could not run query: ' . mysql_error();
>>     exit;
>> }
>> $row = mysql_fetch_row($result);
>>
>> echo $row[0]; // 42
>> echo $row[1]; // the email value
>> ?>
>
> Your question is a little confusing. It sounds like you are GETting an
> integer and want to use it in the mysql query there the id is stored
> as a string in the database.
>
> $number = $_GET['number'];
> $result = mysql_query("select id, email from people where id='$number'
> ";
>
> But I also wonder why your id numbers in the database are stored as
> strings and not as integer primary keys (which they seem to be).
>
>

I am no expert here but in my experience $_GET holds a string from the 
URI query no matter the value.

And the MySQL results will return a string as well whether it is as an 
integer or string.

I would double check that in the mysql ng.




[toc] | [prev] | [next] | [standalone]


#9238

FromLew Pitcher <lpitcher@teksavvy.com>
Date2013-05-03 17:38 -0400
Message-ID<57Wgt.41416$YR4.18232@newsfe28.iad>
In reply to#9236
On Friday 03 May 2013 17:00, in comp.lang.php, richard@yatesguitar.com
wrote:

> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
> wrote:
> 
>>
>>How would I change the '42' value to a string value based upon the value
>>retrieved from using $_GET('number')?
>>
>>
>><?php
>>$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>>if (!$result) {
>>    echo 'Could not run query: ' . mysql_error();
>>    exit;
>>}
>>$row = mysql_fetch_row($result);
>>
>>echo $row[0]; // 42
>>echo $row[1]; // the email value
>>?>
> 
> Your question is a little confusing. It sounds like you are GETting an
> integer and want to use it in the mysql query there the id is stored
> as a string in the database.
> 
> $number = $_GET['number'];
> $result = mysql_query("select id, email from people where id='$number'
> ";

Have you met little Bobby Tables (http://xkcd.com/327/) ?

The PHP documentation webpages have a slightly better suggestion for this
sort of query...

From http://www.php.net/manual/en/function.mysql-query.php
  // Formulate Query
  // This is the best way to perform an SQL query
  // For more examples, see mysql_real_escape_string()
  $query = sprintf("SELECT firstname, lastname, address, age FROM friends 
      WHERE firstname='%s' AND lastname='%s'",
      mysql_real_escape_string($firstname),
      mysql_real_escape_string($lastname));

  // Perform Query
  $result = mysql_query($query);


-- 
Lew Pitcher
"In Skills, We Trust"

[toc] | [prev] | [next] | [standalone]


#9239

FromRichard Yates <richard@yatesguitar.com>
Date2013-05-03 15:15 -0700
Message-ID<efd8o8191dv49c5pu0ejrr4nu3nf7c4rol@4ax.com>
In reply to#9238
On Fri, 03 May 2013 17:38:08 -0400, Lew Pitcher
<lpitcher@teksavvy.com> wrote:

>On Friday 03 May 2013 17:00, in comp.lang.php, richard@yatesguitar.com
>wrote:
>
>> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
>> wrote:
>> 
>>>
>>>How would I change the '42' value to a string value based upon the value
>>>retrieved from using $_GET('number')?
>>>
>>>
>>><?php
>>>$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>>>if (!$result) {
>>>    echo 'Could not run query: ' . mysql_error();
>>>    exit;
>>>}
>>>$row = mysql_fetch_row($result);
>>>
>>>echo $row[0]; // 42
>>>echo $row[1]; // the email value
>>>?>
>> 
>> Your question is a little confusing. It sounds like you are GETting an
>> integer and want to use it in the mysql query there the id is stored
>> as a string in the database.
>> 
>> $number = $_GET['number'];
>> $result = mysql_query("select id, email from people where id='$number'
>> ";
>
>Have you met little Bobby Tables (http://xkcd.com/327/) ?
>
>The PHP documentation webpages have a slightly better suggestion for this
>sort of query...
>
>From http://www.php.net/manual/en/function.mysql-query.php
>  // Formulate Query
>  // This is the best way to perform an SQL query
>  // For more examples, see mysql_real_escape_string()
>  $query = sprintf("SELECT firstname, lastname, address, age FROM friends 
>      WHERE firstname='%s' AND lastname='%s'",
>      mysql_real_escape_string($firstname),
>      mysql_real_escape_string($lastname));
>
>  // Perform Query
>  $result = mysql_query($query);

Yes, I know about BQ. And yes, certainly, if I were doing it, the
input would be validated, etc. But, the poster is clearly struggling
with the simplest elements of php/mysql and, to learn that part,
needed only the simplest answer to his question. 

[toc] | [prev] | [next] | [standalone]


#9247

FromSwissCheese <swisscheese@cfl.rr.com>
Date2013-05-04 05:56 -0400
Message-ID<km2lqk$npk$1@dont-email.me>
In reply to#9239
On 05/03/2013 06:15 PM, Richard Yates wrote:
> On Fri, 03 May 2013 17:38:08 -0400, Lew Pitcher
> <lpitcher@teksavvy.com> wrote:
>
>> On Friday 03 May 2013 17:00, in comp.lang.php, richard@yatesguitar.com
>> wrote:
>>
>>> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
>>> wrote:
>>>
>>>>
>>>> How would I change the '42' value to a string value based upon the value
>>>> retrieved from using $_GET('number')?
>>>>
>>>>
>>>> <?php
>>>> $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>>>> if (!$result) {
>>>>     echo 'Could not run query: ' . mysql_error();
>>>>     exit;
>>>> }
>>>> $row = mysql_fetch_row($result);
>>>>
>>>> echo $row[0]; // 42
>>>> echo $row[1]; // the email value
>>>> ?>
>>>
>>> Your question is a little confusing. It sounds like you are GETting an
>>> integer and want to use it in the mysql query there the id is stored
>>> as a string in the database.
>>>
>>> $number = $_GET['number'];
>>> $result = mysql_query("select id, email from people where id='$number'
>>> ";
>>
>> Have you met little Bobby Tables (http://xkcd.com/327/) ?
>>
>> The PHP documentation webpages have a slightly better suggestion for this
>> sort of query...
>>
>>From http://www.php.net/manual/en/function.mysql-query.php
>>   // Formulate Query
>>   // This is the best way to perform an SQL query
>>   // For more examples, see mysql_real_escape_string()
>>   $query = sprintf("SELECT firstname, lastname, address, age FROM friends
>>       WHERE firstname='%s' AND lastname='%s'",
>>       mysql_real_escape_string($firstname),
>>       mysql_real_escape_string($lastname));
>>
>>   // Perform Query
>>   $result = mysql_query($query);
>
> Yes, I know about BQ. And yes, certainly, if I were doing it, the
> input would be validated, etc. But, the poster is clearly struggling
> with the simplest elements of php/mysql and, to learn that part,
> needed only the simplest answer to his question.
>

   Not only that but last time I checked, PHP/MySQL still doesn't allow 
multi-statement queries.

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#9250

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-04 11:08 +0100
Message-ID<km2mls$ngj$3@news.albasani.net>
In reply to#9247
On 04/05/13 10:56, SwissCheese wrote:
> On 05/03/2013 06:15 PM, Richard Yates wrote:
>> On Fri, 03 May 2013 17:38:08 -0400, Lew Pitcher
>> <lpitcher@teksavvy.com> wrote:
>>
>>> On Friday 03 May 2013 17:00, in comp.lang.php, richard@yatesguitar.com
>>> wrote:
>>>
>>>> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
>>>> wrote:
>>>>
>>>>>
>>>>> How would I change the '42' value to a string value based upon the 
>>>>> value
>>>>> retrieved from using $_GET('number')?
>>>>>
>>>>>
>>>>> <?php
>>>>> $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>>>>> if (!$result) {
>>>>>     echo 'Could not run query: ' . mysql_error();
>>>>>     exit;
>>>>> }
>>>>> $row = mysql_fetch_row($result);
>>>>>
>>>>> echo $row[0]; // 42
>>>>> echo $row[1]; // the email value
>>>>> ?>
>>>>
>>>> Your question is a little confusing. It sounds like you are GETting an
>>>> integer and want to use it in the mysql query there the id is stored
>>>> as a string in the database.
>>>>
>>>> $number = $_GET['number'];
>>>> $result = mysql_query("select id, email from people where id='$number'
>>>> ";
>>>
>>> Have you met little Bobby Tables (http://xkcd.com/327/) ?
>>>
>>> The PHP documentation webpages have a slightly better suggestion for 
>>> this
>>> sort of query...
>>>
>>> From http://www.php.net/manual/en/function.mysql-query.php
>>>   // Formulate Query
>>>   // This is the best way to perform an SQL query
>>>   // For more examples, see mysql_real_escape_string()
>>>   $query = sprintf("SELECT firstname, lastname, address, age FROM 
>>> friends
>>>       WHERE firstname='%s' AND lastname='%s'",
>>>       mysql_real_escape_string($firstname),
>>>       mysql_real_escape_string($lastname));
>>>
>>>   // Perform Query
>>>   $result = mysql_query($query);
>>
>> Yes, I know about BQ. And yes, certainly, if I were doing it, the
>> input would be validated, etc. But, the poster is clearly struggling
>> with the simplest elements of php/mysql and, to learn that part,
>> needed only the simplest answer to his question.
>>
>
>   Not only that but last time I checked, PHP/MySQL still doesn't allow 
> multi-statement queries.
>
well it certainly allows the above.


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9249

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-04 11:06 +0100
Message-ID<km2min$ngj$2@news.albasani.net>
In reply to#9236
On 03/05/13 22:00, Richard Yates wrote:
> On Fri, 3 May 2013 15:55:58 -0400, richard <noreply@example.com>
> wrote:
>
>> How would I change the '42' value to a string value based upon the value
>> retrieved from using $_GET('number')?
>>
>>
>> <?php
>> $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
>> if (!$result) {
>>     echo 'Could not run query: ' . mysql_error();
>>     exit;
>> }
>> $row = mysql_fetch_row($result);
>>
>> echo $row[0]; // 42
>> echo $row[1]; // the email value
>> ?>
> Your question is a little confusing. It sounds like you are GETting an
> integer and want to use it in the mysql query there the id is stored
> as a string in the database.
>
> $number = $_GET['number'];
> $result = mysql_query("select id, email from people where id='$number'
> ";
>
> But I also wonder why your id numbers in the database are stored as
> strings and not as integer primary keys (which they seem to be).
>
>
It makes no difference. SQL appears to work with either syntax on a 
numeric field.


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9248

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-04 11:05 +0100
Message-ID<km2mgf$ngj$1@news.albasani.net>
In reply to#9232
On 03/05/13 20:55, richard wrote:
> How would I change the '42' value to a string value based upon the value
> retrieved from using $_GET('number')?

$_GET['number'] IIRC...
>
> <?php
> $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
> if (!$result) {
>      echo 'Could not run query: ' . mysql_error();
>      exit;
> }
> $row = mysql_fetch_row($result);
>
> echo $row[0]; // 42
> echo $row[1]; // the email value
> ?>

$result = mysql_query("SELECT id,email FROM people WHERE id = '<?echo $_GET['number']?>'");

with usual caveats about SQL injection etc.

I tend to use - sprintf as in

$query=sprintf("SELECT id,email FROM people WHERE id = '%d'" 
,$_GET['number'])

$result = mysql_query($query);

to make sure what's in there is only a decimal number.




  -- Ineptocracy (in-ep-toc’-ra-cy) – a system of government where the 
least capable to lead are elected by the least capable of producing, and 
where the members of society least likely to sustain themselves or 
succeed, are rewarded with goods and services paid for by the 
confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#10403

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-08-05 00:19 +0000
Message-ID<ktmr2i$g8m$3@dont-email.me>
In reply to#9232
On Fri, 03 May 2013 15:55:58 -0400, richard wrote:

> How would I change the '42' value to a string value based upon the value
> retrieved from using $_GET('number')?

$_GET isn't a function, it's an associative array.

If you're trying to read the "number" parameter of a get request, you use:

$_GET['number'];

or:

$_GET["number"];

The type of brackets is very important, although in this specific case, 
the type of quote marks is less important than it is at other times.

> <?php $result = mysql_query("SELECT id,email FROM people WHERE id =
> '42'"); if (!$result) {
>     echo 'Could not run query: ' . mysql_error();
>     exit;
> }
> $row = mysql_fetch_row($result);
> 
> echo $row[0]; // 42 echo $row[1]; // the email value ?>

If you only want the email, only request the email.

If you want to output the email address for a specific id number that 
comes from a get request, which I think is what you mean:

<?php

$num = 0;

$if isset( $_GET["number"] ) $num = intval( $_GET["number"] );

$sql = "SELECT email FROM people WHERE id = '{$num}'";

$result = mysql_query( sql );

if ( !$result ) {

  echo "mysql query \"{$sql}\" failed with: " . mysql_error() . "\n";

} else {

  $rows = mysql_num_rows( $result );

  if ( $rows != 1 ) {

    echo "Unexpected result from sql query \"{$sql}\", {$rows} rows 
returned when 1 row expected!\n";

  } else {

    $row = mysql_fetch_row($result);

    if ( $row ) {

      echo "Email address is: {$result[0]}\n";

    } else {

      echo "No email address found for that ID number\n";

    }

  }

}

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web