-4

Sorry for the stupid question. Do not make values in the table from php code Only from phpmyadmin. While able to read records from a table. What could be the problem? User is same, has all privileges.

My code. Table contain id, site name, ip of the site.

$host='localhost'; 
$database='Spider';
$user='myuser';
$pswd='mypassword';
$dbh = mysql_connect($host, $user, $pswd) or die("Не могу соединиться с MySQL.");
mysql_select_db($database) or die("Не могу подключиться к базе.");
$query = "SELECT id, site, ip FROM `sites` LIMIT 0,100";
$res = mysql_query($query);
$reverse_ip = array();
for ($i=1; $row=mysql_fetch_array($res); $i++) {
    echo $row['site']."<br>"; //print site name
    $reverse_ip[$i] = substr($row['site'], 7, strlen($row['site'])-7);
    if (strpos($reverse_ip[$i], 'www.') !== false) {
        $reverse_ip[$i] = substr($reverse_ip[$i], 4, strlen($reverse_ip[$i])-4);
    }
    if (strpos($reverse_ip[$i], '/')) {
        $reverse_ip[$i] = substr($reverse_ip[$i], 0, -1); //editing site name
    }
    echo $reverse_ip[$i]."<br>"; //print the edited name
    $reverse_ip[$i]=gethostbyname($reverse_ip[$i]);
    echo $reverse_ip[$i]."<br>"; //print the ip of site
}
$sql = "UPDATE sites SET ip = 200 WHERE id = 4 "; //table remains unchanged

When I When I use this queries $sql = "UPDATE sites SET ip = 200 WHERE id = 4 "; in phpmyadmin it works

7
  • 4
    The problem is likely your code, which you haven't provided.
    – ceejayoz
    Nov 3, 2014 at 15:29
  • It is unlikely, because the same code in phpmyadmin correctly performs all.
    – Hushpar
    Nov 3, 2014 at 15:31
  • @Hushpar Post your code so we can help you. Nov 3, 2014 at 15:32
  • For example: $sql = "UPDATE sites SET ip = 200 WHERE id = 4 "
    – Hushpar
    Nov 3, 2014 at 15:33
  • @Hushpar That phpMyAdmin works and your code doesn't makes it nearly certain you messed something up. If you won't show code, we can't help you. Not your queries, your code.
    – ceejayoz
    Nov 3, 2014 at 15:36

1 Answer 1

1

Well, $res = mysql_query($query); $res isn't valid syntax.

After your mysql_query lines, do echo mysql_error(); and see what it says. Once you've figured out the immediate issue, go to http://php.net/manual/en/function.mysql-query.php, read the big red box, and stop using the mysql_* functions at all.

Your line:

$sql = "UPDATE sites SET ip = 200 WHERE id = 4 "; //table remains unchanged

doesn't update the database. It's a SQL query, but you aren't executing it anywhere.

2
  • did echo mysql_error(); but it printed me nothing. OK, thx very much.
    – Hushpar
    Nov 3, 2014 at 16:01
  • If it prints nothing, there wasn't an error. I suspect the last bit - you're building a query, but not executing it - is the fundamental issue.
    – ceejayoz
    Nov 3, 2014 at 16:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.