Jump to content

IF = BLANK, die, if valid, load page...HOW!?


shane85

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/170036-if-blank-die-if-valid-load-pagehow/
Share on other sites

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..

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

}

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

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";

 

}

 

?>

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.