move "if" logic from php into query

This is a discussion on move "if" logic from php into query within the PHP General forums, part of the PHP Programming Forums category; I couldn't get the page to load when the logic for line 31, "($id == $_POST[recordID])", was ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-26-2007
Thufir
 
Posts: n/a
Default move "if" logic from php into query

I couldn't get the page to load when the logic for line 31, "($id ==
$_POST[recordID])", was in the query. Can the logic for that be moved to the
query? I expect so.

I tried changing the where clause of the query, no go.


[thufir@localhost ~]$
[thufir@localhost ~]$ cat /var/www/html/insertContacts.php -n
1 <html>
2 <head><title>insert contacts</title></head>
3 <body>
4 <?php
5
6
7 $user="feeds";
8 $host="localhost";
9 $password="password";
10 $database = "feeds";
11
12 $connection = mysql_connect($host,$user,$password)
13 or die ("couldn't connect to server");
14 $db = mysql_select_db($database,$connection)
15 or die ("Couldn't select database");
16
17 $query = "INSERT INTO contacts (id , notes) VALUES
('$_POST[recordID]' , '$_POST[contacts]')";
18 $result = mysql_query($query)
19 or die ("Couldn't execute insert query.");
20
21 $query = "SELECT contacts.id, px_items.id, title, notes
FROM contacts, px_items WHERE
22 contacts.id=px_items.id";
23
24 $result = mysql_query($query)
25 or die ("Couldn't execute second query.");
26
27 while ($row = mysql_fetch_array($result))
28 {
29 extract ($row);
30
31 if ($id == $_POST[recordID])
32 {
33 echo $id;
34 echo "<br>";
35 echo "$title";
36 echo "<br><br>";
37 echo $notes;
38 echo "<br><br><br><br>";
39 echo "<br><br><br><br>";
40
41 }//if
42 }//while
43
44
45
46
47 echo "<br>";
48 echo "<a href=\"";
49 echo "http://localhost/contacts.php";
50 echo "\">";
51 echo "http://localhost/contacts.php";
52 echo "</a>";
53 echo "<br><br>";
54
55
56 echo "<br>";
57 echo "<a href=\"";
58 echo "http://localhost/items_notes.php";
59 echo "\">";
60 echo "http://localhost/items_notes.php";
61 echo "</a>";
62 echo "<br><br>";
63
64
65 ?>
66 </body> </html>
[thufir@localhost ~]$
[thufir@localhost ~]$ date
Thu Apr 26 09:24:31 BST 2007
[thufir@localhost ~]$
[thufir@localhost ~]$



thanks,

Thufir
Reply With Quote
  #2 (permalink)  
Old 04-26-2007
Zoltán Németh
 
Posts: n/a
Default Re: [PHP] move "if" logic from php into query

2007. 04. 26, csütörtök keltezéssel 08.28-kor Thufir ezt Ã*rta:
> I couldn't get the page to load when the logic for line 31, "($id ==
> $_POST[recordID])", was in the query. Can the logic for that be moved to the
> query? I expect so.
>
> I tried changing the where clause of the query, no go.


you SHOULD move it to the query, because if you need only that row, the
rest of the while loop is just a waste of cycles.
however, before putting it into the query, first verify
$_POST['recordID'] to avoid SQL injection. if it should be a number,
just typecast it to int like this:
$rec_id = (int) $_POST['recordID'];

and there are other problems with your handling of the query result. you
use extract, but have two columns named 'id'. this leads to confusion.
either select them with an alias provided or select only one of them.

thus the query should be something like:

$query = "SELECT contacts.id AS c_id, px_items.id AS p_id, title,
notes FROM contacts, px_items WHERE contacts.id=px_items.id AND
contacts.id=$rec_id";
(or you can put px_items.id=$rec_id if that's the id you are looking
for)

greets
Zoltán Németh

