Ilya Libin
Ilya Libin

Reputation: 1617

issue with searching for string inside the array

I almost an hour trying to understand what i doing wrong here. The is no output.

The connection is Ok, The array is full of data

<?php header('Content-Type: text/html; charset=utf-8');

// Here's the argument from the client.
$domain = $_GET['string'];
$quest=$_GET['quest'];
$event=$_GET['event'];

$con = mysql_connect('localhost', '******', '********');
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("vocabulary", $con);

$sql="SELECT * FROM `0` WHERE event_name = '".$event."' AND quest_id = '".$quest."'";

$result = mysql_query($sql);

$row = mysql_fetch_array($result);

$key = array_search($domain, $row);

echo $key;

mysql_close($con);


?>

Any ideas?? Thanks

Upvotes: 0

Views: 50

Answers (2)

Jeff Hawthorne
Jeff Hawthorne

Reputation: 568

i think your issue is that you're not creating an associative array http://php.net/manual/en/function.array-search.php

your mysql_fetch_array is returning an array that has values but not keys, and so there is nothing to be stored in $key. try this instead:

$row = mysql_fetch_array($result, MYSQL_ASSOC)

also, obligatory "you should avoid using mysql_ functions entirely, and move to mysqli or PDO because mysql_ has been deprecated"

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82028

A few things.

  1. You are selecting from the table named 0. I don't think you should be doing that.
  2. Since I doubt the table 0 exists, my guess is that you have an error at mysql_fecth_array. Try putting error_reporting(E_ALL); at the start of your script.
  3. array_search returns False if it doesn't find anything. Try var_dump($key); instead.
  4. Your code has a serious security problem. I suggest that you move over to PDO or MySQLi.

Bobby Tables!

Upvotes: 3

Related Questions