PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Error using prepared statements in PHP
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Error using prepared statements in PHP

Réponse
 
LinkBack Outils de la discussion
Vieux 17/02/2008, 11h19   #1
kodaliece@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Error using prepared statements in PHP

Hi,

I am using prepared statements and executing the below small script.

<?php
$username="root";
$password="admin";
$database="test";

// 'Connect to database'
$con = mysql_connect('localhost', $username, $password, $database) or
die(mysql_error());

if (!($con))
{
echo "<BR>Cannot connect to datbase. Please try again Later...";
}
else
{
$user_query = "SELECT first_name, UserImage FROM userdata WHERE
loginid = ? AND password = ?";

$statement = $con->prepare($user_query); // This line is ging the
error
$statement->bind_param("ss", $_POST['login_id'], $_POST['password']);

$result = $statement->execute();


// 'Close database connection'

if (mysql_close($con))
echo "<BR>Database connection closed";
else
echo "<BR>Failed to close database connection";

}
?>

But I get the error:
Fatal error: Call to a member function prepare() on a non-object in C:
\Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
17.

Can any please me on this.

Thanks
Mohan
  Réponse avec citation
Vieux 17/02/2008, 12h11   #2
Álvaro G. Vicario
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

*** kodaliece@gmail.com escribió/wrote (Sun, 17 Feb 2008 03:19:06 -0800 (PST)):
> $con = mysql_connect('localhost', $username, $password, $database) or
> die(mysql_error());

[...]
> $statement = $con->prepare($user_query);


> Fatal error: Call to a member function prepare() on a non-object in C:
> \Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
> 17.


If $con is the return value of mysql_connection(), then it can't have a
method called prepare(), mainly because it's not even an object:

usage:
resource mysql_connect ( [string server [, string username
[, string password [, bool new_link [, int client_flags]]]]] )

Return Values
Returns a MySQL link identifier on success, or FALSE on failure.

My best guess is that you want to use PDO. Then you need to create an
instance of a PDO object (adapt the example to your code):

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");

More info: http://www.php.net/pdo



--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--
  Réponse avec citation
Vieux 17/02/2008, 14h27   #3
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

kodaliece@gmail.com wrote:
> Hi,
>
> I am using prepared statements and executing the below small script.
>
> <?php
> $username="root";
> $password="admin";
> $database="test";
>
> // 'Connect to database'
> $con = mysql_connect('localhost', $username, $password, $database) or
> die(mysql_error());
>
> if (!($con))
> {
> echo "<BR>Cannot connect to datbase. Please try again Later...";
> }
> else
> {
> $user_query = "SELECT first_name, UserImage FROM userdata WHERE
> loginid = ? AND password = ?";
>
> $statement = $con->prepare($user_query); // This line is ging the
> error
> $statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
>
> $result = $statement->execute();
>
>
> // 'Close database connection'
>
> if (mysql_close($con))
> echo "<BR>Database connection closed";
> else
> echo "<BR>Failed to close database connection";
>
> }
> ?>
>
> But I get the error:
> Fatal error: Call to a member function prepare() on a non-object in C:
> \Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
> 17.
>
> Can any please me on this.
>
> Thanks
> Mohan
>


mysql_connect() is a functional interface to MySQL, not an OO one. It
returns a connection resource, not an object. Also, the mysql_
interface doesn't support prepared statements.

I suspect you want the mysql improved extension. It is an OO interface
which supports prepared statements (and, BTW, is more efficient than PDO).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 18/02/2008, 03h43   #4
kodaliece@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

Hi,

By Searching on internet for some time I came to know that, as you
said, I have to use PDO. So I tried to use it. But I get the error:
Fatal error: Class 'PDO' not found.

So, I did the following steps which I get from some website:

I changed the file "php.ini", by removing the comments at the
following lines:
extension=php_pdo.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite.dll

Then I copied these files to the system32 folder. Still I got the same
error. Can you please tell me how to resolve this

Thanks
Mohan



On 17 Feb, 19:27, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> kodali...@gmail.com wrote:
> > Hi,

>
> > I am using prepared statements and executing the below small script.

>
> > <?php
> > $username="root";
> > $password="admin";
> > $database="test";

>
> > // 'Connect to database'
> > $con = mysql_connect('localhost', $username, $password, $database) or
> > die(mysql_error());

>
> > if (!($con))
> > {
> > echo "<BR>Cannot connect to datbase. Please try again Later...";
> > }
> > else
> > {
> > $user_query = "SELECT first_name, UserImage FROM userdata WHERE
> > loginid = ? AND password = ?";

>
> > $statement = $con->prepare($user_query); // This line is ging the
> > error
> > $statement->bind_param("ss", $_POST['login_id'], $_POST['password']);

>
> > $result = $statement->execute();

>
> > // 'Close database connection'

>
> > if (mysql_close($con))
> > echo "<BR>Database connection closed";
> > else
> > echo "<BR>Failed to close database connection";

>
> > }
> > ?>

>
> > But I get the error:
> > Fatal error: Call to a member function prepare() on a non-object in C:
> > \Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
> > 17.

