Germaris Posted January 15, 2013 Share Posted January 15, 2013 (edited) Hi there! This query : PHP Code: SELECT img_name FROM $table WHERE email = '$email' AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&error=Failed !"); is supposed to return a 2 element array (as I already know the content of $table satisfying to the query statements) "ok" is a column which content is 1 (valid image) or 0 (invalid image) there's 3 rows containing the required $email and only two where ok='1' Statement PHP Code: if ( mysql_num_rows($sql) > 0) { while ($row =mysql_fetch_array ($sql)) { $array .= $row['img_name']."|"; } print "&numberOfRows=".mysql_num_rows($sql)."&myArray=".$array; } else { print "&error=No images found."; } returns a wrong number of rows (3 instead of 2) but a right array !!!??? Why? Many thanks for your advices, remarks and suggestions! Edited January 15, 2013 by Germaris Quote Link to comment Share on other sites More sharing options...
cpd Posted January 15, 2013 Share Posted January 15, 2013 Run your current query but include: SELECT img_name, email, ok .... Dump the results and review what's been retrieved Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2013 Share Posted January 15, 2013 It would also help if you posted the actual output you got, as that would give us the same information that you saw to use as a starting place. Quote Link to comment Share on other sites More sharing options...
Germaris Posted January 15, 2013 Author Share Posted January 15, 2013 Thanks PFMaBISmAd and cpd for responding. Actual output is as I wrote: numberOfRows : 3 myArray : two image names (as expected) Fields in my Flash application display the right information about the array and in the expected order : element1|element2| with no extra space and nothing after the last "|" glyph To "cpd" = I don't understand how useful will be to select the "email" column in the query. Would you please explain? Best regards to both of you! Quote Link to comment Share on other sites More sharing options...
cpd Posted January 15, 2013 Share Posted January 15, 2013 (edited) You have 2 conditions in your query which both have to be met for a result to be returned. MySQL won't decide to return something that doesn't match so the short answer is you have rows in your table that meet the criteria. By selecting the email and ok columns you'll prove this and be able to begin working out what rows match the conditions. This isn't a permanent thing, its just to get you to realise what's happening. Edited January 15, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2013 Share Posted January 15, 2013 (edited) Since you didn't provide any of the requested and actual information that we would need to help you, I'm going to guess that the code you posted isn't the actual and complete code that is producing the incorrect results, since it would be impossible for that code to output what you state. Edited January 15, 2013 by PFMaBiSmAd Quote Link to comment Share on other sites More sharing options...
Germaris Posted January 15, 2013 Author Share Posted January 15, 2013 (edited) Grrr... Some terms in the script are in french, that's why I didn't post everything... Here is the actual and full script for this function excerpt from the general script (2500 lines!!!) <?php function photo_history_bypaths($courriel) { $table = "_files"; $courriel = trim($courriel); $sql =mysql_query("SELECT img_name FROM $table WHERE email = '$courriel'AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&erreur=Échec de la requête !"); if ( mysql_num_rows($sql) > 0) { while ($row =mysql_fetch_array ($sql)) { $flashstr .= $row['img_name']."|"; } print "&erreur="."&nombre=".mysql_num_rows($sql)."&liste=".$flashstr; } else { print "&erreur=Pas de photos présentes pour cet utilisateur."; } } ?> The script works nice. My only problem is: Why it outputs a wrong number of rows (3 instead of 2)? I tried the query in phpMyAdmin with known parameter (my actual email) and it outputs two rows with the expected image names. Edited January 15, 2013 by Germaris Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 As was suggested, try adding the other column names so you can see what the three rows are. I'm curious what it would look like if you changed it to this: <?php function photo_history_bypaths($courriel) { $table = "_files"; $courriel = trim($courriel); $sql =mysql_query("SELECT img_name FROM $table WHERE email = '$courriel'AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&erreur=Échec de la requête !"); if ( mysql_num_rows($sql) > 0) { print "&erreur="."&nombre=".mysql_num_rows($sql)."&liste=".$flashstr; } } ?> Quote Link to comment Share on other sites More sharing options...
Germaris Posted January 15, 2013 Author Share Posted January 15, 2013 I'm curious what it would look like if you changed it to this: <?php function photo_history_bypaths($courriel) { $table = "_files"; $courriel = trim($courriel); $sql =mysql_query("SELECT img_name FROM $table WHERE email = '$courriel'AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&erreur=Échec de la requête !"); if ( mysql_num_rows($sql) > 0) { print "&erreur="."&nombre=".mysql_num_rows($sql)."&liste=".[b]$flashstr[/b]; } } ?> It couldn't work because you have first to fetch the result and, foremost, you should define the $flashstr variable... And even if you define it, this couldn't work that way... Thanks any way for responding! Question remains: Why the script outputs a wrong number of rows (3 instead of 2)? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 15, 2013 Share Posted January 15, 2013 Seriously dude, I'm working with the code you gave us. You do NOT have to fetch a row before getting the number of rows. http://us2.php.net/mysql_num_rows I'm suspecting that is messing up your count, the fact that you're trying to do it after using the rows. We can't tell you why it's not showing what you expect because you refuse to do ANYTHING we suggest. I'm done. Quote Link to comment Share on other sites More sharing options...
shlumph Posted January 15, 2013 Share Posted January 15, 2013 That's what I'm suspecting, too. Try this: <?php function photo_history_bypaths($courriel) { $table = "_files"; $courriel = trim($courriel); $sql =mysql_query("SELECT img_name FROM $table WHERE email = '$courriel'AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&erreur=Échec de la requête !"); $rows = mysql_num_rows($sql); //Only call this function once, before using mysql_fetch_array if ( $rows > 0) { while ($row =mysql_fetch_array ($sql)) { $flashstr .= $row['img_name']."|"; } print "&erreur="."&nombre=".$rows."&liste=".$flashstr; } else { print "&erreur=Pas de photos présentes pour cet utilisateur."; } } ?> Quote Link to comment Share on other sites More sharing options...
cpd Posted January 15, 2013 Share Posted January 15, 2013 Just to echo Jessica's comments. You've done nothing anyone seems to have suggested and given little information about the problem. I've given you a direction to start with and you've not perused it at all... PFMaBiSmAd pointed stuff out and you just pasted more code without any output from your query. See the link in Jessica's signature "How To Ask Questions": read it, understand it, and if you don't like it don't ever expect a good reply. Quote Link to comment Share on other sites More sharing options...
Germaris Posted January 16, 2013 Author Share Posted January 16, 2013 05:00AM... Tough to read... Oh là là... Please, cool it! Hey, fellows, this isn't a war... We all are on a forum to exchange our points of view and opinions. Nothing more, nothing less... To "Jessica": I never stated you must fetch before to get the number of rows. I stated you must fetch before to get some data for the variable "$flashstr". Did you give your script a try? Me too I'm curious to see the output you got. Despite what you have written, I gave a try to your script. It didn't work, exactly because of the reason I told you : You didn't define the variable "$flashstr". To "cpd" and "Jessica": As my project (for this function) is at its very beginning, the table contains only a dozen of rows. I know their content almost by heart... I'm working on my own server and have full access to my databases and all their tables. So, it isn't difficult for me to see if the outputs I get are okay or not. I gave a try to your suggestion to add columns to SELECT and, as I predicted, it didn't change anything. To "PFMaBiSmAd": My first posting contained all which was necessary to determine if the script should work or not. I worked meanwhile you posted and didn't necessary have the time to post all the verifications I did. And I also have, like all of you, a familily life. Even at the age of 72. It didn't mean I was ignoring advices and suggestions from the members partipating to this discussion. To "shlumph": Your script is very well wrote and worked nice. I gave it a try replacing the sent variables by their actual values. It comforted me in my opinion that my own script was good. Conclusion: After having the proof that my PHP Script was good, I made a deep investigation of my Flash .fla file. Debugging shown that Flash had a misinterpretation of the PHP output it received. The error was in Flash, not in PHP!!! And the error was due to a human error. Mea culpa. For who is still interested, look at my PHP Test in action and the script at the end of this post. You can see the results at: http://www.notre-annuaire.com/strict/photos.php I want to thank you all very much for your interest and the time you dedicated to my problem even if I had the sensation some were irritated by my apparent lack of interest for their posts. Best regards. PS: English isn't my native language, please excuse my mistakes. ---------------------------------------------------------------------------- My Test script as follows: <?php include "key.php"; $link = mysql_connect( $host ,$user ,$pass); if (!$link) { die("&erreur=Connexion impossible avec le serveur : " . mysql_error()); } $db_selected = mysql_select_db( $db, $link); if (!$db_selected) { die ($db." inutilisable : " . mysql_error()); } $table = "_files"; $courriel = "myemailaddress@xxxxxx.com"; // here, of course, my real email address $sql =mysql_query("SELECT * FROM $table WHERE email = '$courriel'AND ok = '1' ORDER BY id DESC LIMIT 0 , 3") or die("&erreur=Échec de la requête !"); if ( mysql_num_rows($sql) > 0) { while ($row =mysql_fetch_array ($sql)) { $flashstr .= $row['img_name']."|"; } print "&erreur="."&nombre=".mysql_num_rows($sql)."&liste=".$flashstr; } else { print "&erreur=Pas de photos présentes pour cet utilisateur."; } ?> 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.