New to PDO: basic script failing. Including ORDER BY...
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
How do I do a placeholder in "AGAINST" queries?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
Also for INSERTs, is this correct?
I read that it should be, but does PDO not use placeholders??
Code: Select all
$updatesite = $pdo->exec("INSERT INTO rating (prodid, stars) VALUES ('$myprod', '5')");Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
There's no escaping happening here. This is a bad idea. Use prepared statements with placeholders. The type of query doesn't change anything.simonmlewis wrote:Also for INSERTs, is this correct?I read that it should be, but does PDO not use placeholders??Code: Select all
$updatesite = $pdo->exec("INSERT INTO rating (prodid, stars) VALUES ('$myprod', '5')");
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
How do you do placeholders when the variables are like this: ('$myprod', .....) ? As you cannot do it like this: =:myprod.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
Code: Select all
$STH = $DBH->prepare("INSERT INTO foods ( name ) VALUES ( :name )");
$STH->bindParam(':name', $name);
$STH->execute();Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
That works. Or just pass in the array to execute.
Code: Select all
$query = "INSERT INTO foods ( name ) VALUES ( :name )";
$STH = $DBH->prepare($query);
$STH->execute(array(':name' => $name));-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
Like this.....
Code: Select all
$queryhit = "INSERT INTO usercomments (prodid, dateentered, nickname, prodname, usercomments, ipaddress, email) VALUES (:prodid, :todaydate, :nickname, :prodname, :usercomments, :ipaddressnew, :usercommentsemail)";
$stmt = $pdo->prepare($queryhit);
$stmt->execute(array(':prodid' => $prodid, ':todaydate' => $todaydate, ':nickname' => $nickname, ':prodname' => $prodname, ':usercomments' => $usercomments, ':ipaddressnew' => $ipaddressnew, ':usercommentsemail' => $usercommentsemail));Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
That looks right. What isn't working?
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
It is working - just wondered if I had done it right.
Can you look at this "average" issue please?
[text]Fatal error: Cannot use object of type stdClass as array [/text]
It's on the final line about number_format.
Can you look at this "average" issue please?
Code: Select all
$queryrating = ("SELECT prodid, AVG(stars) FROM rating WHERE prodid =:myprod GROUP BY prodid");
$rating = $pdo->prepare($queryrating);
$rating->execute(array(':myprod' => $myprod));
while ($rowrate = $rating->fetch(PDO::FETCH_OBJ))
{
$starrating = number_format($rowrate['AVG(stars)']);
}It's on the final line about number_format.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
As the error says, you're fetching an object (PDO::FETCH_OBJ) but trying to use it as though it were an array ($rowrate['AVG(stars)']). You'll need to either fetch an array (PDO::FETCH_ASSOC) or use object notation ($rowrate-> ). Speaking of which, why not name the result something more meaningful?
Code: Select all
$queryrating = "SELECT prodid, AVG(stars) AS average FROM rating WHERE prodid =:myprod GROUP BY prodid";-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
Good point about the naming.
$starrating = number_format($rowrate->AVG(stars));
This doesn't work either.
$starrating = number_format($rowrate->AVG(stars));
This doesn't work either.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
$rowrate->average will once you've cleaned up the naming.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
Why would this not work in PHP MY ADMIN:Celauran wrote:Same as usual.Code: Select all
$query = "UPDATE products SET hitcount = (hitcount + 1) WHERE id = :myprod"; $stmt = $sql->prepare($query); $stmt->execute(array(':myprod' => $whatever));
Code: Select all
UPDATE products SET hitcount = (hitcount + 1) WHERE id = '9'Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: New to PDO: basic script failing. Including ORDER BY...
That type of update works fine for me in version 4.0.4.1.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: New to PDO: basic script failing. Including ORDER BY...
It actually works now.
I think this is the cause. The field type was set to NULL as default. Some had numbers in them already (possibly thru forced testing).
This one had NULL.
I set all thsoe where the field was NULL to be 0, but it didn't work. Once I manually set it to be 0, it then began to work.
I think this is the cause. The field type was set to NULL as default. Some had numbers in them already (possibly thru forced testing).
This one had NULL.
I set all thsoe where the field was NULL to be 0, but it didn't work. Once I manually set it to be 0, it then began to work.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.