1

I am getting return values that do not exist in my current database. Even if i change my query the return array stays the same but missing values. How can this be what did i do wrong? My MYSQL server version is 10.0.22 and this server gives me the correct result. So the issue must be in PHP.

My code:

$select_query = "SELECT process_state.UID
FROM process_state 
WHERE process_state.UpdateTimestamp > \"[given time]\"";

$result = mysql_query($select_query, $link_identifier);

var_dump($result);

Result:

array(1) { 
[1]=> array(9) {
    ["UID"]=> string(1) "1" 
    ["CreationTimestamp"]=> NULL 
    ["UpdateTimestamp"]=> NULL 
    ["ProcessState"]=> NULL  
    } 
}

Solution: I have found this code somewhere in my program. The program used the same name ass mine. This function turns the MYSQL result into a array. This happens between the result view and my script. This was done to make the result readable.

parent::processUpdatedAfter($date);

Function:

public function processUpdatedAfter($date)
{
    $result = parent::processUpdatedAfter($date);
    $array = Array();

    if($result != false)
    {
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $array[$row["UID"]]["UID"] = $row["UID"];
            $array[$row["UID"]]["CreationTimestamp"] = $row["CreationTimestamp"];
            $array[$row["UID"]]["UpdateTimestamp"] = $row["UpdateTimestamp"];
            $array[$row["UID"]]["ProcessState"] = $row["ProcessState"];
        }
        return $array;
    }
    return false;
}

I edited this and my script works fine now thanks for all the help.

  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 22 '16 at 10:52
  • 1
    side note: use `mysqli_*` or `PDO` – devpro Sep 22 '16 at 10:52
  • 1
    I doubt that this is all of your code. `mysql_query()` returns a resource and not an array. – simon Sep 22 '16 at 10:57
  • @devpro The server where this is running does not have the support of PHP7. But the program needs to run on all the old systems with PHP5 or lower. – vincent poortvliet Sep 22 '16 at 11:11
  • final test. if this is not the whole code, **1-** check that $result you are dump the same $result you assigned the result(no typos or something), **2-** and check if there any other assignment happened to $result between the query and dumping – Accountant م Sep 22 '16 at 12:26
  • Congratulations on getting it done. can you please give us a details on this function because googling it giving no result. is this a php core function? – Accountant م Sep 22 '16 at 12:55
  • @user56489870 no was self made function i will edit solution. – vincent poortvliet Sep 22 '16 at 13:11

3 Answers3

3

You are var_dumping a database resource handle and not the data you queried

You must use some sort of fetching process to actually retrieve that data generated by your query.

$ts = '2016-09-20 08:56:43';  
$select_query = "SELECT process_state.UID
                FROM process_state 
                WHERE process_state.UpdateTimestamp > '$ts'";

$result = mysql_query($select_query, $link_identifier);

// did the query work or is there an error in it
if ( !$result ) {
    // query failed, better look at the error message
    echo mysql_error($link_identifier);
    exit;
}

// test we have some results
echo 'Query Produced ' . mysql_num_rows($result) . '<br>';

// in a while loop if more than one row might be returned
while( $row = mysql_fetch_assoc($result) ) {
    echo $row['UID'] . '<br>';
}

