1

Im having problems while trying to fetch an id of a specific string in mySQL

My table:

ID |         URL       |
1  | http://google.com |

When I put this V

    SELECT ID FROM urls
WHERE url LIKE ('http://google.com')

code into mySQL it does show the ID 1, but Im trying to let it display on a page on my website.

The code below is the code of the page where Im trying to display this code.

<?php
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

    // sql to create table
$url = 'http://google.com';
$ip = $_SERVER['REMOTE_ADDR'];

$sql = "
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
";

$result = $conn->query($sql);

echo $result;
$conn->close();

Yes I have defined DB_HOST, DB_USER, DB_PASS and DB_NAME and they work fine.

Whenever I execute the code, this error message appears.

Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/u926065153/public_html/db_t.php on line 16

Please note I dont have a lot of experience with SQL and this is my way of learning.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ErroR
  • 11
  • 1
  • 5
  • 2
    `$result` is a mysqli object, NOT a string. you can't echo out the rows/fields you queiried for using that. you have to fetch a row, then display that array/object. In other words, READ THE ERROR MESSAGE – Marc B Jul 07 '16 at 18:18
  • 'Please note I dont have a lot of experience with SQL and this is my way of learning.' Please read the full thread. – ErroR Jul 07 '16 at 18:19
  • Can I please have a link to something I can read? – ErroR Jul 07 '16 at 18:21
  • Sure, see: http://php.net/manual/en/mysqli-result.fetch-assoc.php – chris85 Jul 07 '16 at 18:22
  • @chris85 It seems like *mysqli_fetch_assoc* is more for displaying a whole table, but for my case Im only trying to display a single number. – ErroR Jul 07 '16 at 18:33
  • I fixed it. `if ($result = mysqli_query($conn, $sql)) { /* fetch associative array */ while ($row = mysqli_fetch_assoc($result)) { printf ($row["ID"]); } /* free result set */ mysqli_free_result($result); }` – ErroR Jul 07 '16 at 18:36
  • It is for a row, not a table. – chris85 Jul 07 '16 at 18:36
  • Thanks for the information! Have a nice day – ErroR Jul 07 '16 at 18:42

1 Answers1

0

you should fetch the result.

http://php.net/manual/en/mysqli-result.fetch-array.php

That is,

$row = $result->fetch_array(MYSQLI_NUM);

var_dump($row);

You could see all the rows as array.

Your code will be like this.

<?php
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

    // sql to create table
$url = 'http://google.com';
$ip = $_SERVER['REMOTE_ADDR'];

$sql = "
SELECT ID FROM urls
WHERE url LIKE ('http://google.com')
";

$result = $conn->query($sql);

$row = $result->fetch_array(MYSQLI_NUM);

echo $row[0] . " " . $row[1];

$conn->close();
Aseem PM
  • 97
  • 6