×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Syntax?

Syntax?

(OP)
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) 

RE: Syntax?

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 $pdo->errorCode() and $pdo->errorInfo() methods returning, when called immediately after that $pdo->prepare() ?

Feherke.
feherke.ga

RE: Syntax?

"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

RE: Syntax?

(OP)
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(); 



RE: Syntax?

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

RE: Syntax?

(OP)
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/www/Ultra/Schedule/schedule2.php 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?

RE: Syntax?

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

RE: Syntax?

(OP)
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(); 

RE: Syntax?

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

RE: Syntax?

(OP)
As you can see from 3 posts prior the variables are being pulled. This nested loop will iterate 7 times for the same $id.

RE: Syntax?

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

RE: Syntax?

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?

RE: Syntax?

Hi

If nothing else works, you may try the correct way too :

CODE --> PHP ( fragment )

$qMon = $pdo->prepare('SELECT * FROM `Requests` WHERE idEmployees = ? and rDate = ?');
$qMon->execute([$id, $rMon]); 

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

Feherke.
feherke.ga

RE: Syntax?

Quote (spamjim)


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";
$results = $pdoconn->query($qry);

echo "This is a string: " . $results; //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

RE: Syntax?

(OP)
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/www/schedule2.php 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?

RE: Syntax?

Assuming $id is not a PDO object as you claim,

Quote:


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

RE: Syntax?

(OP)
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)?

RE: Syntax?

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

RE: Syntax?

(OP)
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?

RE: Syntax?

(OP)
Oh crap....disregard!! It was a copy and paste error!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close