Asked  9 Months ago    Answers:  5   Viewed   56 times

I get this error, I tried going through other similar threads but it didn't help.

here is php

$host = "http://www.domain.com";
$hostname = "localhost";
$username = "aaa";
$password = "sss";
$userstable = "ddd";
$dbName = "fff";

if ($url != $host){
$con = mysqli_connect($hostname, $username, $password, $dbName);
//@mysqli_select_db("$dbName");
// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query = mysqli_fetch_row(mysqli_query($con, "SELECT * FROM $userstable where(url = '$url')"));

if ($query == false){
$hits = "1";
$query2 = "INSERT INTO $userstable (url,hits) VALUES('$url','$hits')";
}
else {
$hitquery =  "SELECT `hits` FROM $userstable where url = '$url'";
$result = mysqli_query($con, $hitquery);
$hits = mysqli_fetch_assoc($result, 0,  'hits');
//$hits = mysqli_result(mysqli_query("SELECT `hits` FROM $userstable where url = '$url'"), 0,  "hits");
$query2 = "UPDATE $userstable SET `hits` = hits+1 where url = '$url'";
}
mysqli_query($con, $query2);
}
if(!$url) {
$url =  "$host";
}

eror generate on line 157

$hits = mysqli_fetch_assoc($result, 0,  'hits');

How to fix this mysqli ?

please you're help.

 Answers

45

Syntax mysqli_fetch_assoc is:

array mysqli_fetch_assoc ( mysqli_result $result )

Try (for PHP >=5.4):

$hits = mysqli_fetch_assoc($result)['hits'];
Wednesday, March 31, 2021
 
aWebDeveloper
answered 9 Months ago
16

mysqli_num_rows does nothing even remotely similar to mysql_result.

The replacement for mysql_result in mysqli in this case would be to fetch the entire row and use the first element only, something like;

$result = mysql_query("SELECT COUNT(*) FROM xxx limit 2") or die(mysql_error());
$row = mysqli_fetch_row($result);
$count = $row[0];

for($i=0; $i<$count;$i++){
    echo '<li data-target="#transition-timer-carousel" data-slide-to="'.$i.'"'; if($i==0){ echo 'class="active"'; } echo '></li>';
}
Saturday, May 29, 2021
 
turik
answered 7 Months ago
18

mysqli_error() needs you to pass the connection to the database as a parameter. Documentation here has some helpful examples:

http://php.net/manual/en/mysqli.error.php

Try altering your problem line like so and you should be in good shape:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection)); 
Wednesday, June 2, 2021
 
binoculars
answered 7 Months ago
39

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
answered 5 Months ago
36

This is how your code should look (with added SQL Injection protection):

<?php
include "dbinfo.php"; //contains mysqli_connect information (the $mysqli variable)
//inputs
$name = mysqli_real_escape_string($_GET['name']);
$text = mysqli_real_escape_string($_GET['text']);

$sqlqr = "INSERT INTO `ncool`.`coolbits_table` (`name`, `text`, `date`) VALUES ('" . $name . "', '" . $text . "', CURRENT_TIMESTAMP);";

mysqli_query($mysqli,$sqlqr); //function where the magic happens.
?>

Take a look at what I've done. Firstly I've escaped the user input you're retrieving into the $name and $text variables (this is pretty much a must for security reasons) and as others have suggested you should preferably be using prepared statements.

The problem is that you weren't surrounding string values with single quotes ('), which is a requirement of the SQL syntax.

I hope this helps to answer your question.

Thursday, September 2, 2021
 
Pachvarsh
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share