Solved

Why doesn't this query work?

Posted on 2013-01-25
4
263 Views
Last Modified: 2013-01-26
I'm trying to piecemeal several articles I've read as well as the counsel I'm getting from the experts here and while I'm close, I'm still short of writing a successful query.

Here's my code so far:

<?php
error_reporting(E_ALL);
function SqlQuery($database,$sql){

$user="root";
$pass="";
$datasource="Showdown";
$server="localhost";

$dbase_user = mysqli_connect($server,$user,$pass);
mysqli_select_db($datasource, $dbase_user);

$query_results = mysqli_query($sql, $dbase_user);

return $query_results;

}

class Test {
public $the_first_name;
	function the_query(){
	$bruce=SqlQuery("","select * from registration where id='2545'");
	$this->the_first_name=$michelle['first_name'];
	}
}
 $edgar = new Test();
var_dump($edgar);
?>

Open in new window


The result is this: object(Test)#1 (1) { ["the_first_name"]=> NULL }

What am I doing wrong?
0
Comment
Question by:brucegust
4 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 38821425
I've been futzin around with this for a while.  Of course, this is basically how I make my money is by figuring these things out.  Put in your own info and try this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>brucegust mysqli</title>
</head>
<body>
<h1>brucegust mysqli</h1>
<?php
error_reporting(E_ALL);

function SqlQuery($database,$sql){

$dbhost = "10.202.46.41";    // Your database server 192.168.0.10
$dbuser = "eecontact";      // Your db username
$dbpass = "eazy4U";      // Your db password
$dbname = "nads";      // Your database name

$dbc = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

$query_results = mysqli_query($dbc,$sql);
$row = mysqli_fetch_assoc($query_results);
return $row;
}

class Test {
	public $the_first_name = "";
	function the_query(){
		$bruce = SqlQuery("","SELECT * FROM pdotest WHERE Pid='5'");
		return $bruce['value1'];
		}
	}

$testx = SqlQuery("","SELECT * FROM pdotest WHERE Pid='5'");
//var_dump($testx);
echo "<br><br>testx: ".$testx['value1']."";
$edgar = new Test();
//var_dump($edgar);
echo "<br><br>edgar: ".$edgar->the_query();

?>

</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 38822057
change:
$dbase_user = mysqli_connect($server,$user,$pass);
mysqli_select_db($datasource, $dbase_user);

Open in new window


to:
$dbase_user = mysqli_connect($server,$user,$pass , $datasource);
/* get rid of this => mysqli_select_db($datasource, $dbase_user); */

And also change:
		$bruce=SqlQuery("","select * from registration where id='2545'");
	$this->the_first_name=$michelle['first_name'];

Open in new window


to:
$bruce=SqlQuery("","select * from registration where id='2545'");
      $this->the_first_name=$bruce['first_name'];
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38822378
Most PHP functions have return values, and all of them are documented in the online man pages.  So before you write a statement like this one, have a look at the man page to see what the function arguments are, and what the expected return values will be.

$dbase_user = mysqli_connect($server,$user,$pass);

Man page: http://php.net/manual/en/function.mysqli-connect.php leads you to man page: http://php.net/manual/en/mysqli.construct.php where there are examples showing that you need four arguments for this function to work.

The function "returns an object which represents the connection to a MySQL Server" so your next step after running the PHP statement above will be this:

var_dump($dbase_user);

Then you will know immediately if you got a resource object or FALSE from the function call.

You might also want to read the PHP man pages about OOP5 so you can learn how to organize your function definitions into "class methods" instead of standalone procedural functions.  It's a lot to absorb in one sitting; be patient with yourself and do not get frustrated if it seems complicated at first!

This is untested code, but I believe it's fairly close to functional.  Hopefully the comments will provide some guidance.  Please post back if there is anything you don't follow.

<?php
error_reporting(E_ALL);

// VARIABLES NEEDED TO CONNECT AND SELECT
$user="root";
$pass="";
$datasource="Showdown";
$server="localhost";

// INSTANTIATE THE MySQLi OBJECT
$mysqli = new mysqli($server, $user, $pass, $datasource);

// TEST FOR ERRORS AND VISUALIZE ANY ERROR CONDITION
if ($mysqli->connect_error) 
{
    $msg 
    = 'MySQLi Connect Error: '
    . $mysqli->connect_errno 
    . ' '
    . $mysqli->connect_error
    ;
    error_log($msg);
    die($msg);
}

// SEE WHAT THE SQLI OBJECT LOOKS LIKE
var_dump($mysqli);

// CONSTRUCT A QUERY
$sql = "select * from registration where id='2545' ";

// RUN THE QUERY AND GET A RESULTS OBJECT IN $res
$res = $mysqli->query($sql);

// SEE WHAT THE RESULTS OBJECT LOOKS LIKE
var_dump($res);

// TEST FOR SUCCESS OR FAILURE 
if (!$res)
{
    $err
    = "QUERY FAIL: "
    . $sql
    . ' ERRNO: '
    . $mysqli->errno    
    . ' ERROR: '
    . $mysqli->error
    ;
    error_log($err);
    die($err);
}

// IF WE GET THIS FAR, WE CAN USE THE RESULTS OBJECT TO GET THE ROWS OF THE DATA SET
echo PHP_EOL . $res->num_rows . ' ROWS IN RESULTS SET' . PHP_EOL;
while ($row = $res->fetch_assoc())
{
    print_r($row);
}

Open in new window

Best regards, ~Ray
0
 

Author Comment

by:brucegust
ID: 38822898
Got it!

Thanks for the help guys. Ran into another snag which I have documented at http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28010030.html if you have the time.

Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now