Jump to content

MySQL num rows being unresponsive


canty

Recommended Posts

First off I know this will be a problem with my code but I honestly have looked at it for too long. If anyone can shed a bit of simple light on it that would be great.

 

$count should return about 8 or 9 or so but just returns 1 - I can only print out the number 1 if I do a direct echo of the mysql_num_rows() instead of putting it into the $count variable. Its really confusing, the database is all set up correctly as earlier on in the page I get all the information from the database and print out a table with it. There is something wrong here!

 

Problem only occurs when $count = mysql_num_rows($result1); is written.

 

//Following user presses button with name applyChanges.

if(isset($_POST['applyChanges']))
{
$result1 = mysql_query("SELECT * FROM music") or die ("Could not get result1");
$count = mysql_num_rows($result1) or die ("Could not count rows"); //Changed this to an echo without $count and prints a 1. - Should be greater than 8
echo "$count";

for($i=0; $i<$count; $i++)
{
	$query2 = "UPDATE music SET name='$name[$i]' WHERE path='$path[$i]'";
	$result2 = mysql_query($query2);
}
}

 

Thanks,

Matt

Link to comment
Share on other sites

I've edited the original for a bit more clarity, should this be an issue when the page is only about 30 lines or so. The code is efficient I hope. Besides I'm doing another mysql_query specifically for this bit. Would putting it at the top of the code help?

Link to comment
Share on other sites

Can you please copy this code to the top of your script, directly after the database connection is made.  Then post the output.

 

	$result1 = mysql_query("SELECT * FROM music") or die ("Could not get result1");
        print "<pre>"; var_dump($result1);
$count = mysql_num_rows($result1) or die ("Could not count rows"); //Changed this to an echo without $count and prints a 1. - Should be greater than 8
echo "Count: $count\n";
        exit;

Link to comment
Share on other sites

Can you please copy this code to the top of your script, directly after the database connection is made.  Then post the output.

 

	$result1 = mysql_query("SELECT * FROM music") or die ("Could not get result1");
        print "<pre>"; var_dump($result1);
$count = mysql_num_rows($result1) or die ("Could not count rows"); //Changed this to an echo without $count and prints a 1. - Should be greater than 8
echo "Count: $count\n";
        exit;

Result:

 

resource(3) of type (mysql result)

Count: 9

 

This looks promising but its getting late... The count is now right!

Link to comment
Share on other sites

Ok, so we've confirmed that your query and count code is correct.  My gut feeling is that you are actually seeing output from different code, not the code you are editing.  There are 2 approaches I use in this kind of situation, and you can use either or both:

 

1.  Keep adding print/var_dump statements until it becomes obvious where things are going wrong.  A good idea is to include some text to identify what part of the code is producing the output, so you don't spend hours debugging the wrong code (yes I've made that mistake many times!)

2.  If your script is not terminating, add "exit" at various points so you can confirm how far it gets before it hangs.  Then you can narrow down the problem area.

Link to comment
Share on other sites

Hi guys, thanks all for your help the solution was to put the count at the top straight after the query. I've got something else though, I have been building a site for a musician friend and they have hosting and domain with 34sp.com .... I've been having problems all the way through mostly due to permissions which I've never had with netoxide.co.uk.

 

Latest problem is this

<?php
$newPlaylist = "<?xml version='1.0' encoding='UTF-8'?><xml>";	
echo $newPlaylist;
?>

 

I have this running its own file called "string.php" and it does not run - $newPlaylist ="xml"; does work however. Never had a problem like this before, and I certainly remember it working when I first wrote it. What is going on?

Link to comment
Share on other sites

Yeah its just showed a blank page. I meant to write back to say that I have somewhat solved the problem... yet at the same time I have not!

 

Basically I have a system where my client can upload songs and rename them etc, this is all stored in an SQL database. When a user comes to listen to the songs on the website itself the player requires an XML file for the playlist which needed to be auto updated each time my client made changes.

 

Option 1 : Each user makes a new playlist.xml file when they visit the page - This caused problems with strings containing <?

Option 2 : Rewrite the XML file each time client makes a change - This now works. But the code is actually no different to that in Option 1.

 

My problems are solved, thanks very much for help. If anyone would like more info on a similar problem please don't hesitate to ask, I hope to help.

Link to comment
Share on other sites

Hmm, I tried your xml printing code and it worked for me, php version 5.2.0.  Did you try "view source" in your browser after accessing that script?  I've noticed when working with xml that some browsers will parse the xml and display it as a blank page if they think there's nothing to show.

Link to comment
Share on other sites

Option 1 : Each user makes a new playlist.xml file when they visit the page - This caused problems with strings containing <?

 

<? is the short version of <?php you may need to output those directly with php to avoid this

 

<?php echo '<?'; ?>

bit untidy.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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