LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   php mysql db code question (https://www.linuxquestions.org/questions/linux-server-73/php-mysql-db-code-question-508858/)

radiodee1 12-09-2006 12:13 PM

php mysql db code question
 
Does anybody know about php and mysql? I ran both on an old XP installation, and now I'm trying to move all the db files over to linux. I'm using Debian 3.1 and apache2... php 4 and mysql along with phpmyadmin. I moved my db files over from my windows partition, and can look at the contents of the files with phpmyadmin. This makes me think I moved them OK.

I start my php aplication from my browser using localhost. It seems my php application can read from the first table fine, even multiple times from the first table, then when the application tries to read for the first time from another table (in the same database) I get an error. mysql_select_db() has trouble selecting the database, so the next line, mysql_query() doesn't work.

Code:

//this is from the second file

mysql_select_db($DBname, $link); // this line fails
$Result = mysql_query($sql, $Link);

There is another wrinkle. The two tables are accessed in totally different files. I seperated the code in order to maintain order, but after the first table is accessed program control changes to a different file, and then the second table (from the same database) is accessed. I establish the link with mysql_connect() all over again in the second file.

I was working on XP with php5. I don't know if this is a problem. I am prepared to go through my code and change lines one by one (in fact I expect to) but only when it makes sense. I would like a php.ini setting or something that will make this work.:confused:

Wim Sturkenboom 12-11-2006 12:34 AM

And what is the error? One thing I notice is that you use $link and $Link.

radiodee1 12-11-2006 06:29 AM

The actual code looks like this:
Code:

        $Link = mysql_connect($host, $user, $password);
        if(!mysql_select_db($DBname, $Link))
        {
                print("'mysql_select_db()' Error Here in<br>\n");
                print("game_resume.php<br>\n");
                exit;
        }
               
        $sql = "SELECT * from pwdlist WHERE usernum = '0'";

        // use query with link
        if(!$Result = mysql_query($sql, $Link))
        {
                print("Removed Select Statement in game_resume");
                exit;
        }

my program prints the error on the 4th line; "mysql_select_db() Error Here in game_resume.php". game_resume.php is the name of the second file.

BUT it doesn't matter right now!! In an attempt to fix this problem I tried to reinstall php4, and in the process messed things up and now do not have working mysql at all. I apologize to anyone reading this. I really messed up!! Sorry.

Wim Sturkenboom 12-11-2006 10:15 PM

For when it's working again:
1)
You no not check the result of the connect. If it fails, you don't know and you pass an invalid connection to mysql_select_db.
2)
It's nice to have your own error message, but it does not tell you what goes wrong.
Use of mysql_error() is often more usefull.

radiodee1 12-12-2006 08:22 AM

I got php4 and mysql to work again, but the same exact problem as above appears.

If I take out the if() statements and the personalized error statements, the code looks something like this:

Code:

        $Link = mysql_connect($host, $user, $password);
        mysql_select_db($DBname, $Link);
               
        $sql = "SELECT * from pwdlist WHERE usernum = '0'";

        $Result = mysql_query($sql, $Link);

        $row = mysql_fetch_array($Result);

The error I get says this, and refers to the last line of the code.

Quote:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/rms/game_resume.php on line 47
I thought it might be my firewall settings, but after disabling the firewall, the problem was not fixed. Any help would be apreciated....

radiodee1 12-12-2006 11:15 AM

When I moved my database from Windows to linux I retyped the name of the database folder wrong. I changed an uppercase letter to a lowercase letter. Then when I was trying to get my php scripts to work I must have made the same change on some of my scripts in the declaration for the variable "$DBname". I have gotten everything to work now. ALSO, I tried mysql_error() and it was helpfull. Thanks to everyone who looked at this. Thanks Wim.


All times are GMT -5. The time now is 06:20 AM.