Jump to content


Photo

if/else loop problem


  • Please log in to reply
7 replies to this topic

#1 Comenius

Comenius
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 October 2006 - 05:07 PM

Hey, I'm rather new to php and am suffering from what is probably termed as a "rookie error"...

I have 100 tickets for sale for a society event, and people are reserving tickets online. Their information is sent to a mysql database such that each reservation takes up one row.

I've set up a php script which tries to access row x of the mysql table. If it finds row x, then it sends the user's data to a waiting list table. If not, then it sends it to the ticket reservation table. I got it working last night, then started fannying around with it this afternoon and now I can't get it to work again (I'm sure the code is the same as last night but obviously not...).

The 'if' section doesn't seem to work, so the script just always includes tickets_success.php...

Here's the code:

<?php

// Open database connection

include 'dbdata.php';

// Check the number of tickets already sold

$result = mysql_query("SELECT id FROM tickets WHERE id = '100'");

// Determine which table to send the data to and include the script

   if ($result == '100')
   {
   include('tickets_waiting.php');
   }
   
   else
   {
   include('tickets_success.php');
   }

?>

Any pointing out of general idiocy on my part would be much appreciated... :)

#2 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 22 October 2006 - 05:11 PM

You need to extract the mysql results using mysql_fetch_array or any other of it's cousin functions.

<?php

// Open database connection

include 'dbdata.php';

// Check the number of tickets already sold

$result = mysql_query("SELECT id FROM tickets WHERE id = '100'");
$row = mysql_fetch_array($result);

// Determine which table to send the data to and include the script

   if ($row['id'] == '100')
   {
   include('tickets_waiting.php');
   }
   
   else
   {
   include('tickets_success.php');
   }

?>


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 October 2006 - 05:13 PM

$result is a result source. you actually have to pull the data out of it, using a variety of available functions. for instance:

<?php

// Open database connection

include 'dbdata.php';

// Check the number of tickets already sold

$result = mysql_query("SELECT id FROM tickets WHERE id = '100'");
$blah = mysql_fetch_assoc($result);
// Determine which table to send the data to and include the script

   if ($blah[0] == '100')
   {
   include('tickets_waiting.php');
   }
   
   else
   {
   include('tickets_success.php');
   }

?>

although, you are kind of doing double work here.  Your query is already fetching a result that equals 100.  If there is a result, then obviously id = 100, so there's no reason to check it again in php.

<?php

// Open database connection

include 'dbdata.php';

// Check the number of tickets already sold

$result = mysql_query("SELECT id FROM tickets WHERE id = '100'");
$blah = mysql_fetch_assoc($result);
// Determine which table to send the data to and include the script

   if ($blah)
   {
   include('tickets_waiting.php');
   }
   
   else
   {
   include('tickets_success.php');
   }

?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 Comenius

Comenius
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 October 2006 - 05:25 PM

Ok, I now get the error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/html/tickets_add.php on line 18

And the same error if I use mysql_fetch_array().

This was happening earlier, and I couldn't work out why, so in a rather rookie-ish way I just decided to try and do it a different way...

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 October 2006 - 05:30 PM

then you probably aren't actually connecting to your database. i see this:

include 'dbdata.php';

now what's in it? you sure the connect info is right? you sure that dbdata.php actually connects to the db with that info?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Comenius

Comenius
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 October 2006 - 05:44 PM

dbdata.php connects to the mysql server ok - it's the same script used by all my form processing scripts, which enter information into the database without any problems. The code is:

$server = mysql_connect("localhost", "*user*", "*pass*"); 
$connection = mysql_select_db("camlawsoc_com_-_data");


Where *user* and *pass* are obviously substituted for the username and password.



#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 October 2006 - 05:49 PM

okay then are you sure that your table and field names are currect?

change your code to this (add the '..or die(mysql_error()) to the end of your query):
<?php

// Open database connection

include 'dbdata.php';

// Check the number of tickets already sold

$result = mysql_query("SELECT id FROM tickets WHERE id = '100'") or die(mysql_error());
$blah = mysql_fetch_assoc($result);
// Determine which table to send the data to and include the script

   if ($blah)
   {
   include('tickets_waiting.php');
   }
   
   else
   {
   include('tickets_success.php');
   }

?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 Comenius

Comenius
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 October 2006 - 05:55 PM

I've found the problem: I called the field user_id in the table, but then id when trying to connect to it. It now all works well.

Thanks very much for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users