/    Sign up×
Community /Pin to ProfileBookmark

Why Php Got Too Many Differnt Syntaxes As If Different Languages ?

Hi,

Folks,

Why some php code like this with the arrow:
->
New Version

[code]
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

$stmt = $mysqli -> prepare(‘UPDATE users SET name = ? WHERE id = ? LIMIT 1’);

$name = ‘Joe’;
$id = 1;

if (
$stmt &&
$stmt -> bind_param(‘si’, $name, $id) &&
$stmt -> execute() &&
$stmt -> affected_rows === 1
) {
echo ‘Updated’;
} else {
echo ‘Not updated’;
}
[/code]

While other codes are with the brackets:
Old Version

[code]
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);

$input_1 = ‘fat’; //keyword.

$stmt = mysqli_prepare($conn,”SELECT COUNT(id) from links WHERE keyword = ?”);
mysqli_stmt_bind_param($stmt,”s”,$input_1);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$output_1);
mysqli_stmt_fetch($stmt);

mysqli_stmt_close($stmt);
mysqli_close($conn);

printf(‘Found Result: %s’,$output_1);
[/code]

This is why I decided to move over to Python.
Php is one lang but yet so many different syntaxes.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 12.2022 — What brackets? I see no brackets in your post.

And why do you not recognize this for what it is?????????
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — Hi,

I am not used to the new version syntax of php.

I read here:

https://supunkavinda.blog/php/prepared-statements

These can fail:

mysqli_stmt_prepare()

mysqli_stmt_bind_param()

mysqli_stmt_execute()

mysqli_stmt_store()

mysqli_stmt_bind_result()

mysqli_stmt_fetch()

The tutorial gives this new syntax code to test for errors:
<i>
</i>
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

$role = 'admin';
$stmt = $mysqli -&gt; prepare('SELECT name, email FROM users WHERE role = ?');

if (
$stmt &amp;&amp;
$stmt -&gt; bind_param('s', $role) &amp;&amp;
$stmt -&gt; execute() &amp;&amp;
$stmt -&gt; store_result() &amp;&amp;
$stmt -&gt; bind_result($name, $email)
) {

<i> </i>while ($stmt -&gt; fetch()) {
<i> </i> echo "$name: $email &lt;br&gt;";
<i> </i>}

} else {
echo 'Prepared Statement Error';
}


Can someone be kind enough to show me how to convert this to old version ?

I am doing it like this:
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","buzz_off");

$input_1 = 'fat'; //keyword.

$sql = "SELECT name, email FROM users WHERE role = ?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_prepare($stmt,$sql))
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
elseif(!mysqli_stmt_bind_param($stmt,"s",$input_1)
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
elseif(!mysqli_stmt_execute($stmt))
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
elseif(!mysqli_stmt_store_result($stmt,$output_1))
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
elseif(!mysqli_stmt_bind_result($stmt,$output_1))
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
elseif(!mysqli_stmt_fetch($stmt))
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}
else
{

while (mysqli_stmt_fetch($stmt))
echo "$name: $email &lt;br&gt;";
}



Is above ok ?

Note, I wrote the mysqli_fetch() twice at the end. I have doubts about this.

Do read this small tutorial before giving answer.

https://supunkavinda.blog/php/prepared-statements

This tutorial also says to check for errors on these:

mysqli_stmt_prepare()

mysqli_stmt_bind_param()

mysqli_stmt_execute()

https://www.codegrepper.com/code-examples/php/mysqli+prepared+statement+error+handling

Thanks
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — Hi,

I made this other thread before this one. It is related to this one:

https://forum.webdeveloper.com/d/400393-which-insert-into-should-i-stick-to
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — @ginerjm#1645890

What do you mean recognise for what it is ?

I asked why two different syntaxes ?

New version uses: ->.
<i>
</i>$stmt -&gt; bind_param('si', $name, $id)

Old version uses: ()
<i>
</i>mysqli_stmt_bind_param($stmt,'si',$name, $id)


They are different syntax doing same thing. Why different syntax confusing the student ?
Copy linkTweet thisAlerts:
@ginerjmAug 12.2022 — You are comparing the OO method of using PDO vs. the procedural version. Why do you not see that? Have you read the manual for any of these functions where you would LEARN how to do this for yourself instead of pestering the forum with your lack of knowledge?
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — @nogdog

Care to input ?

I mean, should I be shortening it like this:
<i>
</i>if(!mysqli_prepare($stmt,$sql) OR (!mysqli_stmt_bind($stmt,"s",$input_1) OR (!mysqli_stmt_execute($stmt) OR (!mysqli_stmt_store($stmt) OR (!mysqli_stmt_bind_result($stmt,$output_1) OR (!mysqli_stmt_fetch($stmt))
{
echo 'Mysqli Error: ' .mysqli_stmt_error($stmt);
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno($stmt);
}


Which one you deem I should stick to ?

The above code ?

Or, the code here:

https://forum.webdeveloper.com/d/400394-why-php-got-too-many-differnt-syntaxes-as-if-different-languages/3
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — @ginerjm#1645895

I knew the -> was oop. But someone told me it is procedural style too. That confused me.

Thanks for clarifying.

Now, howabout answering my previous post ?
Copy linkTweet thisAlerts:
@ginerjmAug 12.2022 — I answered this. That's enough for anyone.
Copy linkTweet thisAlerts:
@novice2022authorAug 12.2022 — @ginerjm#1645899

No.

I can't go for the oop style cos I do not understand oop and I do not want to be just copy & pasting.

So I made 2 versions of the procedural style from the oop style.

Which one of these following two you say I should stick to ?

https://forum.webdeveloper.com/d/400394-why-php-got-too-many-differnt-syntaxes-as-if-different-languages/3

https://forum.webdeveloper.com/d/400394-why-php-got-too-many-differnt-syntaxes-as-if-different-languages/7
Copy linkTweet thisAlerts:
@ginerjmAug 13.2022 — Not interested.
Copy linkTweet thisAlerts:
@novice2022authorAug 14.2022 — @sempervivum

I guess you want me to ignore the code here:

https://forum.webdeveloper.com/d/400394-why-php-got-too-many-differnt-syntaxes-as-if-different-languages/3

But go for the one here to keep it DRY:

https://forum.webdeveloper.com/d/400394-why-php-got-too-many-differnt-syntaxes-as-if-different-languages/7

Yes ?
×

Success!

Help @novice2022 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...