开发者

Need help in converting my PHP & MySQL script from mysql to mysqli?

开发者 https://www.devze.com 2022-12-13 12:36 出处:网络
My script was working before but when I changed all the mysql tags to mysqli everything stopped working. Can some please help me make the script work agian.

My script was working before but when I changed all the mysql tags to mysqli everything stopped working. Can some please help me make the script work agian.

Here is the script.

<?php

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "sitename";

mysqli_connect($db_host, $db_user, $db_pass) or die(mysqli_error());
mysqli_select_db($db_name);

function tag_info() {

$page = "3";.
$result = mysqli_query("SELECT tags.tag, COUNT(questions_tags.tag_id) 'num' FROM questions_tags JOIN tags ON tags.id = questions_tags.tag_id WHERE que开发者_运维技巧stions_tags.users_questions_id = '$page' GROUP BY tags.tag ORDER BY num DESC");
while($row = mysqli_fetch_array($result)) {
$arr[$row['tag']] = $row['num'];
}
ksort($arr);
return $arr;
}

function tag_cloud() {

$min_size = .8;
$max_size = 1.5;

$tags = tag_info();

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

if($spread == 0) {
$spread = 1;
}

$cloud_html = '';
$cloud_tags = array();

foreach ($tags as $tag => $count) {
$size = $min_size + ($count - $minimum_count)
* ($max_size - $min_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. $size . 'em'
. '" class="tag_cloud" href="http://www.example.com/tags/' . $tag .'/'
. '" title="\'' . $tag . '\' returned a count of ' . $count . '">'
. htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;

}

?>

<div id="wrapper">
<?php print tag_cloud(); ?>
</div>


Converting from mysql_* to mysqli_* is not that simple : you should take a quick look at the manual ;-)

For instance, you'll see that mysqli_query is waiting for two parameters, in procedural style :

mixed mysqli_query  ( mysqli $link  , string $query  [, int $resultmode  ] )

Which means you have to pass the link identifier (returned by mysqli_connect) as a first parameter -- and the SQL query as the second one, and not the first one as you did.


The same stands for mysqli_select_db, btw :

bool mysqli_select_db  ( mysqli $link  , string $dbname  )

And maybe for other functions too : I didn't check the manual for every functions your are using.


Another one : when checking if the connection (call to mysqli_connect) has been successfully established or not, you should not use mysqli_error, but the dedicated function mysqli_connect_error.

And [mysqli_error], that you can get to determine if there was an error during the execution of a query, also takes the link identifier as a parameter.


As a sidenote : when reading the documentation for mysqli_*, note that this extension has two API :

  • object oriented
  • and procedural style

The functions using the procedural style often require more parameters (especially, the link identifier) than the methods when using OO-style...

0

精彩评论

暂无评论...
验证码 换一张
取 消