...

View Full Version : Resolved Keeps coming out as true!



Dat
09-14-2009, 10:24 PM
So what I want to do is check if there is a entry for a specified title already in the database, if there is no entry then output the form for it. otherwise just display the 'title' of the entry. Pretty simple i'd say

There are no mysql_errors();

The problem is it keeps coming out as "TRUE" and keeps outputting an empty 'title' or the listed title. Please help.


<?php
$query = "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);

if(!$result)
{
?>
<form>
The form
</form>
<?php
}
else
{
echo $data['title'];
}
?>SOLUTION:


<?php
$query = "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);

// Add this
$num = mysql_num_rows($result);
//Edit this
if($num == 1)
{
?>
<form>
The form
</form>
<?php
}
else
{
echo $data['title'];
}
?>

bacterozoid
09-14-2009, 10:28 PM
You forgot to do the query ;)

Edit: You also don't get any data...


<?php
$query = "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
$result = mysql_query($query) or die(mysql_error()); /// <--- add that line

if(!$result)
{
?>
<form>
The form
</form>
<?php
}
else
{
$data = mysql_fetch_array($result, MYSQL_ASSOC); // <---- That line, too
echo $data['title'];
}
?>

Dat
09-14-2009, 10:40 PM
You forgot to do the query ;)

Edit: You also don't get any data...


<?php
$query = "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
$result = mysql_query($query) or die(mysql_error()); /// <--- add that line

if(!$result)
{
?>
<form>
The form
</form>
<?php
}
else
{
$data = mysql_fetch_array($result, MYSQL_ASSOC); // <---- That line, too
echo $data['title'];
}
?>
I did i edited before you posted. I forgot, but it's there. Just assume I made a data connection already.

CFMaBiSmAd
09-14-2009, 10:45 PM
A query that returns zero rows is a successful query. $result will be a result resource with zero rows in it.

$result will only be FALSE if the query fails to execute (usually due to syntax errors...)

You need to use mysql_num_rows() to find out how many rows there are in the result set.

Dat
09-14-2009, 10:54 PM
A query that returns zero rows is a successful query. $result will be a result resource with zero rows in it.

$result will only be FALSE if the query fails to execute (usually due to syntax errors...)

You need to use mysql_num_rows() to find out how many rows there are in the result set.
Thank you

SOLUTION:


<?php
$query = "SELECT title FROM entry WHERE entry_id='$id' AND type='$type'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);

// Add this
$num = mysql_num_rows($result);
//Edit this
if($num == 1)
{
?>
<form>
The form
</form>
<?php
}
else
{
echo $data['title'];
}
?>

bacterozoid
09-14-2009, 10:58 PM
That's backwards of what you originally said.


if there is no entry then output the form for it

You would want to use


if($num == 0) // Notice the ==, not =. You need to do a comparison, not an assignment.

Dat
09-15-2009, 01:13 AM
That's backwards of what you originally said.



You would want to use


if($num == 0) // Notice the ==, not =. You need to do a comparison, not an assignment.
Hehe good catch sorry bout that, I edit the code for easier understanding on the forum.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum