Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax?

Status
Not open for further replies.

LWolf

Programmer
Feb 3, 2008
77
0
0
US
I am trying to grab records from a 'Requests' table by a date using an $id and $rDate variables. When the code is commented out the program works normally without grabbing the records. The problem is once the code, even just echoing a string, stops the display halfway through another loop. The question I have is does this code have a syntax error because I cannot determine the issue even when just displaying a string.

Code:
echo "SELECT * FROM `Requests` WHERE idEmployees = $id AND rDate = '$rMon'"; // bombs out

// above line displays this... SELECT * FROM `Requests` WHERE idEmployees = 14 AND rDate = '2015-09-14'

$qMon = $pdo->prepare("SELECT * FROM `Requests` WHERE idEmployees = $id and rDate = '$rMon'"); // bombs out (I have tried putting double and single quotes around everything)
 
Hi

Did you anything else with that displayed string, other than visually analyzing ? I mean, have you executed it in MySQL command-line tool, PHPMyAdmin or other client utility ? If yes, what was the result ?

What are the [tt]$pdo->errorCode()[/tt] and [tt]$pdo->errorInfo()[/tt] methods returning, when called immediately after that [tt]$pdo->prepare()[/tt] ?


Feherke.
feherke.ga
 
bombs out" isn't really all that explanatory.

What exactly happens when you echo your query first? In other words how is it bombing out?

Do you have display_errors turned on in your PHP.ini?

When developing, its essential to have it turned on.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I have a table that loops through users in a dept. Currently there are 3 users in the department giving me 3 iterations through the loop. The above string stops that loop given only 1 iteration. This is odd because
Code:
echo "SELECT * FROM `Requests` WHERE idEmployees = $id AND rDate = '$rMon'"
is simply echoing a string. Why is the code stopping the iteration? This "echo" is simply to display the string that is to query the db. The code below is the actual code that I am using to get the data, however, it is currently commented out to error trap the string.

Code:
//$qMon = $pdo->prepare("SELECT * FROM `Requests` WHERE idEmployees = $id and	 rDate = '$rMon'");
//$qMon->execute();



 
Can you post the loop?

Again, do you have display_errors turned on?

Are you sure that query is returning 3 rows? Because going by your WHERE clause, unless you have 3 employes with same idEmployee that would return only one employee each time its run assuming $id is being changed.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
It is in a nested loop, the entire coding for both loops is over 200 lines. I turned on display_errors and received got an error. The first line is the sql string then the error. Oddly it appears that it is after the first iteration leading me to think that it is now the 2nd iteration.

SELECT * FROM `Requests` WHERE idEmployees = 14 AND rDate = '2015-09-07'

Catchable fatal error: Object of class PDOStatement could not be converted to string in /var/ on line 327

The first user has a record in the requests table but the other 2 do not. Why would this error happen if it is only displaying a string and not actually retrieving from the database?
 
There you go, you have an error in your code.

What is in line 327 of schedule2.php?

What are you attempting to do with the PDOstatement there?

This is basic error tracking.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Line 327...
Code:
327  echo "SELECT * FROM `Requests` WHERE idEmployees = $id AND rDate = '$rMon'";

That is the line I am using to error trap the code beneath it...
Code:
328  //$qMon = $pdo->prepare("SELECT * FROM `Requests` WHERE idEmployees = $id and	 rDate = '$rMon'");
329  //$qMon->execute();
 
And where are you setting $id, and $rMon.

Basically the error is telling you one of your variables is a PDO object and you are trying to use it as a string, you can't.

If your $id is coming from a previous query, then you need to extract the actual results. Sounds like you are just passing a result object directly into your next query.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
As you can see from 3 posts prior the variables are being pulled. This nested loop will iterate 7 times for the same $id.
 
Nowhere do I see where your variables are getting set. They are just being used as part of your query string. Why this reluctance to post the rest of the code?

Where are you setting $id?

We can't help you if we can't see the rest.

The most I can tell you, is like I said, you seem to be assigning a PDO result object directly to one of your variables, and then trying to use it as a string.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Is there any difference in not wrapping '$rMon' in single quotes?

The pasted line 328 has some extra space within (between "and" & "rDate"). Are there disagreeable hidden characters in it that need to be cleansed?
 
Hi

