Put in array SQL results?

Hi, how can I put in array the sql query result that contains 2 columns and then use that array to check the result based on the id in the array?

I mean the array will contain ID and URL and I would like to then some how retreat URL based on the ID i am providing. I mean retrieve the URL that has X ID??

$query2 = $db->query("
        SELECT g.id,g.url
        FROM ".TABLE_PREFIX."google_seo g
        ");  
$results = array();        
while($tids = $db->fetch_array($query2))
        {
            $results[] = $tids;
        }
    
        
        
        echo array_search(213,$results,true);

you mean something like where the id is the array key and the url is the array value?

with PDO you would do that by specifying the PDO::FETCH_KEY_PAIR mode. for mysqli you’d have to create that array manually.

Thanks buddy for the reply. Looks like this works:

$results[$tids['id']] = $tids['url'];

It creates this:

Array
(

    [11] => Forum-Rules
    [12] => Community-News-Announcements
    [129] => General-Discussions
)

Now how do I echo URL based on the ID from this array???

echo $results[$id];

Thanks buddy. But I would like to echo the URL from array based on the ID?

If the ID = 129 echo URL from this array or what comes after =>?

[129] => General-Discussions

see post #4.

what you have there is the output from print_r(). that’s used to give you a visualisation of the array content.

in other words

[{1}] => {2}

{1} - the ID form your database
{2} - the URL from your database

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.