Solved

Why doesn't this query work?

Posted on 2013-01-25
4
284 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

688 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