Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 7th, 2011, 03:34 PM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default sql.phpm

I will start by apologizing for not even knowing what to search for. So I'm asking a question that may have already been answered.

I am using the sql.phpm example out of your book "Professional PHP 5" from chapter 7. After adapting it to mysql and debugging it, I have beat my head against the wall over and over because I when I call GetRowHash(0), what I get back seems to be useless. It appears to be an array containing a result row. I try to access it using either:
Code:
$record = $sql->GetRowHash(0);
foreach ($record as $key => $value) {
	print $key ." = " . $value;
}
or
Code:
$ss = $record['ss'];
and I get nothing. Please help me to understand how I can use this result array properly. I have included the modified sql.phpm file.

Code:
<?php
class sql {
	private $result_rows;  # Result rows hash
	private $query_handle;  # db: the query handle
	private $link_ident;  # db: the link identifier
	private $num_rows;  # db: count rows returned

	public function __construct() {
		/* Define connection details to the MySQL server */
		define("DB_HOST", "localhost");
		define("DB_USER", "someuser");
		define("DB_PASS", "password");
		$db_name = "PCPD";
		$this->link_ident = mysql_connect(DB_HOST, DB_USER, DB_PASS) OR die("Cannot connect to MySQL server!");
		mysql_select_db($db_name);
	}

	public function Query($sql, $code_return_mode = 0) {
		$querytype = substr($sql, 0, strpos($sql, ' '));
		$result = mysql_query($sql)or die('Query failed: ' . mysql_error() . "<br />\n$sql"); ;

		if (!$result) {
			error_log("QUERY FAILED: $sql\n");
			return(0);
		}

		if ($querytype == 'INSERT' | $querytype == 'UPDATE' | $querytype == 'REPLACE' | $querytype == 'DELETE') {
			if (mysql_affected_rows() > 0) { // mysql_affected_rows() = INSERT, UPDATE, REPLACE or DELETE Statements
				$this->num_rows = mysql_affected_rows();
			}
		}
		else {
			if (mysql_num_rows($result) > 0) {
				$this->num_rows = mysql_num_rows($result); //Only SELECT Statements
				for ($i=0; $i <= mysql_num_rows($result)-1; $i++) { // mysql_num_rows() = SELECT Statements
					$resultarray = mysql_fetch_array($result);
					$return_array[$i] = $resultarray;
				}
				$this->result_rows = $return_array;
				return(1);
			}
			else {
				$this->num_rows = 0;
				return(0);  # return 0 if it fails
			}
		}
	}

	public function GetNumRows() {
		return ($this->num_rows);
	}

	public function GetResult($row_num, $column_name) {
		return ($this->result_rows[$row_num][$column_name]);
	}

	public function GetRowHash($row_num) {
		return ($this->result_rows[$row_num]);
	}

	public function GetTableHash() {
		return $this->result_rows;
	}

	public function Done($close_connection = 0) {
		if ($close_connection) {
			mysql_close($this->link_ident);
		}
	}
}
?>
Thanks, Bill
Reply With Quote
  #2 (permalink)  
Old March 7th, 2011, 05:29 PM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found that I called the query in the instantiation rather than in the actual function call sql->Query("SELECT * FROM..."). Stupid me!!!

Now it acts like an array and I can extract data normally.

Thanks,

Bill
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronization between SQL 2008 Express on local machine and SQL hosting server avidan ASP.NET 4 General Discussion 0 December 29th, 2010 12:31 PM
MySQL version for sql.phpm [Chapter 7] shegxzyl BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 0 December 2nd, 2009 01:57 PM
Chapter 15: usersession.phpm database conversion cashflowtips BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 August 9th, 2007 04:00 AM
Chapter 15: usersession.phpm mlange BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 12 May 4th, 2007 09:04 AM
Move SQL DB from one sql to another sql server Israr SQL Server 2000 3 January 24th, 2005 02:13 PM



All times are GMT -4. The time now is 07:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.