0

when I try this code I receive warning about second parameter to string. I have seen some answers in previous similar questions but I did not find the solution... As far as I can get the problem is with if statement?? Thanks.

if (isset($_GET['id'])) {

    $str_id = $_GET['id'];

    ($conn->set_charset("utf8"));   

    if ($result=mysqli_query($conn, $q )) {
        while ($obj=mysqli_fetch_object($result)) {
        }
?>
michaelbn
  • 7,393
  • 3
  • 33
  • 46
Vlad
  • 1
  • 1
  • 4
    Where is `$q` defined? – Sougata Bose Jul 28 '15 at 09:52
  • possible duplicate of [Warning: mysqli\_query() expects parameter 2 to be string, object given in](http://stackoverflow.com/questions/21285327/warning-mysqli-query-expects-parameter-2-to-be-string-object-given-in) – Genhis Jul 28 '15 at 09:55
  • 1
    Second parameter to `mysqli_query` is `$q`. PHP expects this parameter is a string, but apparently it's an object. So far the message is very clear. So now the task is to find out what kind of object it is and where it is set. – GolezTrol Jul 28 '15 at 09:55
  • $q was defined in previous document in query $q = "SELECT str_id,... – Vlad Jul 28 '15 at 10:03
  • But now I replaced it with: if (isset($_GET['id'])){ $str_id = $_GET['id'];. And it wont work – Vlad Jul 28 '15 at 10:05
  • Can you `var_dump($q)` right before the `if`? I guess that it doesn't have the value you expect it to have. – GolezTrol Jul 28 '15 at 10:11
  • If you replaced it.. it is not existent anymore, right? So you have to set it again.. – Cagatay Ulubay Jul 28 '15 at 10:42
  • THANK YOU ALL! I'm old and quite new in PHP :) I found errror. It is simple I've forgot query b=3s and other helped me understand the problem: So I am exposing (in not so nat&beginers) complete code: ($conn->set_charset("utf8")); if (isset($_GET['id'])){ $str_id = $_GET['id']; $q = "SELECT * FROM articles WHERE str_id='{$str_id}' "; if ($result=mysqli_query($conn, $q )) { while ($obj=mysqli_fetch_object($result)){... Now it works. SO I stupido left query. Thanks and for help and for code editing... – Vlad Jul 28 '15 at 10:49

1 Answers1

0
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  while ($obj=mysqli_fetch_object($result))
    {
    printf("%s (%s)\n",$obj->Lastname,$obj->Age);
    }
  // Free result set
  mysqli_free_result($result);
}

mysqli_close($con);
?> 
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
Amit Rana
  • 190
  • 5