-1

I am trying to execute a query with a WHERE clause but it looks like the id I retrieve needs to be perhaps converted from an array into something else. I am new to PHP so I am struggling a little:

...some previous query here
$sharepoint_id = $data[0];
//returns Array([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)   

qry = mysql_query("SELECT HostName FROM MSSWireList WHERE id=".$sharepoint_id);     
    $data = array();
    while($rows = mysql_fetch_array($qry))
    {
        $data[] = array(
                    "ID"          => $rows['ID'],
                    "Record"      => $rows['Record'],                                
                    "HostName"    => $rows['HostName']
                    );
    }
    return json_encode($data);  

also tried $sharepoint_id = $data[0]->ID; Thank you

Max
  • 1,289
  • 3
  • 26
  • 50
  • What error are you getting?done – William Madede Nov 28 '15 at 01:34
  • What's wrong with JOIN? – Strawberry Nov 28 '15 at 01:54
  • I really must concentrate on cultivating the habit of asking, "***What does the error message say?***" – Darwin von Corax Nov 28 '15 at 02:11
  • Your 'qry' does not have a dollar sign in front of it. – Fintan Creaven Nov 28 '15 at 02:13
  • @FintanCreaven I noticed that too [and asked them about it in comments](http://stackoverflow.com/questions/33966745/having-problems-to-execute-a-php-code-with-simple-mysql-query/33966767?noredirect=1#comment55691261_33966761) under my (edited) answer, but haven't gotten feedback from it, or any other comment for that matter. I'll add it in my answer, just... in... case. – Funk Forty Niner Nov 28 '15 at 02:13
  • 1
    He/ she needs to make the syntax changes. Add in error catching code suggested in the answer given and confirm that we are all at the same stage. I imagine once all changes are made it will work. – Fintan Creaven Nov 28 '15 at 02:21
  • sorry guys, the code is okay it was just a typo here. The suggestion from Darwin worked well. Yes I am doing maintenance in a crap code where they don't use mysqli or PDO and I am new to PHP so you know how it goes...frustration. Thanks! – Max Nov 28 '15 at 02:35

2 Answers2

1

"returns Array([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)"

That's a string and not an integer. The variable in your WHERE clause needs to be quoted.

WHERE id='".$sharepoint_id."' ");

Checking for errors would have signaled the syntax error.

Add or die(mysql_error()) to mysql_query().


Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.


Edit:

You only selected the HostName column from your query and not the other two, ID and Record.

However, when going over a loop, row names are case-sensitive.

So, if your row's case is id as opposed to ID, then that will matter in your loop.

  • $rows['ID'] and $rows['id'] are two different animals.

Sidenote:

Pulled from a comment I asked already:

qry = mysql_query if that your real code, it's missing a $ for qry.

And if so, error reporting would have thrown you an undefined constant qry notice.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

I won't take you to task for using the old mysql driver instead of mysqli or PDO, or for not using a prepared statement - I'll leave that for others to do - but

"...WHERE id = '" . $sharepoint_id['ID'] . "'"

should do the job.

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
  • I actually thought you were right on this one. We both got a nice downvote. Not my downvote btw. Seems like whoever downvoted us, doesn't want to share their popcorn with us ;-) – Funk Forty Niner Nov 28 '15 at 01:59
  • 'S okay - I have a little extra popcorn to share. – Darwin von Corax Nov 28 '15 at 02:03
  • what I don't get is that they show us `returns Array([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)` - no idea why either of our answers would fail him. Oh well, I might end up deleting my answer and go to the store to get me a soda for that popcorn ;-) after I finish my sandwich. – Funk Forty Niner Nov 28 '15 at 02:06
  • Who knows. Could be anyone. Could even be Groucho Marx ;-) I made an edit to my answer, could be an added contribution to their query failing. – Funk Forty Niner Nov 28 '15 at 02:13