Comenius Posted October 22, 2006 Share Posted October 22, 2006 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:[code]<?php// Open database connectioninclude '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'); }?>[/code]Any pointing out of general idiocy on my part would be much appreciated... :) Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/ Share on other sites More sharing options...
High_-_Tek Posted October 22, 2006 Share Posted October 22, 2006 You need to extract the mysql results using mysql_fetch_array or any other of it's cousin functions.[code]<?php// Open database connectioninclude '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'); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112709 Share on other sites More sharing options...
.josh Posted October 22, 2006 Share Posted October 22, 2006 $result is a result source. you actually have to pull the data out of it, using a variety of available functions. for instance:[code]<?php// Open database connectioninclude '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'); }?>[/code]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. [code]<?php// Open database connectioninclude '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'); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112711 Share on other sites More sharing options...
Comenius Posted October 22, 2006 Author Share Posted October 22, 2006 Ok, I now get the error:[code]Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/html/tickets_add.php on line 18[/code]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... Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112715 Share on other sites More sharing options...
.josh Posted October 22, 2006 Share Posted October 22, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112716 Share on other sites More sharing options...
Comenius Posted October 22, 2006 Author Share Posted October 22, 2006 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:[code]$server = mysql_connect("localhost", "*user*", "*pass*"); $connection = mysql_select_db("camlawsoc_com_-_data");[/code]Where *user* and *pass* are obviously substituted for the username and password. Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112721 Share on other sites More sharing options...
.josh Posted October 22, 2006 Share Posted October 22, 2006 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):[code]<?php// Open database connectioninclude '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'); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112723 Share on other sites More sharing options...
Comenius Posted October 22, 2006 Author Share Posted October 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/24753-ifelse-loop-problem/#findComment-112725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.