Cenobitez Posted December 2, 2006 Share Posted December 2, 2006 I don't seem to be able to get this to work right...Basically the user enters a URL like script.php?id=1 my script then ensures that the data is clean my only accepting numeric data, which i hope is enough, it then queries the db, for the record that matches that ID, and gets the ID's name, then displays the ID later in the script.All works find upto here, Now if someon enters an ID that doesnt exist, it still shows a blank later on in the script, so i want to check the ID, and no name exists (record doesnt exist), it then sets to the default ID of the time, which at the moment is 1, thenre-queries and gets the name of the backup item.I dont seem to be able to get this to work.[code=php:0]If (is_numeric($_GET['id'])) { $id = $_GET['id']; require('./mysql_connect.php'); // connect to the DB $query = "SELECT * FROM mod WHERE id='" . $id . "' LIMIT 1"; $result = mysql_query ($query); //run it $numrow = mysql_num_rows($result); if($numrow = 0) { //if all ok $id = '1'; //default id for now $query = "SELECT * FROM mod WHERE id='" . $id . "' LIMIT 1"; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row[name]; } //while ok 1 } else { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row[name]; } //while ok 2 } //numrow ok mysql_free_result($result); //free resources mysql_close(); //close db conn} //if numeric[/code] Where have i gone wrong, or is there a neater way to do this ?p.s. I hope to turn this into a re-useable function once its up and running, so my script needs to do this in one block :)p.s. Sorry if this should have been in the mysql area, but i figured was more a PHP issue :)Thanks Link to comment https://forums.phpfreaks.com/topic/29262-solved-setting-an-alternative-if-first-failed/ Share on other sites More sharing options...
chiprivers Posted December 2, 2006 Share Posted December 2, 2006 in this row:[code] if($numrow = 0) { //if all ok[/code]shouldn't it be:[code] if($numrow == 0) { //if all ok[/code] Link to comment https://forums.phpfreaks.com/topic/29262-solved-setting-an-alternative-if-first-failed/#findComment-134136 Share on other sites More sharing options...
Cenobitez Posted December 3, 2006 Author Share Posted December 3, 2006 Bugger, I always miss that one :(Unfortunately thats not the error :( I have just set $debugged = '1st';and $debugged = '2nd';after the 2 queries, and regardless of what id number i enter it stil says '1st'. however if i output the value of numrow it is set to 0 when no record and 1 when a record, so it has to be that if statement you pointed out. Going to try putting 's or "s around the 0, as its got to be something simple :([code=php:0]if($numrow == 0) { //if all ok[/code] Link to comment https://forums.phpfreaks.com/topic/29262-solved-setting-an-alternative-if-first-failed/#findComment-134141 Share on other sites More sharing options...
Cenobitez Posted December 3, 2006 Author Share Posted December 3, 2006 Well 3 hours later and 3:30AM, i got it :)My $debugged 2nd was inside the while statement (I accidentally moved it, when copying and pasting my code into the window here, and i missed a $result = mysql_query ($query); so the while statement never activated, making it always say 1st, once i fixed that, i emptied the IF statement and introduced each line one by one still it stopped work, then realised what i had done. I marked the missing line with //<<<<< THIS LINE MISSING >>>>> in its any use to anyone else. However i am not convinced this is the best way to do this :)[code=php:0]If (is_numeric($_GET['id'])) { $id = $_GET['id']; require('./mysql_connect.php'); // connect to the DB $query = "SELECT * FROM mod WHERE id='" . $id . "' LIMIT 1"; $debugged = '1st Pass'; $result = mysql_query ($query); //run it $numrow = mysql_num_rows($result); if($numrow == 0) { //if all ok $id = 1; //default id for now $query = "SELECT * FROM mod WHERE id='" . $id . "' LIMIT 1"; $result = mysql_query ($query); //<<<<< THIS LINE MISSING >>>>> $debugged = '2nd Pass'; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row[name]; } //while ok 1 } else { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row[name]; } //while ok 2 } //numrow ok mysql_free_result($result); //free resources mysql_close(); //close db conn} //if numeric[/code] Link to comment https://forums.phpfreaks.com/topic/29262-solved-setting-an-alternative-if-first-failed/#findComment-134183 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.