simple mysql query works in phpmyadmin but will not work in a php script

424

I have been working all day on a php script that pulls data from a single mysql database. My script has worked fine, and then all of a sudden mysql stopped returning rows with certain queries. It started with a complex join, but now one certain query will not return a row. The same exact query pasted into phpmyadmin does return a row.

First, the query:

    $sql = "SELECT tagsetsname_id FROM tagsets_docpaths_sets WHERE documents_paths_id = '233'";  //the '233' comes from {$_SESSION['document']['docpathid']}

When I paste this query in phpmyadmin I get this result:

tagsetsname_id
    0

That is the result I wanted. The tagsetsname_id is 0.

When I try this in a php script:

    //$db is connection to a database, some queries are working so $db is connected
    $sql = "SELECT tagsetsname_id FROM tagsets_docpaths_sets 
    WHERE documents_paths_id = '233'";
$res = $db->query($sql);

And then look at $res in Firebug, I have the following result:

    mysqli_result(
    current_field =
    field_count =
    lengths =
    num_rows =
    type =
    )

It's empty. I'm baffled.

I restarted apache; I restarted mysql.

What simple thing could I be missing?

Am new to mysql. Could the database be corrupt in some way? Is there a rebuild or something I could try?

**script

    $sql = "SELECT tagsetsname_id FROM tagsets_docpaths_sets \
    WHERE documents_paths_id = '{$_SESSION['document']['docpathid']}'";
    //{$_SESSION['document']['docpathid']} = 233;

    $res = $db->query($sql);
    $row = $res->fetch_row();
    $firephp->trace($db);
    $firephp->group('tags');
    $firephp->log('$sql');
    $firephp->trace($sql);
    $firephp->log('$row');
    $firephp->trace($row);
    $firephp->groupEnd();

**debug

    mysqli(
    affected_rows =
    client_info =
    client_version =
    connect_errno =
    connect_error =
    errno =
    error =
    field_count =
    host_info =
    info =
    insert_id =
    server_info =
    server_version =
    stat =
    sqlstate =
    protocol_version =
    thread_id =
    warning_count =
    )

    SELECT tagsetsname_id FROM tagsets_docpaths_sets WHERE documents_paths_id = '233' 

     $row
    null
    File         Line   Instruction
    .../create_metatags.php

    291

    FirePHP->trace( '')

YET in phpmyadmin the pasted $sql from the debug works perfectly. Am I crazy?!

641

Answer

Solution:

As shown on this page http://us3.php.net/manual/en/mysqli-result.fetch-row.php , $res->fetch_row() returns results in an array with numeric indexes. Your result should be in $row[0].

70

Answer

Solution:

You don't need the' in'233' I believe.

589

Answer

Solution:

What is the object type of $db...? You might need to fetch results from $res

i.e. $data = $res->fetchAll();

958

Answer

Solution:

The problem looks very obvious, first you didn't use the exact syntax when pass numeric data to be equal to some filed. second you are not fetching any thing from the database, even not execute, even not prepare to be executed. it is correct you must not see any thing on the screen...try to follow the following...

<pre>
$sql = "SELECT tagsetsname_id FROM tagsets_docpaths_sets 
    WHERE documents_paths_id = 233 ";  // with out (').
$res = $db->query($sql);
</pre> 

then add the following after you query the sql...

<pre>
$query = $db->prepare($sql);      
$query->execute();
$ent = $query->fetchAll();
</pre>

In general use this...

<pre>
 $sql = "SELECT tagsetsname_id FROM tagsets_docpaths_sets 
        WHERE documents_paths_id = 233 ";  // with out (').
 $query = $db->prepare($sql);      
 $query->execute();
 $ent = $query->fetchAll();
</pre>

then print_r($ent); now you should get list of available data as array.

is that helpful?

People are also looking for solutions to the problem: php - How to parse a Zend URL for parameters?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.