Weird MYSQL_FETCH_ARRAY error

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
PHP:
function display_nextSession() {
   $today = strtotime(date("m/d/y"));
   $sql = "SELECT * FROM nextsession WHERE date>='$today' LIMIT 1";
   $result = mysql_query($sql);
   $row = mysql_fetch_array($result);
      $nextDay = date("m/d/y", $row[1]);
echo $nextDay;
}
Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\mcs\sources\functions\home.php on line 13
[B]12/31/69[/B]
For some reason, the above function works on one page, but does not work on another. I get the error shown above.

Im first going to see if you can solve it without me having to put up the pages where the code is on, but if it is needed just reply, and I will get it up ASAP
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Sorry, this is quite an unusual error from what I can tell. More details may be necessary. I recommend switching to the mysqli family of functions.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
You code produces:

$sql == "SELECT * FROM nextsession WHERE date>='1253077200' LIMIT 1"

What sort of field is 'date' ? Did you store it as a INT? I believe mySQL stores the 'Date' type in YYYY-MM-DD format, not as a UNIX time. Since 'Timestamp' has the UNIX date range, that might be ok.
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
is the query returning no results?
I did var_dump(result); and got bool(false)

You code produces:

$sql == "SELECT * FROM nextsession WHERE date>='1253077200' LIMIT 1"

What sort of field is 'date' ? Did you store it as a INT? I believe mySQL stores the 'Date' type in YYYY-MM-DD format, not as a UNIX time. Since 'Timestamp' has the UNIX date range, that might be ok.

it is an INT(11). what do you suggest I do?
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
..deleted bad suggestion...

...Edit to add:

Below is what you want. Since $result is not a resource, it is because of an error, not because the query returned no results.
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
I would implement some error checking:

Code:
$result = mysql_query(.....); //your query goes here :P
if (!$result) {
  die(mysql_error());
}
if (mysql_num_rows($result) == 0) { //no rows match!
//do something else
}
else {
//now do what you were originally doing
}

Just make sure you get rid of the die(mysql_error()) part and handle it more gracefully when you go into launching your site so users don't get nasty errors.
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
I followed what garrettroyce said,
and it gave me this error:

No Database Selected.

Let me explain something first:
It works on the user end. But in the admin end I get a problem. The admin header includes the user header, but it just has a little extra code for the administrative actions. So technically if the user end can establish a connection, shouldn't the admin area be able too?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Without seeing all the code, I have no idea.

By the way, my car won't start. Can you tell me why?
 
Last edited:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
These are only parts and not the whole code.
PHP:

Header
PHP:
<?php if (!defined('ROOT')) die; 
require_once ROOT.'config.php';
require_once ROOT.'sources/language/setlanguage.php';
require_once ROOT.'sources/functions/home.php'; 
require_once ROOT.'sources/functions/gallery.php'; 
require_once ROOT.'sources/functions/textbook.php'; ?>

Config
PHP:
/* DB Connection */
$dbName = '';
$dbUsername = '';
$dbPassword = '';
$dbPort = 'localhost';
include ROOT.'connect.php';

Connect
PHP:
<?
// connect to the database server
if (!($db = mysql_pconnect($dbPort, $dbUsername , $dbPassword))){
  die("Can't connect to database server.");    
}else{
  // select a database
    if (!(mysql_select_db($dbName, $db))){
      die("Can't connect to database.");
    }
}
?>

Admin Header
PHP:
<?php
include ROOT.'sources/bits/admin/smf.php';
   if (!$context['user']['is_logged']) {
      header('location: '.ROOT.'admin/login/');
   }
$title = "Monmouth Chinese School Administration Panel";
include ROOT.'sources/bits/header.php';
?>  
   <div id="body"><div class="widthConstraint_body">
      <?php include ROOT.'sources/bits/topstrip.php'; ?>
      
      <div class="doublewhiteBox floatContainer">
         <div class="c-content">
            <div class="doublewhiteBox_head"></div>
            <div class="doublewhiteBox_body">

SMF
PHP:
<?php 
require_once ROOT.'config.php';
if ((!defined('ROOT')) && (!defined('HTTPDIR'))) die;
define ('SMFDIR', ROOT.'forum/');
define ('SMFSOURCEDIR', ROOT.'forum/Sources/');
include SMFDIR.'SSI.php';
   $_SESSION['login_url'] = HTTPDIR.'/admin';
   $_SESSION['logout_url'] = HTTPDIR;
?>



I do notice in the Admin Header, I can calling config twice. If that might screw up any of the db configurations.




And btw, a reason why your car is not starting is that someone could have stolen the battery or its dead.
 

Colony

New Member
Messages
11
Reaction score
0
Points
0
I think this should work.

$result = mysql_query($sql, $conn);

mysql_query takes 2 parameters.

edit: forgot you could use mysql_connect
 
Last edited:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
I think this should work.

$result = mysql_query($sql, $conn);

mysql_query takes 2 parameters.

edit: forgot you could use mysql_connect

well im actually using mysql_pconnect

but thanks anyways
Edit:
its alright guys.

I was right about my hunch. It was because I had included the config.php file twice, which I needed to do. So I just moved the connect.php out of config.php and into header.php.

a mod can close this now.
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
I think this should work.

$result = mysql_query($sql, $conn);

mysql_query takes 2 parameters.


edit: forgot you could use mysql_connect

$conn is an optional parameter and defaults to the last established connection
 
Top