shane85 Posted August 13, 2009 Share Posted August 13, 2009 hey guys....im trying to do an if/then statement involivng my mysql db... basically what I want to achieve is IF $ticker (a row in my database) is blank, die, and do not load the page, however IF $ticker has any sort of text in the row, continue loading the page...I was thinking something like if ($ticker='') { die; but obvioustly thats not correct...im a newbie so any help would be appreciated Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 13, 2009 Share Posted August 13, 2009 You should not use it in that fasion, you should use 'empty'. If (empty($ticker)){ die("Ticker is empty or not set"); //Display an error and kill page. } else { echo "Page is valid."; // Write code here if $ticker is valid } If $ticker is filled with a value then it'll execute the code in the ELSE, the DIE function is handy when writing a database error when you set text into it like I have.. Quote Link to comment Share on other sites More sharing options...
shane85 Posted August 13, 2009 Author Share Posted August 13, 2009 thank you for your help...I tried that, but it didnt work...perhaps it is because it is an rss file??? I have posted the code below would you mind telling me what I have done wrong? I get "feed code error" on title of page, and nothing displays <?php header('Content-type: application/xml'); $dbhost = 'localhost'; $dbuser = 'my_user_name'; $dbpass = 'my_password_name'; $dbname = 'my_database_name'; // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); // begin IF statement for ticker If (empty($ticker)){ die("Ticker is empty or not set"); //Display an error and kill page. } else { echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; echo "<link>http://www.mywebsite.ca</link>\n"; $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "<description></description>\n"; echo "<pubDate></pubDate>\n"; echo "<link>http://www.mywebsite.ca/test.php?post=$row->location_id</link>\n"; echo "<atom:link href=\"http://www.mywebsote.ca/test.php?post=$row->location_id\" rel=\"self\" type=\"application/rss+xml\"/>\n"; echo "</item>\n"; } echo "</channel>\n"; echo "</rss>\n"; // end of IF statement for ticker } Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 13, 2009 Share Posted August 13, 2009 I believe this is an SQL problem as your code seems alright. If it says 'RSS feed error'.. that is not in your code or SQL, is it? That means there's a problem in.. A) Either the database's output, B) The RSS is invalid. Quote Link to comment Share on other sites More sharing options...
shane85 Posted August 13, 2009 Author Share Posted August 13, 2009 but if I take out that IF statement, it displays correctly...only when the IF statement is in does it not display the page properly Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 Try var_dump($ticker); and paste here what this variable contains. Quote Link to comment Share on other sites More sharing options...
shane85 Posted August 13, 2009 Author Share Posted August 13, 2009 I took out the IF statement and currently have it like this <?php header('Content-type: application/xml'); $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'pass'; $dbname = 'name'; // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; echo "<link>http://www.website.ca</link>\n"; $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "<description></description>\n"; echo "<pubDate></pubDate>\n"; echo "<link>http://www.website.ca/testing/rss/test.php?post=$row->location_id</link>\n"; echo "<atom:link href=\"http://www.websote.ca/testing/rss/test.php?post=$row->location_id\" rel=\"self\" type=\"application/rss+xml\"/>\n"; echo "</item>\n"; } echo "</channel>\n"; echo "</rss>\n"; var_dump($ticker); and it displays http://www.website.ca http://www.website.ca/testing/rss/test.php?post=2 NULL ahhhhhh I had this working before...just without the IF ... lol Quote Link to comment Share on other sites More sharing options...
shane85 Posted August 13, 2009 Author Share Posted August 13, 2009 sorry guys....last time...whats wrong??? now its displaying in kinda in a style sheet but saying The XML Page Cannot Be Displayed Cannot view XML using style sheet ..... and heres the bottom half of my code again..its also saying "ticker is empty or not set" however I do have text in my ticker field in the database // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); If (empty($ticker)){ die("Ticker is empty or not set"); //Display an error and kill page. } else { echo "Page is valid."; // Write code here if $ticker is valid echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; echo "<link>http://www.fireshock.ca</link>\n"; $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "<description></description>\n"; echo "<pubDate></pubDate>\n"; echo "<link>http://www.fireshock.ca/testing/rss/test.php?post=$row->location_id</link>\n"; echo "<atom:link href=\"http://www.fireshock.ca/testing/rss/test.php?post=$row->location_id\" rel=\"self\" type=\"application/rss+xml\"/>\n"; echo "</item>\n"; } echo "</channel>\n"; echo "</rss>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 13, 2009 Share Posted August 13, 2009 I'd comment out your echo'ing code for now and vardump everything to make sure it is 100% valid before displaying it as XML.. It seems to be dumping, but is not <tag>'ing or formatting in a valid manner, that's why you get the XML error.. Also you can note: echo "<a href=\"test\" rel=\"self\"></a>"; Can become: echo '<a href="test" rel="self"></a>'; Single quotes are handy when working with HTML.. no need to escape anything. Quote Link to comment Share on other sites More sharing options...
shane85 Posted August 13, 2009 Author Share Posted August 13, 2009 wont let me do var dump...maybe because its an xml file? seems like any kind of code I put in just seems to screw up the file Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted August 13, 2009 Share Posted August 13, 2009 $ticker looks to me like php normal variable. No matter whats inside the variable it will be definitely dumped. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 13, 2009 Share Posted August 13, 2009 The XML Page Cannot Be Displayed Cannot view XML using style sheet Does your XML appear at all? Because Firefox/IE always throws a style error or one-another if you view the XML directly... try viewing the source, and DOUBLE CHECK the <rss> tags are correct, I doubt it's really a PHP problem unless your database/rss feed is not valid, the code seems to be... Quote Link to comment 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.