>
>
> [thufir@localhost ~]$
> [thufir@localhost ~]$ cat /var/www/html/insertContacts.php -n
> 1 <html>
> 2 <head><title>insert contacts</title></head>
> 3 <body>
> 4 <?php
> 5
> 6
> 7 $user="feeds";
> 8 $host="localhost";
> 9 $password="password";
> 10 $database = "feeds";
> 11
> 12 $connection = mysql_connect($host,$user,$password)
> 13 or die ("couldn't connect to server");
> 14 $db = mysql_select_db($database,$connection)
> 15 or die ("Couldn't select database");
> 16
> 17 $query = "INSERT INTO contacts (id , notes) VALUES
> ('$_POST[recordID]' , '$_POST[contacts]')";
> 18 $result = mysql_query($query)
> 19 or die ("Couldn't execute insert query.");
> 20
> 21 $query = "SELECT contacts.id, px_items.id, title, notes
> FROM contacts, px_items WHERE
> 22 contacts.id=px_items.id";
> 23
> 24 $result = mysql_query($query)
> 25 or die ("Couldn't execute second query.");
> 26
> 27 while ($row = mysql_fetch_array($result))
> 28 {
> 29 extract ($row);
> 30
> 31 if ($id == $_POST[recordID])
> 32 {
> 33 echo $id;
> 34 echo "<br>";
> 35 echo "$title";
> 36 echo "<br><br>";
> 37 echo $notes;
> 38 echo "<br><br><br><br>";
> 39 echo "<br><br><br><br>";
> 40
> 41 }//if
> 42 }//while
> 43
> 44
> 45
> 46
> 47 echo "<br>";
> 48 echo "<a href=\"";
> 49 echo "http://localhost/contacts.php";
> 50 echo "\">";
> 51 echo "http://localhost/contacts.php";
> 52 echo "</a>";
> 53 echo "<br><br>";
> 54
> 55
> 56 echo "<br>";
> 57 echo "<a href=\"";
> 58 echo "http://localhost/items_notes.php";
> 59 echo "\">";
> 60 echo "http://localhost/items_notes.php";
> 61 echo "</a>";
> 62 echo "<br><br>";
> 63
> 64
> 65 ?>
> 66 </body> </html>
> [thufir@localhost ~]$
> [thufir@localhost ~]$ date
> Thu Apr 26 09:24:31 BST 2007
> [thufir@localhost ~]$
> [thufir@localhost ~]$
>
>
>
> thanks,
>
> Thufir
>

Reply With Quote
  #3 (permalink)  
Old 04-26-2007
Chris
 
Posts: n/a
Default Re: [PHP] move "if" logic from php into query


Thufir wrote:
> I couldn't get the page to load when the logic for line 31, "($id ==
> $_POST[recordID])", was in the query. Can the logic for that be moved to the
> query? I expect so.



$query = "SELECT contacts.id, px_items.id, title, notes
FROM contacts, px_items WHERE contacts.id=px_items.id AND recordid='" .
(int)$_POST['recordID'] . "'";

The (int) will make sure that it is a number (so a string becomes "0").

--
Postgresql & php tutorials
http://www.designmagick.com/
Reply With Quote
  #4 (permalink)  
Old 04-26-2007
Sebe
 
Posts: n/a
Default Re: [PHP] move "if" logic from php into query

Chris wrote:
>
> Thufir wrote:
>> I couldn't get the page to load when the logic for line 31, "($id ==
>> $_POST[recordID])", was in the query. Can the logic for that be
>> moved to the
>> query? I expect so.

>
>
> $query = "SELECT contacts.id, px_items.id, title, notes
> FROM contacts, px_items WHERE contacts.id=px_items.id AND recordid='"
> . (int)$_POST['recordID'] . "'";
>
> The (int) will make sure that it is a number (so a string becomes "0").
>

i always use intval() on something i'm inserting into database that
*should* be a integer. i don't know if there is a difference or a good
reason to pick one or the other.. i'm not Richard so maybe he can create
an interesting story for us on the *proper* way ;-)
Reply With Quote
  #5 (permalink)  
Old 04-27-2007
Thufir
 
Posts: n/a
Default Re: move "if" logic from php into query

On Apr 26, 1:45 am, dmag...@gmail.com (Chris) wrote:
[...]
> $query = "SELECT contacts.id, px_items.id, title, notes
> FROM contacts, px_items WHERE contacts.id=px_items.id AND recordid='" .
> (int)$_POST['recordID'] . "'";
>
> The (int) will make sure that it is a number (so a string becomes "0").

[...]


aha! thanks. this is like a cast, or making sure that the it's a
number, that the right type is passed? ok :)


-Thufir

Reply With Quote
  #6 (permalink)  
Old 04-27-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] move "if" logic from php into query

On Thu, April 26, 2007 4:02 am, Sebe wrote:
> i always use intval() on something i'm inserting into database that
> *should* be a integer. i don't know if there is a difference or a good
> reason to pick one or the other.. i'm not Richard so maybe he can
> create
> an interesting story for us on the *proper* way ;-)


Well...

(int) $foo;
intval($foo);

are pretty interchangable, really.

Just depends if you're an old C hacker or you prefer making function
calls.

I can't imagine doing enough of either in any real script for
performance to be an issue, so let's skip the whole benchmark thread,
please???

That said, I've recently decided that even after doing (int) [or
intval()] that I'd like to be 100% kosher and still do
mysql_real_escape_string.

Mainly because somebody pointed out that doing the same thing for
(float) could yield things that may not be "good" in SQL like
underflow, overflow, exponential notation, and also that the - sign in
front *could* end up being part of a subtraction and you *could*
manage to leave out the space, and then you've got an SQL comment
instead of subtraction.

$a = (int) $_REQUEST['a'];
$b = (int) $_REQUEST['b'];
$query = "select $a-$b ";

http://example.com/a=5&b=-3

So the (int) or intval() is "not enough" imho.

YMMV

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:22 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0