/    Sign up×
Community /Pin to ProfileBookmark

PHP + MySqli errors

Hi everyone,
I can’t understand what happens… When I try my site in WAMP, I have the follow errors:
[I]
Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO (…)[/I]

[I]
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in (…)
[/I]

The same happened with the connect to mysql, but I solved using mysqli extension. But in this case is totally diferent. When I use the [B]mysqli_query [/B]or [B]mysqli_num_rows[/B] that are the alternatives presented I receive again other error, in PHP Manual says: [I]“function.mysqli-query doesn’t exist. Closest matches:”[/I]
[SIZE=4]
[B][SIZE=5]Someone know solve this problem???[/SIZE][/B][/SIZE]

using MySQL

[code=php]<?php
session_start();
if (isset($_SESSION[“manager”])){
header(“location:index.php”);
exit();
}
?>
<?php
if(isset($_POST[“username”])&&isset($_POST[“password”])){

$manager=preg_replace(‘#[^A-Za-z0-9]#i’,”,$_POST[“username”]);
$password=preg_replace(‘#[^A-Za-z0-9]#i’,”,$_POST[“password”]);

$cnn= include “../lojascript/connect_mysql.php”;
$sql=mysql_query($cnn, “SELECT id FROM admin WHERE username=’$manager’ AND password=’$password’ LIMIT 1”);

$existCount=mysql_num_rows($sql);
if ($existCount==1){
while ($row = mysqli_fetch_array($sql)){
$id=$row[“id”];
}
$_SESSION[“id”]=$id;
$_SESSION[“manager”]=$manager;
$_SESSION[“password”]=$password;
header(“location:index.php”);
exit();
}else {
echo ‘Informação incorrecta <a href=”index.php”> Click here</a>’;
exit();
}
}
?>[/code]

When I use MySQLi

[code=php]<?php
session_start();
if (isset($_SESSION[“manager”])){
header(“location:index.php”);
exit();
}
?>
<?php
if(isset($_POST[“username”])&&isset($_POST[“password”])){

$manager=preg_replace(‘#[^A-Za-z0-9]#i’,”,$_POST[“username”]);
$password=preg_replace(‘#[^A-Za-z0-9]#i’,”,$_POST[“password”]);

$cnn = include “../lojascript/connect_mysql.php”;
$query= “SELECT id FROM admin WHERE username=’$manager’ AND password=’$password’ LIMIT 1”;
$result= mysqli_query($cnn,$query) or die(mysqli_error());
$num_row = mysqli_num_rows($result);

if ($num_row==1)
{
while ($row = mysqli_fetch_array($result)){
$_SESSION[“id”]=$row[“id”];
}

$_SESSION[“id”]=$id;
$_SESSION[“manager”]=$manager;
$_SESSION[“password”]=$password;
header(“location:index.php”);
exit();
}else {
echo ‘Informação incorrecta <a href=”index.php”> Click here</a>’;
exit();
}
}
?>[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmNov 27.2014 — You cannot mix MySQL and mysqli functions. Period.


You're making a connection using the same code in both examples. No Can Do.


Interesting to see how you are picking up the connection thru the use of an assignment out of 'included' code. Never knew you could do that. (Assuming it is working here)
Copy linkTweet thisAlerts:
@rootNov 30.2014 — Also, typos need to be sorted out...

[code=php]if(isset($_POST["username"])&&isset($_POST["password"])){ [/code]

[code=php]if( isset( $_POST["username"] ) && isset( $_POST["password"] ) ){ [/code]
if you put additional space in to your code, it does help with being able to read the code and also hopefully stop any future typo's occurring from poor code formatting.

mysqli_ functions need to be used with a mysqli_ connection being made using the proper opener. If you want to use the OO method then use that method, otherwise stick to procedural methods, which I much prefer on a personal note, much less code needed than OO or PDO. you should note as well that PDO is a class, so it needs to be installed on your server.
Copy linkTweet thisAlerts:
@NogDogDec 01.2014 — I personally find the OO syntax clearer than the procedural syntax, but perhaps that's simply because I use it all the time? (And these days I exclusively use PDO.) I don't think the functional syntax is "[I]much[/I] less code"; and even if it's "[I]somewhat[/I] less code", that should be the least of your worries: clear, logical, easy-to-maintain code is much, much more important than saving a few keystrokes, unless perhaps you're writing a small, one-off, throw-away script you don't expect to have to maintain.

IMHO. ?
Copy linkTweet thisAlerts:
@NogDogDec 01.2014 — PS: Here's an example of how you can use PDO to do the first query without an inordinate amount of code. Your include file would have something like this:
[code=php]
$dsn = 'mysql:dbname=nogdog;host=127.0.0.1';
$user = 'nogdog';
$pwd = 'youwontguess';

$pdo = new PDO($dsn, $user, $pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
[/code]

Your app code for that query could then be:
[code=php]
try { // the try/catch structure is not required, but very useful
$sql = 'SELECT id FROM admin WHERE username=:manager AND password=:password LIMIT 1';
$pdo->query($sql, array(
':manager' => $manager,
':password' => $password
));
$existcount = $stmt->rowCount();
}
catch(PDOException $e)
{
// handle DB errors here, e.g.:
error_log("PDO error:".PHP_EOL.var_export($e, 1));
die("Sorry, there was a database error. Debug info has been logged.");
}
catch(Exception $e)
{
// handle any other exceptions here
die("WTF?"); // preferable something "nicer" than that. ;)
}
[/code]
Copy linkTweet thisAlerts:
@iBeZiDec 02.2014 — NogDog +1 for using PDO but I think you've messed up the syntax for creating a prepared statement a bit there, the query section of your code should look like this.

[code=php]
$sql = 'SELECT id FROM admin WHERE username=:manager AND password=:password LIMIT 1';
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':manager' => $manager,
':password' => $password
));
$existcount = $stmt->rowCount();
[/code]


Also you should make sure 'emulate prepared statements' is turned off when creating your DB connection

[code=php]
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

[/code]
Copy linkTweet thisAlerts:
@NogDogDec 02.2014 — Thanks, I think I was thinking about two other things when I typed that, while working with code that uses a very different ORM. That's what happens when you don't test your code. ?
Copy linkTweet thisAlerts:
@iBeZiDec 02.2014 — Indeed, I'm just happy to see someone else using PDO, the amount of people who are apparently still using the mysql functions is downright depressing. PDO does not add that much extra work, especially if you make a wrapper class for it, and it makes things so much simpler...
Copy linkTweet thisAlerts:
@NogDogDec 02.2014 — We use PDO at work, with a PostgreSQL DB, and I'm in the process of learning Laravel PHP framework and its "Eloquent" ORM, along with sort of trying to learn Ruby/Rails* -- so I'm continually confused these days. ?

_____________
  • * It's slow-going, as I just don't like Ruby itself
  • Copy linkTweet thisAlerts:
    @iBeZiDec 02.2014 — Ah I haven't had a chance to use PostgreSQL, mostly just stick with MySQL. I've done a few projects at work using Laravel, I liked its ORM so much I built my own version of it just to see if I could which has evolved into a stripped down version of Laravel which I use day to day. I really need to branch out into other programming languages at some point, I started learning Python but haven't gotten too far with it yet, need to get back into it. The only thing I've used Ruby for is SASS, but I haven't built anything myself with it.
    ×

    Success!

    Help @Ezkiel 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 5.19,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...