Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   Web design and Programming (https://novahq.net/forum/forumdisplay.php?f=32)
-   -   Php? (https://novahq.net/forum/showthread.php?t=42373)

atholon 12-19-2008 08:58 PM

Php?
 
So does the newest version of PHP not go off of the last MYSQL connection? So you have to specify which link to use every time?

IcIshoot 12-20-2008 09:29 AM

by latest version do you mean the latest one they have released? or the latest one that servers are running...


my host uses php 5.2.6 with mysql 5.0.67-community and no, I don't have to specify a specific link for a mysql connection.

And according to the php site, that hasn't changed.

It tries to use the latest connection first if no link is specified. If it can't, then it tries to make a connection with a call to mysql_connect() with no arguments passed to it.

If that doesn't work then it throws the error.

atholon 12-20-2008 10:53 AM

Yeah, that is what I thought but for some reason my queries are not using the last link. It is weird.

PHP Version is 5.2.6

The link is created and the database is selected in a config file but for some reason some of the files that include it will not use the same link.

When I do a die or the queries that have warnings it says no database has been selected.

IcIshoot 12-20-2008 01:32 PM

are you calling mysql_select_db()?

ie:

Code:

<?php
mysql_connect("localhost", "myusername","password");
mysql_select_db("mydatabasename");

$result = mysql_query("SELECT * FROM tablename");


atholon 12-20-2008 02:53 PM

Yes in the config file which is included in each page.

atholon 12-20-2008 03:00 PM

Code:

<?
$GLOBALS['host']="localhost";
$GLOBALS['user']="root";
$GLOBALS['pass']="";
$GLOALS['connect']=mysql_connect("$GLOBALS[host]", "$GLOBALS[user]", "$GLOBALS[pass]") or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error());
mysql_select_db("test", $GLOBALS['connect']) or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error());
?>

That password and user obviously are not the same....for security reasons.

SilentTrigger 12-20-2008 03:05 PM

Thought I was keeping up with the updates but I'm apparently running 5.1.6 on my server heh

Anyways, don't understand what you mean by this "for some reason some of the files that include it will not use the same link."

Does that files include the connect to the database or what do they include?

Never mind. So you do not get any error's when running that code? Have you checked the loggs and made sure the log level is sufficent so that you will be noted when it errors out?

Why are you using $GLOBALS in such way? Globals should be avoided at all times as things as you discribe can happen and in that it is a serious security flaw, it is to be removed in future PHP releses anyways.

If you're issuing that code over and over then why not just type it in plain text directly then you've removed the flaw of GLOBALS

In other words, like this

PHP Code:

<?
test = mysql_connect("host", "user", "pass") or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error());
mysql_select_db("test", test) or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error());
?>


IcIshoot 12-20-2008 03:28 PM

have you ever used that method before?

never seen a mysql setup done that way..... I would have to do some testing.


what I would just do is in your config file make the normal connections, for get saving the link to a global then in the other php files just do the mysql query like normal.

Forget globals, the link isn't needed, your config file makes the connection so you should be all set. that should work with out you having to carry the connection link all over the place, in functions, etc.

atholon 12-20-2008 03:33 PM

It is because I didn't want to have to pass it to EVERY method as an argument.

I can just make a SQL Connection class that does the same thing.

SilentTrigger 12-20-2008 03:37 PM

I'm just not sure of what you're trying to achive. It soundes as if you ran that code each time you ware to gather info from the MySQL database.

Scott 12-20-2008 03:50 PM

1 Attachment(s)
Here is a pretty simple class I wrote long ago... I use it in every script it build now..

PHP Code:

<?
$ph = new phMySQL;
$ph->connect("localhost","USER","PASSWORD","DATABASE") or die("Could not connect to database server");

//############################################################################
//                                                                            phMySQL
//############################################################################
class phMySQL {

    var $link;

    //############################################################################
    //                                                                            connect
    //############################################################################
    function connect($host="localhost",$user="null",$pass="null",$database="null") {

        $this->link=mysql_connect($host,$user,$pass);
        unset($host,$user,$pass);

        If(($this->link) AND ($database) AND (mysql_select_db($database, $this->link))) {
            return True;
        }
        
        return False;
    }
    
