TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-19-2009, 02:48 AM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default Grabbing IDs from Database

Hey

I'm creating a simple shopping cart but I'm stuck trying to figure out why I'm not getting the output I'm looking for.

PHP Code:
function product_exists($input_id)
{
  
$mysqli = new mysqli('localhost''root''''store');
  
$result $mysqli->query("SELECT ID FROM products");
  
  
$array = array();
  while (
$row $result->fetch_object())
  {
    
$array[] = $row->ID;
  }
  
  foreach (
$array as $product_id)
  {
    if (
$product_id == $input_id)
      return 
true;
    else
      return 
false;
  }

Basically what I'm trying to do is store all the IDs that are in my database into an array and compare the ID the user has given me. If it matches it needs to return true, else false.

Here is the front side:

PHP Code:
$product_id $_REQUEST['id'];
if (
product_exists($product_id))
  echo 
'Product Exist!';
else
  echo 
'Not working yet'
If I do add.php?id=1 it works fine. But if I do add.php?id=2 it will not work.

Can anyone lend a hand please?
9three is offline  
Reply With Quote
Old 06-19-2009, 02:52 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

First off you are not doing this correctly, you should never leave this sort of processing to the script. Make your query
sql Code:
SELECT id FROM products WHERE id='$product_id'
Just be sure to change
$product_id = $_REQUEST['id']; to
$product_id
= (int)$_REQUEST['id'];
to prevent SQL injection.

If it returns a row it exists, if there are no rows it does not.

I would strongly suggest putting anything as complicated as a shopping cart aside to learn SQL.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-19-2009, 03:04 AM   #3 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

Got it. Thank you, I've added the (int).

PHP Code:
$result $mysqli->query("SELECT ID FROM products WHERE ID = '$input_id'"); 
I'm trying to re factor the loops because if I (for example)4,000 items then the function would need to place all those IDs into an array and then sort through it. I would imagine it would slow down the application a lot.
9three is offline  
Reply With Quote
Old 06-19-2009, 03:11 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by 9three View Post
Got it. Thank you, I've added the (int).

PHP Code:
$result $mysqli->query("SELECT ID FROM products WHERE ID = '$input_id'"); 
Just a style tip, don't capitalize field names. Generally the only thing that should be caps are keywords. It does not really matter for small stuff like this, but it pays off when you have really big queries.

Quote:
Originally Posted by 9three View Post
I'm trying to re factor the loops because if I (for example)4,000 items then the function would need to place all those IDs into an array and then sort through it. I would imagine it would slow down the application a lot.
Doing it in the script would slow it immensely. SQL can sort though tens of thousands of rows in under a second, doing it in PHP would be slow and memory intensive. A database that returns too much or too little is not doing its job, a good query should return exactly what you need to complete the process at hand. Let SQL do as much of the data processing as possible.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-19-2009, 03:21 AM   #5 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

So this is what I thought of but its returning false on anything bigger than 1

PHP Code:
$mysqli = new mysqli('localhost''root''''store');
$result $mysqli->query("SELECT ID FROM products WHERE ID = '$input_id'");
  
  if (
$result == $input_id)
    return 
true;
  return 
false
I'm not sure if that's what you meant by allowing SQL do the work?
9three is offline  
Reply With Quote
Old 06-19-2009, 03:22 AM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by 9three View Post
So this is what I thought of but its returning false on anything bigger than 1

PHP Code:
$mysqli = new mysqli('localhost''root''''store');
$result $mysqli->query("SELECT ID FROM products WHERE ID = '$input_id'");
  
  if (
$result == $input_id)
    return 
true;
  return 
false
I'm not sure if that's what you meant by allowing SQL do the work?
SQL is doing all the work you need to, just count the rows (mysql_num_rows). This is not incredibly efficient, but better ways would require more advanced SQL. Once again, I highly seggust you learn to code SQL before making a data driven script.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Adding Images to a database from a folder Rendair Advanced PHP Programming 3 01-13-2008 07:40 PM
Important Database Structure Question! AnthonyOS MySQL & Databases 5 12-20-2007 03:26 PM


All times are GMT. The time now is 08:05 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design