• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Why doesn't this query work?

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
brucegust
Asked:
brucegust
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
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
 
hieloCommented:
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
 
Ray PaseurCommented:
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
 
brucegustPHP DeveloperAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now