mysqli is b0rk?

Status
Not open for further replies.

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
Currently, I'm doing:
Code:
<?php
        include("config/databasecfg.php");   
        $stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
        $stmt->bind_param('s', $_GET['id']);

        $stmt->execute();

        $result = $stmt->get_result();
       
        while ($row = $result->fetch_assoc()) {
            $name = $row['name'];
        }
    ?>

But it doesn't work. It cuts off the page at line before <?php. :(
Anybody have any ideas? It works on my local XAMPP server.
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
What is the name of the file you are using? It has to end in .php here for the server to parse and run it as PHP. .html will not work.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You also need to be aware that output buffering is on by default on XAMPP (and on WampServer, for that matter) and off by default on Free Hosting servers. Some scripts may require that you use the obXXXX() functions to work properly.
 

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
You also need to be aware that output buffering is on by default on XAMPP (and on WampServer, for that matter) and off by default on Free Hosting servers. Some scripts may require that you use the obXXXX() functions to work properly.
Does doing simple mysqli stuff need output buffering?
What is the name of the file you are using? It has to end in .php here for the server to parse and run it as PHP. .html will not work.
Of course it's .php! Chrome would have just shown the code if it weren't.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
That depends entirely on the script, but all it takes to test is a one-line edit to your local php.ini.
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
What does
config/databasecfg.php
do?
Could that be the problem? And just because it works on your local machine does not mean it will work on any other machine.

Sprinkle echo messages around the code and see exactly where the script stops.
 

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
What does
config/databasecfg.php
do?
Could that be the problem? And just because it works on your local machine does not mean it will work on any other machine.

Sprinkle echo messages around the code and see exactly where the script stops.
config/databasecfg.php connects to the database with $db as the variable. Uhh. Just so you know, that's not the actual name. I changed it to something similar (but it's correct in the actual code) just in case someone (aka a person I'm hiding from) figures out my link.
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
config/databasecfg.php connects to the database with $db as the variable.

I sort of guessed that.
Which means it can contain a dozen possible points of failure.
Which we can only guess at, since you don't want to post it.
Nobody else is reporting the mysqli is acting up, as far as I can see in this Forum.
So, my guess is that your script is at fault, not the server on x10hosting.

Bugfinding 101:

1. Remove any suppression of error reporting -- you can add it back once the bug is found and fixed.

2. Test all return values to make sure they are not False or Null

3. Add print/echo statements to output values or progress statements, ie:
echo "Checkpoint 1";
SOMECODE
echo "Checkpoint 2";
MORE CODE
echo "Checkpoint 3";

... so if the script prints "Checkpoint 2" but not "Checkpoint 3", you can begin to isolate the bug.
 

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
Code:
        include("config/databasecfg.php");
        echo("Checkpoint 1/6 - included newsdb<br>");
        $stmt = $sql->prepare('SELECT * FROM games WHERE id = ?');
        echo("Checkpoint 2/6 - prepared statement<br>");
        $stmt->bind_param('s', $_GET['id']);
        echo("Checkpoint 3/6 - binded parameter<br>");
        $stmt->execute();
        echo("Checkpoint 4/6 - executed<br>");

        $result = $stmt->get_result();
       
        echo("Checkpoint 5/6 - got result<br>");
       
        while ($row = $result->fetch_assoc()) {
            $name = $row['name'];
        }
       
        echo("Checkpoint 6/6 - done<br>");

http://utest.rblxdev.pw/play.php?id=1 - it appears to be stuck at get_result();
I just don't get why it does this though..
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
Code:
$stmt->bind_param('s', $_GET['id']);
echo $_GET['id'];
echo("Checkpoint 3/6 - binded parameter<br>");
x = $stmt->execute();
echo x;
printf("Error: %s.\n", $stmt->error);
echo("Checkpoint 4/6 - executed<br>");


First additional echo will make sure that the id is passed properly
Then test return value of execute() and then see if there is an error message.
 

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
Code:
$stmt->bind_param('s', $_GET['id']);
echo $_GET['id'];
echo("Checkpoint 3/6 - binded parameter<br>");
x = $stmt->execute();
echo x;
printf("Error: %s.\n", $stmt->error);
echo("Checkpoint 4/6 - executed<br>");


First additional echo will make sure that the id is passed properly
Then test return value of execute() and then see if there is an error message.

No error here. If you look at the page again, I added some more debuggy info.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You're speccing ID as a string param. Are you sure about that?
 

rblxdevx

New Member
Messages
17
Reaction score
0
Points
1
Okay, so I just ended up resorting to PDO for this specific page. Thanks for the help, everyone.
I'll just leave this here:
Code:
try {
            $db = new PDO('mysql:host=localhost;dbname=dbnamehere;charset=utf8', 'usernamehere', 'pwhere');
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $db->prepare("SELECT * FROM users WHERE id = :id");
            $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
            $sq = $stmt->execute();
          
            $result = $stmt->fetchAll();
          
            // echo $stmt->rowCount(); // for the row count
          
            foreach($result as $row)
            {
                echo $row['name'];
            }
          
            $db = null;
        }
        catch(PDOException $ex)
        {
            echo $ex->getMessage();
        }
 
Status
Not open for further replies.
Top