Jump to content

Simple SQL query help


Mindez

Recommended Posts

Hey.

I'm having trouble with my SQL.  Which is probably going to be something very simple that I'm getting wrong.  But here goes.

A friend of mine made some SQL include files for me, about 2 years ago.  And now, when I come to use them, it doesn't seem to work.  I've since lost touch with that friend, and I can't get it to work any more.

The include file contains:

[code] function SQL_GET($table, $section, $id, $idtable)
{
$db_object = mysql_connect($_ENV['server'], $_ENV['sql_user'], $_ENV['password']);
mysql_select_db($_ENV['database'],$db_object);
$result = mysql_query("SELECT * FROM $table");
if ($myrow = mysql_fetch_array($result)) {
do {
if (!$id == 0)
{
if ($id == $myrow[$idtable])
{
$temp = $myrow[$section];
return $temp;
}
}
else
{
$temp = $myrow[$section];
return $temp;
}
} while ($myrow = mysql_fetch_array($result));
return "ERROR";
} else {
return "ERROR";
}
mysql_close($db);
}[/code]

And the code I'm sending to it:

[code]for ( $for=0; $for<=10; $for=$for+1 ) {

$datetocheck = mktime(0, 0, 0, date("m"), date("d")-$for, date("Y"));
$checkdate = date("Y-m-d", $datetocheck);
echo $checkdate;
$endgetweekid = SQL_GET(images, weekID, date, $checkdate);
echo $endgetweekid;
}[/code]

But it's just returning:

[code]2006-11-25
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-24
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-23
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-22
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-21
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-20
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-19
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-18
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-17
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-16
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR2006-11-15
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 41
ERROR[/code]

(Line 41 is the if ($myrow = mysql_fetch_array($result)) {    line)


Can anyone see a problem?

I'm using MySQL version 4.1.10, etc.

Thanks.

EDIT: By the way, the weekID for date 2006-11-19 should be '6'.

EDIT: The four _ENV variables are defined earlier in the script.  Obviously.

-Mindez
Link to comment
https://forums.phpfreaks.com/topic/28469-simple-sql-query-help/
Share on other sites

[quote author=fenway link=topic=116300.msg473841#msg473841 date=1164505931]
Try adding or die( mysql_error() ) and see what happens.
[/quote]

I'm not quite sure exactly where you want me to add that.  I'll have a go though. :D

EDIT: Well, I added it like:

[quote] if ($myrow = mysql_fetch_array($result) or die(mysql_error())) {[/quote]

and it just displays:

[quote]2006-11-26
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\includo\SQL.php on line 40
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images'' at line 1[/quote]

('images' is the name of the SQL table.)

It doesn't display any more than just the one error.

EDIT: However, if I remove the quotes around the $table variable, it just says "No database selected" o_O;
Link to comment
https://forums.phpfreaks.com/topic/28469-simple-sql-query-help/#findComment-130332
Share on other sites

Thank you, muchly, for this.

I had two things wrong.  I didn't give my username any privileges in the SQL database (Which was a stupid mistake) and I had "$checkdate" and "date" the wrong way round.

Thanks to everyone that helped.  I'm glad I got it by myself (With a few hints).  Ah, the self-gratification.

:)
Link to comment
https://forums.phpfreaks.com/topic/28469-simple-sql-query-help/#findComment-130636
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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