Gnub Posted February 6, 2007 Share Posted February 6, 2007 Wondering if any of you guys can help. I have a problem with a little script that is supposed to return data from a database, and list it. I've set it up for now to just return a single item from a record, for testing purposes. The script returns nothing appart from a blank page. As far as im aware, the sql statement, and connection strings are fine. So im left thinking it's PHP related. The PHP script snippet is blow. Anyone here able to shine a light on this problem? <?PHP $mnuDayFrom = $_POST["mnuDayFrom"]; $mnuMonthFrom = $_POST["mnuMonthFrom"]; $mnuYearFrom = $_POST["mnuYearFrom"]; $mnuDayTo = $_POST["mnuDayTo"]; $mnuMonthTom = $_POST["mnuMonthTo"]; $mnuYearTo = $_POST["mnuYearTo"]; $sep = "-"; $DateFrom = $mnuYearFrom . $sep . $mnuMonthFrom . $sep . $mnuDayFrom; $DateTo = $mnuYearTo . $sep . $mnuMonthTo . $sep . $mnuDayTo; $sql = "Select `Teletext`.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date from `Teletext` Where DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; echo $DateFrom; if ($Row) { $sql = "Select Teletext.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date FROM <table> WHERE DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; $db = mysql_connect("Host", "Username", "Password"); mysql_select_db("Username",$db); $result = mysql_query($sql,$db); ?> <?PHP //List results while ($myrow = mysql_fetch_array($result)) { printf("Row Number: ", mysql_result($myrow,0,"Row_No")); } } ?> Quote Link to comment Share on other sites More sharing options...
jimmi8 Posted February 6, 2007 Share Posted February 6, 2007 Hi i dont see the $row variable getting set anywhere....if this varibale isnt set then your query isnt getting run. also why are you setting your $sql varibale with the same string twice? Quote Link to comment Share on other sites More sharing options...
only one Posted February 6, 2007 Share Posted February 6, 2007 wtf is the point with that if with the row variable? Quote Link to comment Share on other sites More sharing options...
hvle Posted February 6, 2007 Share Posted February 6, 2007 I see more than one 'error', please look at explaination in red ... $sql = "Select `Teletext`.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date from `Teletext` Where DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; // here $sql is not query, you need to query, and fetch row echo $DateFrom; // because you haven't done anything with that query, $Row is unset. The If never excuted if ($Row) { $sql = "Select Teletext.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date FROM <table> WHERE DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; $db = mysql_connect("Host", "Username", "Password"); mysql_select_db("Username",$db); $result = mysql_query($sql,$db); ... My advice is organize your code well, use good variable name. ie. $db should be $db_link so you know it was a connection. that's it for now Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 6, 2007 Author Share Posted February 6, 2007 Im chaning an existing setup, trying to simplify it. Hence my problem. Little documentation, and lots of code. $sql variable was set twice(by me) as a test, and as it isn't script critical. $sql is used in the "$result = mysql_query($sql,$db);" I will remove the $row to see the result. *** Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in *URL* on line 79 *** line 79 is the "while ($myrow = mysql_fetch_array($result))" Quote Link to comment Share on other sites More sharing options...
jimmi8 Posted February 6, 2007 Share Posted February 6, 2007 yeah exactly, maybe you need something like this: $db = mysql_connect("Host", "Username", "Password"); mysql_select_db("Username",$db); $sql = "Select `Teletext`.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date from `Teletext` Where DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; $query = mysql_query($sql); while($row = mysql_fetch_array($query) { $variable1 = $row['departuredate']; $variable1 = $row['another_example_row']; Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 6, 2007 Author Share Posted February 6, 2007 Used your code (jimmi8), with very small addaptations, and am getting a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource " on a certain line. Addaptations were, password/username etc etc. yeah exactly, maybe you need something like this: Code: $db = mysql_connect("Host", "Username", "Password"); mysql_select_db("Username",$db); $sql = "Select `Teletext`.* DATE_FORMAT(`DepartureDate`, '%a %D %b`) AS revised_date from `Teletext` Where DepartureDate BETWEEN '$DateFrom' AND '$DateTo' ORDER BY Price ASC;"; $query = mysql_query($sql); while($row = mysql_fetch_array($query) { $variable1 = $row['departuredate']; $variable1 = $row['another_example_row']; Quote Link to comment Share on other sites More sharing options...
jimmi8 Posted February 6, 2007 Share Posted February 6, 2007 have you run your query in the terminal/command line or phpmyadmin to see if your query is ok. Also ive noticed an error in my code....the close } for the while loop 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.