Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

php / MySQL issue??

I have a site that I just moved from one account at GoDaddy to another. It worked fine at the old account. At the new account, it will not work.

Specifically, I get the following:

qry = SELECT * from homeowner where email = 'jboland11@cox.net' and password = 'Triumph'
res =

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/content/21/10338421/html/chk_login.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at /home/content/21/10338421/html/chk_login.php:14) in /home/content/21/10338421/html/chk_login.php on line 19

The source php file is attached.

I took the sql as displayed in the echo & ran it against the database using phpMySql. It works fine & returns one row in the result set.

What is going on??

Thanks
chk-login.php
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

It looks like you probably didn't change the server name because you are not getting a 'valid' resource returned.  And line 3 in that code is "localhosterror_reporting(E_ALL);" which is wrong.  Should be "error_reporting(E_ALL);", the 'localhost' is an error.
You probably want to get in the habit of using the full <?php tag instead of short open.  I'll see what else I can find... back in a few.
Avatar of Richard Korts

ASKER

To Dave Baldwin

That inclusion of localhosterror_report........ was an error; that is NOT included in the real code. I was changing the $Host variable to "localhost" to hide the REAL host; had the cursor in the wrong place.

The "real" GoDaddy host name is:

Hostname:
GBHAdb.db.10338421.hostedresource.com

I did a MASS replace of the numeric part (which was different on the "OLD" account). So I think it's right.
Here is the script with some comments and some man page links that may be helpful.  I think your script has to select the DB.  It's wise to use the escape() functions before running a query.  You're looking for one row, so you use the LIMIT query.

While this is untested code, I believe it will be correct in principle.  If you see anything you don't understand in here, please post back and I'll try to help.

Also, you probably want to begin converting to PDO or MySQLi -- MySQL is going away soon.

<?php // RAY_temp_rkorts.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
session_start();


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php
// MAN PAGE: http://php.net/manual/en/function.error-log.php


// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "GBHAdb";
$db_user = "GBHAdb";
$db_word = "xyz";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $err <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!mysql_select_db($db_name, $db_connection))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $err <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES

// MAKE EXTERNAL VARIABLES SAFE FOR USE IN QUERY
$e = mysql_real_escape_string($_POST['email']);
$p = mysql_real_escape_string($_POST['pwd']);

// CONSTRUCT THE QUERY STRING FROM SAFE VARIABLES
$qry = "SELECT * from homeowner where email = '$e' and password = '$p' LIMIT 1";

// IF mysql_query() RETURNS FALSE, LOG AND SHOW THE ERROR
$res = mysql_query($sql);
if (!$res)
{
    $err
    = "QUERY FAIL: "
    . $sql
    . ' ERRNO: '
    . mysql_errno()
    . ' ERROR: '
    . mysql_error()
    ;
    error_log($err);

    // HANDLE THE PROGRAMMATIC CONSEQUENCES HERE
    die($err);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS

// GET THE COUNT OF ROWS
$nr = mysql_num_rows($res);

// IF NO ROWS, REDIRECT
if ($nr == 0) 
{
	header("Location: index.php?bad=Y");
	exit;
} 

// COPY SOME VARIABLES INTO THE SESSION
$_SESSION['pwd'] = $_POST['pwd'];
$h = mysql_fetch_assoc($res);
$_SESSION['lot'] = $h['lot'];
if (intval($_SESSION['lot']) < 82) 
{
    $_SESSION['ccr'] = "pdf/Covenants_Units1&2.pdf";
} 
else 
{
    $_SESSION['ccr'] = "pdf/Covenants_Unit3.pdf";
}
$_SESSION['alast_used'] = time();
header("Location: home.php");
exit;

Open in new window

HTH, ~Ray
The "real" GoDaddy host name probably belongs in the code I posted at line 24.
To all,

I changed the opening tag to <?php. Same result.

If the host was wrong wouldn't the $con = mysql_connect ($Host, $User, $Password) fail?

Thanks,

Richard
To all,

To test the connection, I pasted in Ray's code & ran it again; same result.

I have attached all the REAL code EXCEPT the password.

Thanks,

Richard
chk-login.php
To all,

Do you think maybe the GoDaddy Database is NOT yet set up?

It let me upload all the tables (structure & data) using phpMyAdmin.

I guess I'll have to move it back to my other hosting environment (at 1 & 1) to make it work.

ARRRRRRRRRRRRRGH!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
From the most recent code snippet, it appears that you did not paste my code in, and it appears that there is still no error checking, and it appears that there is still no mysql_select_db() function call.  I'm not a fan of GoDaddy, but it looks like you have some more work to do before you decide it's their problem.
Ray may have the clue since if you moved to a 'new' hosting account, it will probably have PHP 5.3 and 'short_open_tag' may be off.
To know if PHP is at 5.3, etc., you can run this script (posted here in its entirety).

<?php phpinfo();

Open in new window

You guys were busy while I was talking to Godaddy.  Setting up databases on Godaddy usually takes about a 30 minutes most days.  However, until it is setup, you can not do anything with it.  So if you were able to load your database in phpMyAdmin, it is up and running.

I am responsible for 8 web sites on Godaddy on both Linux and Windows hosting.  I have no problems with Godaddy in maintaining those sites.  But then, the only reason I call them is for policy issues, not technical issues.
The issue was a comedy of errors, on my part.

First off, I had the old style mysql query (mysql_db_query) so when I first ran it, it gave me a depreciated error. So I changed it to mysql_query, but I FAILED to recognize that I had to ALSO use mysql_db_select to NAME the database. Based on Ray's last comments, I put that it and VWALLA, it works.

I have 3 million scripts (exaggeration) using the "old" style, Now you are saying I will have to convert MySQL to something else? Soon?

That's even worse.
Yes, it's going to be a big issue.  Please see the large red warning box on this page:
http://us3.php.net/manual/en/function.mysql-connect.php