Solved

Why doesn't this query work?

Posted on 2013-01-25
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 83

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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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