I am using a simple test table to upgrade a website from mySQL to PHP7 which is outside of my control.
This is a test case for a user log in. I want the whole row not the userID. It contains users access levels and last time logged in to update. Otherwise I could have obtained a single field of the record.

[vode]<?
$pdo = connectDB();
$Name = "Diana";
$stmt = $pdo->query("SELECT * FROM PDO WHERE Name_F = '" . $Name . "'");
$count = $stmt->rowCount();
if($count ==1) // Will be one.
{
$row = $stmt->fetch())
echo $row['Name_F'] . "<br>";
echo "Music count = " . $count; // Should be 1
}
?>[/code]

What a rubbish object PDO class is.
I have written classes before with properties and methods. But I have never seen an object destroyed by calling a method. A call to stmt->fetch() only works if you don’t make a call to the method stmt->rowcount()
So you have to re-establixh the stmt object again as

<?
$pdo = connectDB();
$Name = "Marvin";
$stmt = $pdo->query("SELECT * FROM PDO WHERE Name_F = '" . $Name . "'");
$count = $stmt->rowCount();
if($count ==1)
{
	$stmt = $pdo->query("SELECT * FROM PDO WHERE Name_F = '" . $Name . "'");
	$row = $stmt->fetch();
	echo $row['Name_F'] . "<br>";
}
echo "Music count = " . $count;  // Should be 1
?>

I also noticed that nobody on this or any other forum knows what stmt stands for only replies like the name of a variable should be ...

I prefer to use $sql for an syl and $Qsql for the query of the sql.

otuatail rowCount()

if you bother to read the documentation for rowCount(), you will find that there's no guarantee that it will work for a query that returns a result set.

you should instead just attempt to fetch the data from the query and test the result of the fetch statement.

otuatail what stmt stands

It is an arbitrary variable name. You can call it $nogdog if you want to -- as long as you are consistent. It is being used in your example to store a PDOStatement object, in this case returned by $pdo->query(). (See the "Return Values" section of https://www.php.net/pdo_query .) Using the variable name $stmt is just an unofficial common practice, but you could give it whatever meaningful name you prefer.

    otuatail I also noticed that nobody on this or any other forum knows what stmt stands for only replies like the name of a variable should be ...

    Actually, @pbismad did say, as I pointed out last time you asked.

    otuatail
    Does s.t.m.t stand for anything. Can't be 4 random letters

    The post immediately before that said:

    pbismad
    The value is a PDOStatement object. Naming it $stmt is just a naming convention, so that anyone looking at the code will be able to deduce what it is, just like naming the PDO connection, $pdo, so that anyone looking at the code can deduce what database extension you are using.

      otuatail What a rubbish object PDO class is.
      I have written classes before with properties and methods. But I have never seen an object destroyed by calling a method. A call to stmt->fetch() only works if you don’t make a call to the method stmt->rowcount()
      So you have to re-establixh the stmt object again as

      It's so rubbish and you're the only person to realise this? If I found I had to do something so obviously stupid my first thought would be that I must be doing something wrong.

        pbismad if you bother to read the documentation for rowCount(), you will find that there's no guarantee that it will work for a query that returns a result set.

        Further, if you read the documentation for rowCount() you'd see it's for DELETE, INSERT, or UPDATE statements (which in SQL return the number of rows affected). Not SELECT, which returns a result set.

        https://www.php.net/pdostatement.rowcount

          Write a Reply...