Jump to content

[SOLVED] mysql_num_rows


Lamez

Recommended Posts

I am having a problem with my marquee, it is suppose to check to see if there is anything in the database. Well it is not echoing anything out, because there is nothing in the database, but I setup a default message in case something like this happens. But my default message is not working. Here is my code:

 

<?php
$query = mysql_query("SELECT * FROM `marquee`");
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query);  
if($num_rows < '1'){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/114665-solved-mysql_num_rows/
Share on other sites

try removing the single quotes

<?php
$query = mysql_query("SELECT * FROM `marquee`");
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query);  
if($num_rows < 1){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}
?>

Im pretty sure you dont need '1' it should be just 1

 

like this

<?php
$query = mysql_query("SELECT * FROM `marquee`");
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query);  
if($num_rows < 1){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}
?>

make sure you are not getting any errors

<?php
$query = mysql_query("SELECT * FROM `marquee`") or die(mysql_error());
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query);  
if($num_rows < 1){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}
?>

 

also add mysql_error() to your connection strings to make sure you are connecting.

 

Ray

Thanks for the replys guys.

 

I did do the mysql_error after I posted this topic, and I am connect, and no errors.

 

I did do the var_dump($num_rows); and I got this: int(0)

 

You guys where asking about $status, here is the rest of the code:

<?php
$q = mysql_query("SELECT * FROM `site_status`");
$row = mysql_fetch_array($q);

$query = mysql_query("SELECT * FROM `marquee`");
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query); 
if($num_rows < 1){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}

if ($marquee['num'] == (2)){
$status = $marquee['mar_custom'];
}else{

$stat = $row['status'];
if ($stat == ('0')){
$status = $marquee['mar_nu'];
}
if ($stat == ('1')){
$status = $marquee['mar_basket'];
}
if ($stat == ('2')){
$status = $marquee['mar_foot'];;
}


}
?>
               <script type="text/javascript">
                  //<![CDATA[
                     document.write("<marquee>   <?php echo $status; ?></marquee>");
                  //]]>
               </script>

 

this is a included file in my head.php

gosh I feel retarted, once I posted the code I saw my else statement was in the wrong spot.

 

here is the fix:

<?php
$q = mysql_query("SELECT * FROM `site_status`");
$row = mysql_fetch_array($q);

$query = mysql_query("SELECT * FROM `marquee`");
$marquee = mysql_fetch_array($query);


$num_rows = mysql_num_rows($query); 
if($num_rows < 1){
$status = "This is the default marquee message, if you are the admin please login and change it.";
}else{

if ($marquee['num'] == (2)){
$status = $marquee['mar_custom'];
}

$stat = $row['status'];
if ($stat == ('0')){
$status = $marquee['mar_nu'];
}
if ($stat == ('1')){
$status = $marquee['mar_basket'];
}
if ($stat == ('2')){
$status = $marquee['mar_foot'];;
}


}
?>
              <script type="text/javascript">
                 //<![CDATA[
                    document.write("<marquee>   <?php echo $status; ?></marquee>");
                 //]]>
              </script>

 

since the else statement was in the wrong spot, it was setting $status to a empty variable. That is why I got nothing echoed out.

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.