    //############################################################################
    //                                                                            query
    //############################################################################
    function query($query="") {
        
        If(!$q=mysql_query($query, $this->link)) {
            
            $message="<b>Failed query in:</b> ".$_SERVER['PHP_SELF']."<br />".$query."<br /><br />".$this->error();
            Echo($message);

            return False;
            
        }
        return($q);
    }
    
    //############################################################################
    //                                                                            fetch_array
    //############################################################################
    function fetch_array($query="") {
        $query=mysql_fetch_array($query);
        return($query);
    }
    
    //############################################################################
    //                                                                            fetch_assoc
    //############################################################################
    function fetch_assoc($query="") {
        $query=mysql_fetch_assoc($query);
        return($query);
    }
    
    //############################################################################
    //                                                                            fetch_row
    //############################################################################
    function fetch_row($query="") {
        $query=mysql_fetch_row($query);
        return($query);
    }
    
    //############################################################################
    //                                                                            result
    //############################################################################
    function result($query="") {
        $query=$this->query($query);
        $result=$this->fetch_assoc($query);
        return($result);
    }

    //############################################################################
    //                                                                            num_rows
    //############################################################################
    function num_rows($query="") {
        $query=mysql_num_rows($query);
        return($query);
    }
    
    //############################################################################
    //                                                                            get_rows
    //############################################################################
    function get_rows($query="") {
        $query=$this->query($query);
        $result=$this->num_rows($query);
        return($result);
    }
    
    //############################################################################
    //                                                                            num_fields
    //############################################################################
    function num_fields($query="") {
        $query=mysql_num_fields($query);
        return($query);
    }

    //############################################################################
    //                                                                            last_id
    //############################################################################
    function last_id() {
        $result=$this->result("SELECT LAST_INSERT_ID()");
        return $result['LAST_INSERT_ID()'];
    }
    
    //############################################################################
    //                                                                            error
    //############################################################################
    function error() {
        return mysql_error($this->link);
    }
    
    //############################################################################
    //                                                                            errno
    //############################################################################
    function errno() {
        return mysql_errno($this->link);
    }
    
    //############################################################################
    //                                                                            escape_string
    //############################################################################
    function escape_string($str) {
        return mysql_real_escape_string($str, $this->link);
    }
    
    //############################################################################
    //                                                                            free
    //############################################################################
    function free($query="") {
        mysql_free_result($query);
    }
    
    //############################################################################
    //                                                                            disconnect
    //############################################################################
    function disconnect() {
        mysql_close($this->link);
    }
    

} //class

?>

Just fill out the top part where it says connect and then include it in your script...
PHP Code:

$qry=$ph->query("STATEMENT");
$result=$ph->result($qry);

$qry=$ph->query("STATEMENT");
while(
$result=$ph->fetch_assoc($qry)) {



I would advise you do what st said and not use GLOBALS unless there is some requirement.

atholon 12-20-2008 04:31 PM

You crack me up Scott I built a class similar hehe!

Just wasn't sure if maybe a setting was just wrong...


this is what i had:

Code:


<?php
class MySql
{
  var $host;
  var $dbUser;
  var $pass;
  var $connect;
  var $db;
 
  function MySql($iHost = "localhost", $iUser = "atholon", $iPass = "yousuck", $iDb = "site")
  {
    $this->host = $iHost;
    $this->dbUser = $iUser;
    $this->pass = $iPass;
    $this->db = $iDb;
   
    $this->connect = mysql_connect("$this->host", "$this->dbUser", "$this->pass");
    mysql_select_db("$this->db", $this-connect);
  }
 
  function selectDb($theDb)
  {
    $this->db = $theDb;
    mysql_select_db("$this->db", $this->connect); 
  }
 
  function getResultSet($queryString)
  {
    $query = mysql_query($queryString, $this->connect);
    return $query; 
  }
}
?>

I will use some of the stuff you suggested Scott
And thanks for the help you guys. :)

SilentTrigger 12-20-2008 04:55 PM

Ah now I understand what you're trying to do, unless what scott typed is something you didn't want to do, if so, I'm clueless again :|

However what you wrote and I wrote is basicly the same thing besides using globals to pass username and password aswell as host, not trying to put you down, just explaining why I was confused as what you wanted to do.

Haven't done much php in a year or two and haven't used the method scott posted above in ages, remember seeing it in vBulletin v1.x the first time and it's really vBulletin that got me interested in PHP, was digging in ASP before that. Oh and UBB with it's slow CGI, ugh!