>
> > Can any please me on this.

>
> > Thanks
> > Mohan

>
> mysql_connect() is a functional interface to MySQL, not an OO one. It
> returns a connection resource, not an object. Also, the mysql_
> interface doesn't support prepared statements.
>
> I suspect you want the mysql improved extension. It is an OO interface
> which supports prepared statements (and, BTW, is more efficient than PDO).
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -
>
> - Show quoted text -


  Réponse avec citation
Vieux 18/02/2008, 03h53   #5
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

kodaliece@gmail.com wrote:
> Hi,
>
> By Searching on internet for some time I came to know that, as you
> said, I have to use PDO. So I tried to use it. But I get the error:
> Fatal error: Class 'PDO' not found.
>
> So, I did the following steps which I get from some website:
>
> I changed the file "php.ini", by removing the comments at the
> following lines:
> extension=php_pdo.dll
> extension=php_pdo_sqlite.dll
> extension=php_sqlite.dll
>
> Then I copied these files to the system32 folder. Still I got the same
> error. Can you please tell me how to resolve this
>
> Thanks
> Mohan
>
>
>
> On 17 Feb, 19:27, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> kodali...@gmail.com wrote:
>>> Hi,
>>> I am using prepared statements and executing the below small script.
>>> <?php
>>> $username="root";
>>> $password="admin";
>>> $database="test";
>>> // 'Connect to database'
>>> $con = mysql_connect('localhost', $username, $password, $database) or
>>> die(mysql_error());
>>> if (!($con))
>>> {
>>> echo "<BR>Cannot connect to datbase. Please try again Later...";
>>> }
>>> else
>>> {
>>> $user_query = "SELECT first_name, UserImage FROM userdata WHERE
>>> loginid = ? AND password = ?";
>>> $statement = $con->prepare($user_query); // This line is ging the
>>> error
>>> $statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
>>> $result = $statement->execute();
>>> // 'Close database connection'
>>> if (mysql_close($con))
>>> echo "<BR>Database connection closed";
>>> else
>>> echo "<BR>Failed to close database connection";
>>> }
>>> ?>
>>> But I get the error:
>>> Fatal error: Call to a member function prepare() on a non-object in C:
>>> \Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
>>> 17.
>>> Can any please me on this.
>>> Thanks
>>> Mohan

>> mysql_connect() is a functional interface to MySQL, not an OO one. It
>> returns a connection resource, not an object. Also, the mysql_
>> interface doesn't support prepared statements.
>>
>> I suspect you want the mysql improved extension. It is an OO interface
>> which supports prepared statements (and, BTW, is more efficient than PDO).
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================- Hide quoted text -
>>
>> - Show quoted text -

>
>


They don't go in the systems folder - you need to set the extension
directory in your php.ini file, then ensure those files are in the
extension directory. See the comments in the php.ini file for more
information.

And ensure you're changing the correct php.ini file. See what phpinfo()
says about it.

And BTW - PDO doesn't really "support" prepared statements. Rather, it
emulates them. The only MySQL code in PHP which currently supports true
prepared statements is the MySQL Improved Interface, mysqli.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 18/02/2008, 05h29   #6
kodaliece@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

On Feb 18, 8:53am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> kodali...@gmail.com wrote:
> > Hi,

>
> > By Searching on internet for some time I came to know that, as you
> > said, I have to use PDO. So I tried to use it. But I get the error:
> > Fatal error: Class 'PDO' not found.

>
> > So, I did the following steps which I get from some website:

>
> > I changed the file "php.ini", by removing the comments at the
> > following lines:
> > extension=php_pdo.dll
> > extension=php_pdo_sqlite.dll
> > extension=php_sqlite.dll

>
> > Then I copied these files to the system32 folder. Still I got the same
> > error. Can you please tell me how to resolve this

>
> > Thanks
> > Mohan

>
> > On 17 Feb, 19:27, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> >> kodali...@gmail.com wrote:
> >>> Hi,
> >>> I am using prepared statements and executing the below small script.
> >>> <?php
> >>> $username="root";
> >>> $password="admin";
> >>> $database="test";
> >>> // 'Connect to database'
> >>> $con = mysql_connect('localhost', $username, $password, $database) or
> >>> die(mysql_error());
> >>> if (!($con))
> >>> {
> >>> echo "<BR>Cannot connect to datbase. Please try again Later...";
> >>> }
> >>> else
> >>> {
> >>> $user_query = "SELECT first_name, UserImage FROM userdata WHERE
> >>> loginid = ? AND password = ?";
> >>> $statement = $con->prepare($user_query); // This line is ging the
> >>> error
> >>> $statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
> >>> $result = $statement->execute();
> >>> // 'Close database connection'
> >>> if (mysql_close($con))
> >>> echo "<BR>Database connection closed";
> >>> else
> >>> echo "<BR>Failed to close database connection";
> >>> }
> >>> ?>
> >>> But I get the error:
> >>> Fatal error: Call to a member function prepare() on a non-object in C:
> >>> \Program Files\apache-tomcat-5.5.26\webapps\LoginValidate.php on line
> >>> 17.
> >>> Can any please me on this.
> >>> Thanks
> >>> Mohan
> >> mysql_connect() is a functional interface to MySQL, not an OO one. It
> >> returns a connection resource, not an object. Also, the mysql_
> >> interface doesn't support prepared statements.

