0

I got this error while adding this code. Would appreciate some help. It's for a CS jackpot site.

$sitename = "website.com"; // YOUR DOMAIN
$link = mysql_connect("localhost", "db_user", "db_pass"); // MYSQL , LOCALHOOST , USERNAME , PASSWORD 
$db_selected = mysql_select_db('db_name', $link); // MYSQL DATABASE
mysql_query("SET NAMES utf8");

function fetchinfo($rowname,$tablename,$finder,$findervalue) {
    if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
    else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
    while($row = mysql_fetch_assoc($query))
        return $row[$rowname];
}
5
  • 2
    Possible duplicate of mysql_fetch_array() expects parameter 1 to be resource (or mysqli_result), boolean given Commented Feb 3, 2016 at 7:49
  • shouldn't $query be $result? Commented Feb 3, 2016 at 7:53
  • 2
    while($row = mysql_fetch_assoc($result)), not while($row = mysql_fetch_assoc($query)) Commented Feb 3, 2016 at 7:53
  • still getting the error, now with "boolean give" Commented Feb 3, 2016 at 8:06
  • Which suggests that the query failed. Echo out the query that it is trying to execute and hopefully we can see the error in it. Commented Feb 3, 2016 at 11:12

2 Answers 2

1

Some tips:

  • Use mysqli better than mysql

  • Split the vars in the query, like "SELECT ".$rowname." FROM ".$tablename;

Sign up to request clarification or add additional context in comments.

1 Comment

I have no idea how to translate this into mysqli: function fetchinfo($rowname,$tablename,$finder,$findervalue) { if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename"); else $result = mysql_query("SELECT $rowname FROM $tablename WHERE $finder='$findervalue'"); while($row = mysql_fetch_assoc($query)) return $row[$rowname];
0

Hope this help...

<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('host','username','password','database_name');

if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

//MySqli Select Query
$result = $mysqli->query("SELECT id, product_name FROM products");

while($row = $results->fetch_assoc()) {
    echo $row["id"].' - '.$row["product_name"].'<br>';
}  

$results->free();
$mysqli->close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.