If nothing else works, you may try the correct way too :
Code:
[navy]$qMon[/navy] [teal]=[/teal] [navy]$pdo[/navy][teal]->[/teal][COLOR=orange]prepare[/color][teal]([/teal][i][green]'SELECT * FROM `Requests` WHERE idEmployees = ? and rDate = ?'[/green][/i][teal]);[/teal]
[navy]$qMon[/navy][teal]->[/teal][COLOR=orange]execute[/color][teal]([[/teal][navy]$id[/navy][teal],[/teal] [navy]$rMon[/navy][teal]]);[/teal]

However normally this should make no difference, so better post the context in which those SQL query is used.

Feherke.
feherke.ga
 
spamjim said:
Is there any difference in not wrapping '$rMon' in single quotes?

Depends on what should be contained in the variables. If the values are strings, then for the DB to run the query they would need to be surrounded by the quotes. If they are numbers, then no, it does not make any difference.

The spaces should not affect much, and any hidden characters would not cause the PDO error shown above anyway.

The PDO error is explicitly stating that LWolf is trying to use a PDO object as a string. i.e its complaining it can't turn the object into text to echo out, and would similarly complain further down, when tried to be used in the followup query.

ie. LWolf is surely doing something like this:

Code:
$pdoconn = new PDO($dsn,$username,$passwd,$options);

$qry = "SELECT *FROM table";
[b][COLOR=#4E9A06]$results[/color][/b] = $pdoconn->query($qry);

[b][COLOR=#A40000]echo "This is a string: " . [COLOR=#4E9A06]$results[/color];[/color][/b] //This line will issue the same error LWolf is seeing.

Even if the query is only returning 1 row, with one column, it will still cause the same error.

LWolf would need to issue a fetch command to get to the actual data he wants for the followup query.

$row = $results->fetch(); Would need to be issued, in the example above.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Sorry for the delay, life gets in the way!

Ok, I have this line...
Code:
304 $rMon = $startDate;
which sets $rMon as a variable string.

$rMon == 2015-09-28
$id == 14 (pulled from the recordset)
Both $rMon and $id get displayed to the screen and neither are fields in the database.


Code:
327  echo "SELECT * FROM `Requests` WHERE idEmployees = $id AND rDate = '$rMon'";
...prints out this error
"Catchable fatal error: Object of class PDOStatement could not be converted to string in /var/ on line 327"

While
Code:
327  echo "SELECT * FROM `Requests` WHERE idEmployees = $id";
...does not throw an error. Obviously the issue is with the $rMon and date type. What am I doing wrong here?
 
Assuming $id is not a PDO object as you claim,
304 $rMon = $startDate;

How is $startDate getting set?

Keep working backwards, and outputting your variables to see exactly what they contain. The answer is still the same. Somewhere you are assigning a PDO object to a variable, and then trying to echo it out as a string.

Basically isolate your variables. you can use the die() method, to kill the script so nothing else runs after that point, that will make it easier to debug.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I can remove the
Code:
rDate = $rMon
from the string and it does not throw an error. So if there is a value for $rMon (2015-10-05) is this not an issue of converting this string to a date (which is what the database field is)?
 
It has nothing to do with what the database field format is.

Its what you are doing to assign a value to the php variable.

How are you assigning the value? As I have already pointed out, if your are getting the value out of a result set form running a query, are you issuing a fetch command, or simply trying to assign the value from a query() or execute() call?

What exactly does your database call that fills this variable look like?



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I was finally able to bipass the issue altogether and got through that section. However now the results are not showing correct. For example...
Code:
$qTh = $pdo->prepare("SELECT * FROM Requests WHERE idEmployees = $id AND rDate = '$rThu'");
$qTh->execute();
echo "SELECT * FROM Requests WHERE idEmployees = $id AND rDate = '$rThu' <br>";
echo $qTh->rowCount() . "<br>";

Displays this to the screen...
SELECT * FROM Requests WHERE idEmployees = 14 AND rDate = '2015-10-08'
1
SELECT * FROM Requests WHERE idEmployees = 12 AND rDate = '2015-10-08'
0
SELECT * FROM Requests WHERE idEmployees = 13 AND rDate = '2015-10-08'
0

The problem is idEmployees 14 has no record, it is under idEmployees 12. I ran the SQL in the db and it outputted correctly. Why would this result be happening?
 
Oh crap....disregard!! It was a copy and paste error!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top