开发者

Mysql resource into PHP array not working

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;
                }
           开发者_StackOverflow社区 }
            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...


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.


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)


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