首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress中MYSQL连接的问题-无法通过套接字'/var/lib/ MySQL /mysql.sock‘连接到本地mysql服务器(2)

Wordpress中MYSQL连接的问题-无法通过套接字'/var/lib/ MySQL /mysql.sock‘连接到本地mysql服务器(2)
EN

Stack Overflow用户
提问于 2017-08-30 06:51:00
回答 2查看 1.4K关注 0票数 0

所以我的数据库查找代码有一个问题:Website Here

这是非常奇怪的,因为我在一个网络主机上(谷歌爸爸),我已经确认他们没有任何问题。

它以前工作得很好,但后来我转到了其他一些项目,我又回到了这个错误上。{无法通过套接字'/var/lib/ MySQL /mysql.sock‘(2)}连接到本地mysql服务器,这是在搜索表单成功尝试查询数据库时导致的。

任何帮助都将不胜感激

代码语言:javascript
复制
  Paste your text here<?php

get_header();

?>



<div id="main-content" style = 'position: relative'>

    <div class = 'wrapper'> 

    <h1 class="entry-title main_title"><?php the_title(); ?></h1>


    <div class = 'sic-text'>
        <p> SIC codes are assigned by the Government and are standard at the 4 digit level.
            The 8 digit codes may be customized by each individual list owner. Because we represent all list
            sources, there may be variance in what the 8 digit codes represent. For greatest accuracy,
            when speaking with one of our List Brokers please supply the sic code # along with a description so
            we can provide as exact a match as possible.To use this search, simply type the Industry you’re
            looking for into the Search By Keyword field. For instance, entering “Dentists” will cause all
            businesses related to dentists listed. <! — If you know the SIC code and want to know the industry
            name, enter the 8 digit code into the Search By Code field. –> </p>

    </div>




    <form action="" method="GET" class = 'sic-search'> 



    <input class = 'sic-search-text' type="text" name="sic" placeholder="Search for an industry, eg 'Agriculture'"/>


    <input type="submit" value="Search" class = 'sic-search-button'/>
    </form>


<?php 
echo $error;
$sic = $_GET['sic'];

$servername='confidential';
$username='confidential';
$password='confidential';

$conn = new mysqli($servername, $username, $password);

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

 $min_length = 2;
    // you can set minimum length of the sic if you want

    if(strlen($sic) >= $min_length && $sic != ''){ // if sic length is more or equal minimum length then

        $sic = htmlspecialchars($sic); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $sic = mysql_real_escape_string($sic);
        // makes sure nobody uses SQL injection


         $sql =  SELECT * FROM siccodes WHERE (`description` LIKE '%".$sic."%') OR (`two-digit-sic` LIKE '%".$sic."%') OR (`description-2` LIKE '%".$sic."%') OR (`four-digit-sic` LIKE '%".$sic."%') OR (`description-3` LIKE '%".$sic."%') OR (`six-digit-sic` LIKE '%".$sic."%');

        $result = mysqli_query($conn, $sql);

 if(mysqli_num_rows($result) > 0){ // if one or more rows are returned do following

echo "<p style = 'text-align:center; margin-bottom: 10px;' id = 'rowCount'> </p>";

echo "<div class = 'print-container'>";
echo "<button class = 'print-button' onclick='window.print()'> Print  </button>"; 
echo "</div>";


echo "<div style = 'overflow-x: auto;'>
<table class = 'sic-code-table'>
<tr>
<th>Description</th>
<th>Two Digit SIC</th>
<th>Description</th>
<th>Four Digit SIC</th>
<th>Description</th>
<th>Six Digit SIC</th>
</tr>";



    while($row = mysqli_fetch_assoc($result)){
     // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop


echo "<tr>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['two-digit-sic'] . "</td>";
echo "<td>" . $results['description-2'] . "</td>";
echo "<td>" . $results['four-digit-sic'] . "</td>";
echo "<td>" . $results['description-3'] . "</td>";
echo "<td>" . $results['six-digit-sic'] . "</td>";
echo "</tr>";

}
echo "</table>";
echo "</div>";

        } else{ // if there is no matching rows do following
            echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'>No results match '$sic' Please try another search term.</p>";
        }

    }else {
        if ($sic != '') {
        echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'> Please search again, '$sic' is to short.</p>";
        }

    } // end of checking mimimum string number
?>

<?php if(mysql_num_rows($raw_results) > 100) : ?>
<form action="" method="GET" class = 'sic-search'>



    <input class = 'sic-search-text' type="text" name="sic" placeholder="Search for an industry, eg 'Agriculture'"/>


    <input type="submit" value="Search" class = 'sic-search-button'/>
    </form>
<?php endif; ?>




    </div> <!-- end of wrapper -->


</script>
</div> <!-- #main-content -->

<?php get_footer(); ?> :)
EN

回答 2

Stack Overflow用户

发布于 2017-08-30 06:58:45

好吧,对于初学者来说,它不能工作,因为你还没有启动任何数据库连接。

第二件事是,WP有自己的类来处理数据库,叫做wpdb。请使用它与可湿性粉剂数据库交互。有关这个类、它的方法等的信息可以在documenationold documenation中找到

你可以这样做

代码语言:javascript
复制
global $wpdb;
// do your escaping stuff
$results = $wpdb->get_results('your query', ARRAY_A);
if(count($results) > 0)
{
    foreach($results as $item)
    {
        //output items here as $item['column_name'];
    }
}

请注意,这只是一个非常基本的示例。阅读上面提供的文档可以更好地理解wpdb类、最佳实践等。

票数 0
EN

Stack Overflow用户

发布于 2017-08-30 07:03:48

检查您的套接字是否在my.cnf中正确列出

代码语言:javascript
复制
socket=/var/lib/mysql/mysql.sock

检查mysql是否正在运行

代码语言:javascript
复制
service mysql status

代码语言:javascript
复制
mysqladmin -u root -p status

mysql不要忘记权限错误可能会导致这种情况,请更改的目录权限,如下所示:

代码语言:javascript
复制
chmod -R 755 /var/lib/mysql/

重启mysql-server

代码语言:javascript
复制
service mysql restart

NJOY

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45949398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档