Page 2 of 10

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:35 am
by simonmlewis
All the error.log shows is this:

[text][Tue Sep 03 14:34:03.548408 2013] [ssl:warn] [pid 12192:tid 420] AH01909: RSA certificate configured for http://www.example.com:443 does NOT include an ID which matches the server name
[Tue Sep 03 14:34:03.636291 2013] [ssl:warn] [pid 12192:tid 420] AH01909: RSA certificate configured for http://www.example.com:443 does NOT include an ID which matches the server name
[Tue Sep 03 14:34:03.661293 2013] [mpm_winnt:notice] [pid 12192:tid 420] AH00455: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.1 configured -- resuming normal operations
[Tue Sep 03 14:34:03.661293 2013] [mpm_winnt:notice] [pid 12192:tid 420] AH00456: Server built: Feb 23 2013 12:42:00
[Tue Sep 03 14:34:03.661293 2013] [core:notice] [pid 12192:tid 420] AH00094: Command line: 'C:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Tue Sep 03 14:34:03.663295 2013] [mpm_winnt:notice] [pid 12192:tid 420] AH00418: Parent: Created child process 11460
AH00548: NameVirtualHost has no effect and will be removed in the next release C:/xampp/apache/conf/extra/httpd-vhosts.conf:46
[Tue Sep 03 14:34:03.948087 2013] [ssl:warn] [pid 11460:tid 308] AH01909: RSA certificate configured for http://www.example.com:443 does NOT include an ID which matches the server name
[Tue Sep 03 14:34:04.002358 2013] [ssl:warn] [pid 11460:tid 308] AH01909: RSA certificate configured for http://www.example.com:443 does NOT include an ID which matches the server name
[Tue Sep 03 14:34:04.028390 2013] [mpm_winnt:notice] [pid 11460:tid 308] AH00354: Child: Starting 150 worker threads.
[/text]

Sadly it means nothing to me.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:37 am
by simonmlewis
How do I set that:
; display_errors
; Default Value: On
; Development Value: On
; Production Value: Off

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:39 am
by Celauran
Lines beginning with a semicolon are commented out. Check beneath those lines for an uncommented value. If not, add

Code: Select all

display_errors On

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:40 am
by Celauran
I wouldn't worry about those warnings. A missing/invalid RSA cert on a development environment is not causing your issues.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:42 am
by simonmlewis
So is mine set correctly?
What on earth is causing it to not even be triggered in PDO??
As I say, if I use the old style of mysql_, it works. So it's not PHP disabling the code.

It works in test.php. But when I use it as an include, it fails.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:47 am
by simonmlewis
Notice: Undefined variable: pdo in C:\xampp\phpMyAdmin\site\includes\menu\home.inc on line 5

Fatal error: Call to a member function query() on a non-object in C:\xampp\phpMyAdmin\site\includes\menu\home.inc on line 5
Bingo. Error turned on and here it is. Maybe you can help me enable whatever I have done wrong.

This is the full script on menu/home.inc:

Code: Select all

<?php
echo "<div class='menuheader'>Categorías</div>";

$query = "SELECT email FROM admin WHERE type = 'admin'";
$result = $pdo->query($query);
var_dump($result); exit;
while ($row = $result->fetch(PDO::FETCH_OBJ)) {
    echo "$row->email<br/>";
} 

	?>

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:50 am
by Celauran
Alright, so $pdo isn't defined. The constructor is being called in dbconn, it's not throwing any exceptions, so it would seem it hasn't been included when this file runs. Have you checked the include order? Is dbconn being included before menu.inc?

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 8:52 am
by simonmlewis
Yes, almost 100 lines before, in index.php.

include "dbconn.php";

...

Code: Select all

<?php
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'sitename');

$sqlconn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ($sqlconn) {
        mysql_select_db(DBNAME);
}

try {
    $pdo = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
} catch (Exception $e) {
    echo 'Could not connect: ' . $e->getMessage();
}
?>
Is this bit still correct??
As I may have made a mess of this bit:

Code: Select all

......... ';dbname=' .........

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 9:19 am
by Celauran
That looks fine and would throw an exception if it couldn't connect. Do you have other queries in your index file prior to the inclusion of menu.inc?

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 9:22 am
by simonmlewis

Code: Select all

$cname= isset($_REQUEST['cname']) ? $_REQUEST['cname'] : null;

$sname= isset($_REQUEST['sname']) ? $_REQUEST['sname'] : null;
$c= isset($_REQUEST['c']) ? $_REQUEST['c'] : null;
$s= isset($_REQUEST['s']) ? $_REQUEST['s'] : null;
$h= isset($_REQUEST['h']) ? $_REQUEST['h'] : null;
$prodid= isset($_REQUEST['product']) ? $_REQUEST['product'] : null;

include "dbconn.php";

if(empty($_COOKIE['type']))
{
echo "<script language=JavaScript>
<!--

//Disable right click script III- By Renigade (renigade@mediaone.net)
//For full source code, visit http://www.dynamicdrive.com

var message=\"\";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function(\"return false\")
// --> 
</script>";
}  
?>
<script>
function precheck(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("srcHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/presearch.php?q="+str,true);
xmlhttp.send();
}
</script>
<script>
function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("search");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Search box not completed");
	// dialog message
	var alertMsg = "OOPS!\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
// -->
</script>

<?php
$page= isset($_GET['page']) ? $_GET['page'] : null;
$menu= isset($_GET['menu']) ? $_GET['menu'] : null;

function getPage()
  {
  $thispage="includes/".$_GET['page'].".inc";

  if (file_exists($thispage)) 
  {
   include $thispage;
  } 
  else 
  {
  echo "<meta http-equiv='Refresh' content='0 ;URL=/error'>";
  }
  }   
function getMenu()
{
$thismenu="includes/menu/".$_GET['menu'].".inc";
if (file_exists($thismenu)) 
  {
   include $thismenu;
  } 
  else 
  {
  echo "<meta http-equiv='Refresh' content='0 ;URL=/error'>";
  }
}
?>
Bear in mind, this works for the older mysql_ methods.
It's only while using PDO that it's failing.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 9:48 am
by Celauran
And there it is; scoping issue. $pdo does not exist within the scope of your getMenu() function. Pass it in as an argument.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 9:51 am
by simonmlewis
Sorry you have lost me. I've no idea how to do that, as never had to.
I'm not passing any variable through, I'm just querying it generally.
On another menu file, I will be passing thru a variable via $menu to query it specifically.

So don't know why I need to put PDO into the menu. Sorry.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 10:01 am
by Celauran
I'd recommend giving this a read if you're unclear on variable scope: http://php.net/manual/en/language.variables.scope.php

Your $pdo variable is defined in the global scope, outside the scope of that function. Within getMenu(), $pdo is undefined. Simply pass the object in as an argument like so:

Code: Select all

function getMenu(PDO $pdo) {
    // Same function logic here
}
Then call it thus:

Code: Select all

getMenu($pdo);
The initial PDO is a type hint; it tells the function to expect the variable $pdo to be an object of type PDO. The $pdo in the function signature sets the variable's name within the function itself.

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 10:05 am
by simonmlewis
Here lies a problem then.
I was told that I could use PDO and the old mysql_ at the same time, until I have the site full PDO converted.
From this, seems I cannot! Because PDO is in the script there.

All of that said, I am now getting this error:
[text]object(PDOStatement)#2 (1) { ["queryString"]=> string(44) "SELECT email FROM admin WHERE type = 'admin'" } [/text]

Re: New to PDO: basic script failing. Including ORDER BY...

Posted: Tue Sep 03, 2013 10:10 am
by Celauran
You can use both. You can refactor piecemeal. If you're at the stage where you're refactoring the menu, then you'll also need to update any code it relies on. That's how refactoring works.

What you're getting below doesn't look like an error, it looks like the output of var_dump.