>
> >> I suspect you want the mysql improved extension. It is an OO interface
> >> which supports prepared statements (and, BTW, is more efficient than PDO).

>
> >> --
> >> ==================
> >> Remove the "x" from my email address
> >> Jerry Stuckle
> >> JDS Computer Training Corp.
> >> jstuck...@attglobal.net
> >> ==================- Hide quoted text -

>
> >> - Show quoted text -

>
> They don't go in the systems folder - you need to set the extension
> directory in your php.ini file, then ensure those files are in the
> extension directory. See the comments in the php.ini file for more
> information.
>
> And ensure you're changing the correct php.ini file. See what phpinfo()
> says about it.
>
> And BTW - PDO doesn't really "support" prepared statements. Rather, it
> emulates them. The only MySQL code in PHP which currently supports true
> prepared statements is the MySQL Improved Interface, mysqli.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -
>
> - Show quoted text -


Hi,

Can you please tell me how to use mysqli.

Here are some details about my php.ini file:
phpinfo() is using the php.ini located at C:\WINDOWS\php.ini
In PHP file, I am using, extension_dir = "C:/Program Files/PHP"
and uncommented the following lines:
;extension=php_pdo.dll
;extension=php_pdo_sqlite.dll
;extension=php_sqlite.dll
These three files are available in C:/Program Files/PHP/ext

Thanks
Mohan
  Réponse avec citation
Vieux 18/02/2008, 06h24   #7
Anonymous
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

kodaliece@gmail.com wrote:
> Can you please tell me how to use mysqli.


Uncomment extension=php_mysqli.dll.

> In PHP file, I am using, extension_dir = "C:/Program Files/PHP"
> These three files are available in C:/Program Files/PHP/ext


Isn't the problem obvious?

Bye!
  Réponse avec citation
Vieux 18/02/2008, 07h00   #8
kodaliece@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

On Feb 18, 11:24am, Anonymous <anonym...@nowhere.invalid> wrote:
> kodali...@gmail.com wrote:
> > Can you please tell me how to use mysqli.

>
> Uncomment extension=php_mysqli.dll.
>
> > In PHP file, I am using, extension_dir = "C:/Program Files/PHP"
> > These three files are available in C:/Program Files/PHP/ext

>
> Isn't the problem obvious?
>
> Bye!


Hi,

I Uncommented extension=php_mysqli.dll and able to connect to
database. But now I am facing another problem.
Below are the few lines from my code:
$statement = $db_connection->prepare($user_query);
$statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
$result = $statement->execute();
$num=mysqli_num_rows($result); // This line is giving error
In this code I am trying to count the number of rows returned by the
query. But, I get the error, "Warning: mysqli_num_rows() expects
parameter 1 to be mysqli_result, boolean given" at the line I marked
in the code.

Can anyone please suggest me how to get the resultset.

Thanks
Mohan

  Réponse avec citation
Vieux 18/02/2008, 09h31   #9
kodaliece@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

Hi All,

I am able to resolve this.

Thanks for your ,
Mohan
  Réponse avec citation
Vieux 18/02/2008, 09h44   #10
Anonymous
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

kodaliece@gmail.com wrote:
> I Uncommented extension=php_mysqli.dll and able to connect to
> database. But now I am facing another problem.
> Below are the few lines from my code:
> $statement = $db_connection->prepare($user_query);
> $statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
> $result = $statement->execute();
> $num=mysqli_num_rows($result); // This line is giving error
> In this code I am trying to count the number of rows returned by the
> query. But, I get the error, "Warning: mysqli_num_rows() expects
> parameter 1 to be mysqli_result, boolean given" at the line I marked
> in the code.


Well, the error message is very clear. mysqli_stmt_execute returns a
boolean, 0 on failure and 1 on success and not a result set.
mysqli_num_rows can only be executed after a normal query, what you need
is mysqli_stmt_num_rows. Try this:

$statement = $db_connection->prepare($user_query);
$statement->bind_param("ss", $_POST['login_id'], $_POST['password']);
$statement->execute();
$statement->store_result();
$num = $statement->num_rows;

P.S.: Stick to one style, procedural or object oriented. Don't mix them.

Bye!
  Réponse avec citation
Vieux 18/02/2008, 09h52   #11
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error using prepared statements in PHP

..oO(Jerry Stuckle)

>And BTW - PDO doesn't really "support" prepared statements. Rather, it
>emulates them. The only MySQL code in PHP which currently supports true
>prepared statements is the MySQL Improved Interface, mysqli.


It depends on the driver. The PDO_MYSQL driver uses native prepared
statements if available. For older versions of MySQL they will be
emulated.

Micha
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h05.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26266 seconds with 19 queries