Jump to content

[SOLVED] Unknown problem...PHP to SQL.


Gnub

Recommended Posts

 

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"));

 

}

}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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))"

 

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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'];

 

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.