views:

511

answers:

2

Hello,

I have the following simple mysqli php application, which should work fine. $pk is accepted perfectly and is a valid ARTICLE_NO, and the query works perfectly when executed directly by mysql. I have put output statements after every event and all except tetsing while executes. The while loop is never entered, and I am unsure why.

edit: I have narrowed the problem down to the fact that 0 rows are returned, but I have no idea why as the same query in phpmyadmin gives the right result.

edit2: if I get rid of the while loop and just have

    if (!$getRecords->fetch()) {
    printf("<p>ErrorNumber: %d\n", $getRecords->errno);
}

It shows that the errno is 0. So no records are fetched, and there is no error, yet it is a valid query.

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
$pk = $_GET["pk"];
$con = mysqli_connect("localhost", "", "", "");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->set_charset("utf8");
echo "test outside loop";
if(1 < 2) {
    echo "test inside loop";
    $query1 = 'SELECT ARTICLE_NO FROM AUCTIONS WHERE ARTICLE_NO = ?';
    if ($getRecords = $con->prepare($query1)) {
echo "inside second loop";
     $getRecords->bind_param("i", $pk);
echo "test after bind param";
     $getRecords->execute();
echo "test after bind execute";
     $getRecords->bind_result($ARTICLE_NO);
echo "test after bind result";
     while ($getRecords->fetch()) {
      echo "test inside while";
      echo "<h1>".$ARTICLE_NO."</h1>";
     }
    }
}

edit:

I tried with this code:

<?php
$mysqli = new mysqli("localhost", "", "", "");
$pk = $_GET["pk"];
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT ARTICLE_NAME, WATCH FROM AUCTIONS WHERE ARTICLE_NO = ? LIMIT 5")) {
    $stmt->bind_param("i", $pk);
    $stmt->execute();
    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);
    /* fetch values */
    while ($stmt->fetch()) {
     printf("%s %s\n", $col1, $col2);
    }
    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();
?>

This works without $pk, if I take away the parameters it works fine. It is not a problem with getting pk via GET, because if I assign $pk = 1; instead it still fails. 1 is a valid ARTICLE_NO, and SELECT ARTICLE_NAME, WATCH FROM AUCTIONS WHERE ARTICLE_NO = 1 LIMIT 5 works fine in phmyadmin.

edit: the problem was that mysqli could not handle bigint, I am now using k as a string and it works fine.

A: 

I'm not sure if you've modified that code, but you don't seem to be selecting the database you want to connect to there.

Use mysqli_select_db(...) for that if that is the problem.

EDIT: It also looks like you're using uppercase for the column, table name etc.

Get case sensitivity right, it could be that you're presuming case insensitivity because it works from the command line. As far as I know the mysqlI driver in PHP is case sensitive about at least column names.

Phil
if you check earlier questions you'll find the database name, along with his root username and password...
Alnitak
no you wont, you will find the fields filled in with fake information. Phil: I just commented out all the information, I am definitely selecting a database, Alnitak is causing trouble for no reason.
Joshxtothe4
not exactly - you certainly did post scripts a couple of months back which had your password in. glad to see that's not the case now :)
Alnitak
the password I posted was geheim, which is just german for secret. I never posted my real password.
Joshxtothe4
just posted an update. Could it be case sensitivity? You're using all uppercase chars for the column and table name.
Phil
fair enough - you didn't explain that at the time
Alnitak
Alnitak, not on every post but I tried to, I had hoped people would realize I was not posting actual credentials. Phil: Nope, The sensitivity is right. I updated my post to, it works without parameters.
Joshxtothe4
see my latest edit - you probably need to cast $pk to an integer. By default it's likely to be a string.
Alnitak
A: 

Check the value of:

$getRecords->num_rows

which should help reveal whether the earlier SELECT is actually returning any data

You may need to also add:

$getRecords->store_result()

first to ensure that you've the whole query has completed before asking for the number of rows in the result set.

Also - make sure you cast $pk to an integer! It's possible that the value being passed in is getting mangled.

Alnitak
num_rows is 0. I have cast pk to an integer to no avail, would it matter if pk was too long? its value is 110313660559. The problem only happens with paramters
Joshxtothe4
yes, that probably matters. bind_param() may not be able to cope with a bigint
Alnitak
ahh, is there any solution to this?
Joshxtothe4
On your platform PHP may only have 32 bit int/long types. Maybe try ignoring the advise from yesterday's question and treat $pk as a string!
Alnitak
also, the fact that this happens even when $pk = 1 suggests some sort of data conversion problem.
Alnitak