Jump to content

Query Runs In Native Mysql But Not Through Php


EricRueter

Recommended Posts

I have a simple SQL Query that when run in PHPMyAdmin, or MySQL Workbench it produces the desired results. However when run via a PHP page, it returns nothing and does not produce an error. The problem -seems- to be the WHERE clause. If I remove the WHERE, the PHP version returns rows (obviously many more than desired). Any thoughts? Below is the SQL from the PHP page with the variable hard coded. Note - I have an extensive background with Oracle, but have only "played" with PHP and MySQL, so please do not laugh at my code...

// get the chosen value

$strEntry = "SELECT lbentries.LBEntryID

, lbentries.FirstName

, lbentries.LastName

, CONCAT_WS(', ',lbentries.LastName,lbentries.FirstName) AS RiderName

, lbentries.EntryHorseName

, lbentries.RideName

, lbentries.HorseAERCNumber

, lbentries.HorseAERCID

, lbentries.HorseAHANumber

, lbentries.RiderAERCNumber

, lbentries.RiderAERCID

, lbentries.RiderAHANumber

, lbentries.EntryNumber

, lbentries.EntryDivision

, lbentries.RideID

, DATE_FORMAT(lbrides.RideStartTime,'%H:%i') RideStartTime

FROM lbentries

JOIN lbrides ON lbrides.RideID = lbentries.RideID

WHERE lbentries.LBEntryID = 333";

$dbconnection = mysqli_connect($host,$user,$password,$database)

or die ("Could not sign on database - LeaderBoardDetailResults.php - strEntry - $strEntry");

$EntryResult = mysqli_query($dbconnection, $strEntry)

or die ("Could not execute query - LeaderBoardDetailResults.php - strEntry - $strEntry");

echo $strEntry;

echo ("Num Rows = ").mysqli_num_rows($EntryResult);

Link to comment
Share on other sites

SELECT lbentries.LBEntryID , lbentries.FirstName , lbentries.LastName , CONCAT_WS(', ',lbentries.LastName,lbentries.FirstName) AS RiderName , lbentries.EntryHorseName , lbentries.RideName , lbentries.HorseAERCNumber , lbentries.HorseAERCID , lbentries.HorseAHANumber , lbentries.RiderAERCNumber , lbentries.RiderAERCID , lbentries.RiderAHANumber , lbentries.EntryNumber , lbentries.EntryDivision , lbentries.RideID , DATE_FORMAT(lbrides.RideStartTime,'%H:%i') RideStartTime FROM lbentries JOIN lbrides ON lbrides.RideID = lbentries.RideID WHERE lbentries.LBEntryID = 333 Num Rows = 0 mysqli_result Object ( [current_field] => 0 [field_count] => 16 [lengths] => [num_rows] => 0 [type] => 0 )

 

This PHP page is called from another PHP page, which passes in the LBEntryID. The calling page is attached to the same DB (via an included connect) so I am fairly sure I am in the correct (or at least the same DB).

Link to comment
Share on other sites

Jessica: The "AS" keyword is optional, I'm afraid. So that shouldn't give any errors. Though, I agree that it should be added none the less, for no other reason than clarity.

 

Eric: Is that result from the page with the error condition, or the one that works? If the latter, please try the same from the page that has the problem, and post the results here. Preferably use the [code][/code] tags around the paste as well, as it'll help make it easier to read.

 

Never mind, just re-read the post and realized it was from the erroneous page.

Tried echoing out the database connection variables, or executed a test query against the table. To verify that there aren't anything going on with the DB connection details, or some strangeness about the data/query. Only thing I can think of now is that the DB user that you're using might not have the proper privileges to the database/table, and since you're not checking with mysqli_error () it's hard to say for sure. Error message might be hidden there, after all.

Edited by Christian F.
Link to comment
Share on other sites

The result listed is a cut and paste straight from the page. There is no error showing. I have echos in the code to show the exact SQL, the number of rows returned (Num Rows = 0) and the print_r that Jessica asked for. That seems to be the root problem is the SQL returns no rows in the PHP page - yet the same SQL returns one row elsewhere. I will be happy to include the entire PHP page if that will help. I am old and would like to save what little hair I have left..