However I have to mention Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Did not work did not get any result at all. The reason i use var_dump is to find out were things went wrong. The funny thing is that CreationTimestamp does not exist it is called CreationTime. No clue where he get CreationTimestamp from that is a really old term i used a while back. But DB is dropt and recreated with new kolom names. – vincent poortvliet Sep 22 '16 at 11:05
  • 1
    what is the value of `given time` @vincentpoortvliet – devpro Sep 22 '16 at 11:08
  • @vincentpoortvliet "No clue where he get CreationTimestamp from that is a really old term i used a while back" .. are you sure you are querying the correct database ? – Accountant م Sep 22 '16 at 11:08
  • 1
    This part of your query looks odd for MYSQL `\"[given time]\"";` are you using MYSQL or SQLServer or something else – RiggsFolly Sep 22 '16 at 11:09
  • also run the query manually in PHP my admin: `SELECT process_state.UID FROM process_state WHERE process_state.UpdateTimestamp > "yourDate"` – devpro Sep 22 '16 at 11:09
  • @RiggsFolly: u want to check `$link_identifier` na :) – devpro Sep 22 '16 at 11:11
  • @user56489870 yes i am sure. i even changed the UID to test is i was using the right one. I am thinking in the lines of wrong memory allocation by PHP. But don't now how to check. – vincent poortvliet Sep 22 '16 at 11:16
  • 1
    @user56489870 I know its tempting to blame the tools, but if we are honest 99.5% of the time, its us developers that have screwed up and not the tools – RiggsFolly Sep 22 '16 at 11:18
  • @devpro
     \"[given time]\""; 
    is just to show you guys that a value needs to be put there. the query works fine but only in php things go wrong. getting correct result but not all the result and not with the correct keys connected to them.
    – vincent poortvliet Sep 22 '16 at 11:19
  • and what happened when you used this example? can you please share the result? @vincentpoortvliet – devpro Sep 22 '16 at 11:20
  • @vincentpoortvliet . No. don't think that, php does not make mistakes. we do. $result is not an array , it's is a resource , this is what you should get by var_dump it `resource(4) of type (mysql result)` how did you assign an array to it ? – Accountant م Sep 22 '16 at 11:21
  • @RiggsFolly all my other querys with the same link identifier work fine. – vincent poortvliet Sep 22 '16 at 11:22
  • @user56489870 yes I was expecting that result as well but i got what I showed you guys. All the other mysql_ can't be used because of this reason. – vincent poortvliet Sep 22 '16 at 11:24
  • 1
    @RiggsFolly I have run the code you have given and I get
    Query Produced 
    . No number is given because mysql_ functions aren't useble with this result.
    – vincent poortvliet Sep 22 '16 at 11:30
  • 1
    than `echo $select_query;` and share. @vincentpoortvliet – devpro Sep 22 '16 at 11:32
  • @RiggsFolly SELECT UID FROM process_state WHERE UpdateTime > 2016-09-20 08:56:43 – vincent poortvliet Sep 22 '16 at 11:37
  • missing quotes? i tihnk: what happened, if you run this query in your PHP code `SELECT UID FROM process_state WHERE UpdateTime > '2016-09-20 08:56:43'`; @vincentpoortvliet – devpro Sep 22 '16 at 11:39
  • Then you did not copy the code accurately. There shoudl have been single quotes around that date and time, but that shoudl have activated the Error Message code. – RiggsFolly Sep 22 '16 at 11:39
  • @devpro not the issue – vincent poortvliet Sep 22 '16 at 11:41
  • @vincentpoortvliet: not the issue what? working or not? getting result? – devpro Sep 22 '16 at 11:43
  • @RiggsFolly i have to edit the query because i can't show all the kolom names. But there is nothing wrong with the query. But more with the way i am am getting my return value. – vincent poortvliet Sep 22 '16 at 11:44
  • Oh **HANG ON** I may have been right a while back. ___You said My MYSQL server version is 10.0.22___ There is no MYSQL Server version with that number ___But there is a SQLServer with that version___ – RiggsFolly Sep 22 '16 at 11:45
  • @RiggsFolly he is using Maria DB https://mariadb.com/kb/en/mariadb/mariadb-10022-release-notes/ – Accountant م Sep 22 '16 at 11:46
  • or if anyone want to learn "HOW TO DEBUG?" than follow this.. :) – devpro Sep 22 '16 at 11:48
  • Yes i am use sing MariaDB 10.0.22. – vincent poortvliet Sep 22 '16 at 11:50
  • Can I make sure of something. Are you using EXACTLY the code I have posted in my answer? – RiggsFolly Sep 22 '16 at 11:52
  • Oh I just amended it to use the date you were using in your query – RiggsFolly Sep 22 '16 at 11:54
  • @vincentpoortvliet if you can shutdown the webserver(and php) and the mysql server and restart them again with fresh memory. why don't you give it a shot ? I think PHP does query cache , and mysql does the same. and see what will happen with the first query result – Accountant م Sep 22 '16 at 12:01
  • is there any badge for long long conversations? :) – devpro Sep 22 '16 at 12:15
  • 1
    @devpro Well we both deserve it if there is :) I already deleted some of my sillier ones to keep the list a little shorter – RiggsFolly Sep 22 '16 at 12:17
  • my vote for you for better explanation and debugging. – devpro Sep 22 '16 at 12:18
  • Thanks: I was going to UV your answer, now that has made me get round to it – RiggsFolly Sep 22 '16 at 12:18
  • 1
    @devpro I would love to know who UV's Navnit's answer. Obviously he has friends in low places – RiggsFolly Sep 22 '16 at 12:20
3

Note that, var_dump($result); will only return the resource not data.

You need to mysql_fetch_* for getting records.

Example with MYSQLi Object Oriented:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT process_state.UID
FROM process_state 
WHERE process_state.UpdateTimestamp > \"[given time]\"";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        echo $row['UID'];
    }    
} 
else 
{
    echo "No record found";
}
$conn->close();
?>

Side Note: i suggest you to use mysqli_* or PDO because mysql_* is deprecated and closed in PHP 7.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    I am guessing by the sudden silence, the OP has spotted what was wrong and has disappeared in a puff of smoke. I have definitely run out of steam on this question. I cannot see what is wrong, unless we are not being told the whole story, which I suspect is the case – RiggsFolly Sep 22 '16 at 12:22
  • @RiggsFolly I did not ran off but was still testing stuff my self. The reason of no respons is there was nothing to respond to anymore ;) – vincent poortvliet Sep 22 '16 at 12:51
  • Well there was a request from me to check you were using exactly the code I posted, so we all knew what we were trying to debug. And a check that you had seen an amendment to my posted code. – RiggsFolly Sep 22 '16 at 12:56
  • 1
    @devpro **Swear-word, swear-word** and another **swear-word**. See the question!!! OP has posted the solution!!!! OP as suspected was showing us code that seems to be totally nothing like the code he was actually using. **Thats an hour+ of my/our life(s) we will never get to see again** – RiggsFolly Sep 22 '16 at 13:01
  • 1
    @RiggsFolly: no words.. :) – devpro Sep 22 '16 at 13:05
  • 1
    @devpro I have a head full, but I cannot write any of them here. – RiggsFolly Sep 22 '16 at 13:06
1
$select_query = "SELECT `UID` FROM `process_state ` WHERE `UpdateTimestamp` > \"[given time]\" ORDER BY UID DESC ";

$result = mysql_query($select_query, $link_identifier);

var_dump($result);

Try this hope it will works

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Navnit Mishra
  • 497
  • 4
  • 14