tags:

views:

49

answers:

3

I have posted this yesterday, I didn't get the the answers that could solve my problem but I got some pretty good ideas anyhow. This code retrieves the associated row from the db and translates it into PHP array so I could display it as a profile info. Here's my current code:

Query:

<?php

require 'includes/constants.php';

class newMysql {
    private $conn;

    function __construct() {
        $conn = mysql_connect(DB_SERVER_USERS, DB_USER_USERS, DB_PASSWORD_USERS, DB_NAME_USERS);
        if(!$conn) die('There was a problem connecting to the database.');
    }

    function get_profile(mysql_escape_string($lname)) {
        $query = "SELECT * FROM members WHERE lname = '".$lname."' ";
        $registerquery = mysql_query($query);
        if($registerquery){
            if (gettype($registerquery) == "resource") {
                if (mysql_num_rows($result) != 0 ) {
                    while ($row = mysql_fetch_assoc($registerquery)) {
                    $profile[] = $row;
                    }
                    return $profile;
                }
            }
            if(gettype($registerquery) == "boolean") {
                return "No array returned";
            }
        }
        else return "Query not successful".$registerquery;
    }
}

Display:

<?php

require 'classes/newMysql.php';

$mysql = new newMysql();

$profile = array();

$profile = $mysql->get_profile("lozano");

echo $profile;

Whatever I do, I get the message: "Query not successful". So the query does not return anything? It doesn't complain about db connection failure either. I tried the query directly from the CLI, it did return the expected row of data.

Any help pls...

+2  A: 

Hi, when your query is not successfull use mysql_error to know what happened.

http://php.net/manual/en/function.mysql-error.php

It should help you solve your problem quickly.

Edit : It seems you don't select a specific database in your code. You should do mysql_select_db($dbname, $conn); after your connection, and before querying.

Guillaume Lebourgeois
You're right. I got "No database selected" after adding it. Weird I've been using that same class all the time. And to think that connection is established from a construct there should be no changes of behavior between functions. Unless I am missing something. Login is even working right now. Gotta check it!
Joann
You're missing something : you have to do`mysql_select_db($dbname, $conn;)`After your connection, and before querying.
Guillaume Lebourgeois
A: 

Does

function get_profile(mysql_escape_string($lname)) {

actually work?

I'd have expected a parse error... it certainly objects on my local 5.2.8 installation

function get_profile($lname) {
    $lname = mysql_escape_string($lname)
Mark Baker
A: 

You might want to try this :

<?php

require 'includes/constants.php';

class newMysql {
    private $conn;

    function __construct() {
        $this->conn = mysql_connect(DB_SERVER_USERS, DB_USER_USERS, DB_PASSWORD_USERS, DB_NAME_USERS);
        if(!$this->conn) die('There was a problem connecting to the database.');
    }

    function get_profile($lname) {
        $query = "SELECT * FROM members WHERE lname = '".mysql_escape_string($lname)."' ";
        $registerquery = mysql_query($query, $this->conn) or die(mysql_error());
        if($registerquery){
            if (gettype($registerquery) == "resource") {
                if (mysql_num_rows($result) != 0 ) {
                    while ($row = mysql_fetch_assoc($registerquery)) {
                    $profile[] = $row;
                    }
                    return $profile;
                }
            }
            if(gettype($registerquery) == "boolean") {
                return "No array returned";
            }
        }
        else return "Query not successful".$registerquery;
    }
}

Don't use the or die construct I added in production though (better to log these errors and give a nice error message to your endusers).

wimvds