The interesting thing is that it's done in a simular way in ABB robots, even if it's a bit less code to do the same in the robot (well not the same, don't use MySQL hehe, but the functionallity of it). Probebly because they are both based on C :) You do need to declare data holders such as in php is done by $blah = "blah"; and it's a String, in the robot you actually have to declare $blah as a string :(

atholon 12-20-2008 05:04 PM

I have a ton of php files that use queries, some are classes and some are just pages with functons.

The way I have it set up right now is like this:
index.php
news/index.php
tutorials/index.php
reviews/index.php

Is that a good way to do it or should I all make it part of a single index.php file?
I want the news index to be a class file along with all the other ones lke the tutorial index and reviews but I am not sure how to set it up.

I was thinking of using an array index like in SMF's index. Then it would just create the right object when an "action" is met. Like http://digitalhelpfiles.com/index.php?view=news etc..
Any ideas? Hope that makes sense.

SilentTrigger 12-20-2008 05:21 PM

Then I would probebly just put it like this:

index.php
news.php
tutorials.php
reviews.php

And in index.php get the data using $_GET gathering the info off the url and then phrashing the info doing the appropiate thing.

I might even of only used index.php and then just made a small section for each part (news, tutorials and such). Only issue with that is that it would be a very uncomprehandable file.

Or seperate the url grab with having news.php?date=blah and tutorials.php?program=photoshop and so on.

I would go with the last idea actually.

Index.php would just be a way to nest it all togather.

Oh and try to make sure that register_globals is set to off, unless it's a shared host you're forced to use whatever they have set it up as :(

Scott 12-21-2008 08:48 AM

I find that doing it all in one file can get messy.. Novahq is all done with includes off the index file. I've since moved to either

/index.php
/downloads.php
/downloads.php?action=categories
/tutorials.php
/tutorials.php?action=categories

or

/index.php
/downloads/index.php
/downloads/categories.php
/tutorials/index.php
/downloads/categories.php

and I plan to continue using thoes methods because they are the cleanest for me to work with and understand later when I come back to a script after a year or two. Which one I use depends on the size of the project and how many sections/sub-sections I need. For tghq I will probably use the second method.. For phphq.net I used the first :)

IcIshoot 12-21-2008 09:54 AM

in depends on your implementation of index.php as to how messy it gets.


I tend to use a combination of both


I use index.php to tie every thing together but use the individual files for the special cases.


index.php just includes the necessary files.

so a call to index.php?action=downloads will cause index.php to include the downloads.php file, thus providing that feature.


that way

A. you can set it up so that there is only one way into the site - you try going straight to download.php you get a message telling you no no.

Because of Vwar not using this method I had to go through 119+ php files to make it more secure, as their setup allowed for 119 potential ways to hack vwar.

B. You don't have to duplicate code - why have 5 pages that include a script for handling user sessions (logging in, logging off)? That is 5 pages that have potential conflicts, 5 pages that have to be updated if any changes have to be made...

Just seems like more work to not use index.php as your tie in.

index.php can handle your theme setup, sessions, cookies, security, setup the database connection, leaving your individual pages having to focus only on their job.


And index.php doesn't have to be as messy as it would seem with all of that - you divide it up into sub-scripts, just including them.

sample index.php

Code:

<?php
define('INSCRIPT', true);
include ('user_session.php');
//.... rest of index code

switch($_GET['action'])
{
case "downloads":
include('downloads.php');
break;
case "maps":
include('maps.php');
}

?>

then in downloads.php or maps.php you check to see if INSCRIPT is defined, if so proceed. if not, then some one is trying to come a back way.


I started using this method because of scripts like babstats, smf. Only draw back is url's have to be remembers as index.php?action=downloads, through through url rewriting or url forwarding you can set it up where one could go to /downloads/ and still be going through index.php...


just my 2 cents worth :D

atholon 12-21-2008 11:57 AM

I have a page that handles the common functionality of the pages. It is a class called masterpage.

atholon 12-21-2008 12:56 PM

Looks like the link issue must be related to my host's security settings...I just tried the newest version of PHP on my machine and it still works fine.

atholon 12-24-2008 10:11 AM

I decided to stick with the format I am using.

I just used the class to deal with SQL, it works out quite well and has some sweet methods so I cleaned up my code a lot. :)


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

Powered by vBulletin®