Link to comment
Share on other sites

 // get variable after selecting something from the dropdown with name 'chooser'
 if (!empty($EntrySelected))
 {
 // get the chosen value
 $strEntry = "SELECT lbentries.LBEntryID
  				 , lbentries.FirstName
  				 , lbentries.LastName
  				 , CONCAT_WS(', ',lbentries.LastName,lbentries.FirstName) AS RiderName
  				 , lbentries.EntryHorseName
  				 , lbentries.RideName
  				 , lbentries.HorseAERCNumber
  				 , lbentries.HorseAERCID
  				 , lbentries.HorseAHANumber
  				 , lbentries.RiderAERCNumber
  				 , lbentries.RiderAERCID
  				 , lbentries.RiderAHANumber
  				 , lbentries.EntryNumber
  				 , lbentries.EntryDivision
  				 , lbentries.RideID
			    , DATE_FORMAT(lbrides.RideStartTime,'%H:%i') AS RideStartTime
			 FROM lbentries
                JOIN lbrides ON lbrides.RideID = lbentries.RideID
		    WHERE lbentries.LBEntryID = 333";
   $dbconnection = mysqli_connect($host,$user,$password,$database)
			    or die ("Could not sign on database - LeaderBoardDetailResults.php - strEntry - $strEntry");                  
   $EntryResult = mysqli_query($dbconnection, $strEntry)
			   or die ("Could not execute query - LeaderBoardDetailResults.php - strEntry - $strEntry");    
echo $strEntry;
echo ("Num Rows = ").mysqli_num_rows($EntryResult);        
print_r($EntryResult);           
   // keeps getting the next row until there are no more to get
   while ($row = mysqli_fetch_assoc($EntryResult))
   {
  extract($row);
     echo "<center>";
     echo "<b>RIDER -> </b>".$RiderName."&nbsp&nbsp&nbsp<b>  AERC NUMBER -> </b>".$RiderAERCNumber;
     echo "<br><b>HORSE -> </b>".$EntryHorseName."&nbsp&nbsp&nbsp<b>  AERC NUMBER -> </b>".$HorseAERCNumber;
     echo "<br><b>DIVISION -> </b>".$EntryDivision."&nbsp&nbsp&nbsp<b>  ENTRY NUMBER -> </b>".$EntryNumber;
     echo "<br><b>RIDE START TIME -> </b>".$RideStartTime;
     echo "<hr></center>";
   }

Link to comment
Share on other sites

You're sure it's the same database? IE, you don't have a prod/dev database either on same or different servers, with similar names? I had the same problem last week and after 2 hours of working on it figured out someone had made a copy of the DB with a misspelled name with one letter off, and in phpMyAdmin I was looking at the misspelled one (simply because it was first in the list!)

 

 

Link to comment
Share on other sites

This is from the calling page - it is a straight listing (table) of the LBEntries MySQL table (with a couple of other tables joined)

<?php
//  Common code and routines
   include("../SERAMySQL.php");
   require_once("../PHPUtility/PHPMySQLUtilities.php");
//  Make Database Connections  
   setDBconnections("leaderboard");      
?>                

and this is from the failing page

[code]
<?php
//  Common code and routines
   include("../SERAMySQL.php");
   require_once("../PHPUtility/PHPMySQLUtilities.php");
//  Make Database Connections  
   setDBconnections("leaderboard");      
?> 

 

Both pages are in the same directory...

 

In the setDBconnections I set the user/host/pwd/db to global variables

Link to comment
Share on other sites

This PHP page is called from another PHP page, which passes in the LBEntryID.

 

STOP or at least slow down.

 

Your actual php code is putting an external value into the query. You probably have some white-space/non-printing character in front of the value that is evaluating to a zero value. When you copy/paste the query, only the printing characters are copied.

 

Post the code for the page that is calling this page, showing the links/form that submits to this page.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

Your actual php code is putting an external value into the query. You probably have some white-space/non-printing character in front of the value that is evaluating to a zero value. When you copy/paste the query, only the printing characters are copied.

I think he said he hardcoded the 333 value in, but do you think if the 333 was copied/pasted from something it could have the non printing character still?

Link to comment
Share on other sites

         $Parm1 = $row['LBEntryID'];
         $Parm2 = $row['EntryNumber'];
  	   echo "<tr><td>";
  	   echo "<a href='http://localhost/LeaderBoard/LeaderBoardDetailResults.php?LBEntryID=$Parm1' target='_blank'>$Parm2</a>";

 

Ouch!!!! Did anyone else hear that loud noise? That was me removing my head from my ... well let's just say THANK YOU! notice where I am loading the new page from??? Not where it should come from THANK YOU --- fixed my problem ...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.